@@ -244,51 +244,43 @@ abstract class _SocketProfile {
244244 InternetAddress addr,
245245 int port,
246246 ) {
247- _SocketProfile .collectStatistic (id, _SocketProfileType .startTime);
248- _SocketProfile .collectStatistic (id, _SocketProfileType .socketType, type);
249- _SocketProfile .collectStatistic (id, _SocketProfileType .address, addr);
250- _SocketProfile .collectStatistic (id, _SocketProfileType .port, port);
247+ if (! _enableSocketProfiling) {
248+ return ;
249+ }
250+ // TODO(srawlins): Assert that `_idToSocketStatistic` does not contain
251+ // `id.toString()`?
252+ final address =
253+ (addr.type == InternetAddress .anyIPv6 ||
254+ addr.type == InternetAddress .loopbackIPv6)
255+ ? '[${addr .address }]'
256+ : addr.address;
257+ _SocketStatistic (
258+ id.toString (),
259+ startTime: Timeline .now,
260+ socketType: type,
261+ address: address,
262+ port: port,
263+ );
251264 }
252265
253266 static void collectStatistic (
254267 int id,
255268 _SocketProfileType type, [
256- dynamic object,
269+ Object ? object,
257270 ]) {
258271 final idKey = id.toString ();
259272 if (! _enableSocketProfiling) {
260273 return ;
261274 }
262- // Skip socket that started before _enableSocketProfiling turned on.
263- if (! _idToSocketStatistic.containsKey (idKey) &&
264- type != _SocketProfileType .startTime)
265- return ;
266- _SocketStatistic stats =
267- _idToSocketStatistic[idKey] ?? = _SocketStatistic (idKey);
275+ // Skip any socket that started before `_enableSocketProfiling` was turned
276+ // on.
277+ final stats = _idToSocketStatistic[idKey];
278+ assert (stats != null , '"$idKey " not found in "_idToSocketStatistic" map' );
279+ if (stats == null ) return ;
268280 switch (type) {
269- case _SocketProfileType .startTime:
270- stats.startTime = Timeline .now;
271- break ;
272281 case _SocketProfileType .endTime:
273282 stats.endTime = Timeline .now;
274283 break ;
275- case _SocketProfileType .address:
276- assert (object is InternetAddress );
277- final internetAddress = object as InternetAddress ;
278- stats.address =
279- (internetAddress.type == InternetAddress .anyIPv6 ||
280- internetAddress.type == InternetAddress .loopbackIPv6)
281- ? '[${internetAddress .address }]'
282- : internetAddress.address;
283- break ;
284- case _SocketProfileType .port:
285- assert (object is int );
286- stats.port = object;
287- break ;
288- case _SocketProfileType .socketType:
289- assert (object is String );
290- stats.socketType = object;
291- break ;
292284 case _SocketProfileType .readBytes:
293285 if (object == null ) return ;
294286 stats.readBytes += object as int ;
@@ -299,8 +291,13 @@ abstract class _SocketProfile {
299291 stats.writeBytes += object as int ;
300292 stats.lastWriteTime = Timeline .now;
301293 break ;
302- default :
303- throw ArgumentError ('type ${type } does not exist' );
294+ case _SocketProfileType .startTime:
295+ case _SocketProfileType .socketType:
296+ case _SocketProfileType .address:
297+ case _SocketProfileType .port:
298+ throw ArgumentError (
299+ 'The "${type }" type can only be set on initialization' ,
300+ );
304301 }
305302 }
306303
@@ -336,25 +333,34 @@ enum _SocketProfileType {
336333/// Socket statistic
337334class _SocketStatistic {
338335 final String id;
339- int ? startTime;
336+ final int startTime;
340337 int ? endTime;
341- String ? address;
342- int ? port;
343- String ? socketType;
338+ final String address;
339+ final int port;
340+ final String socketType;
344341 int readBytes = 0 ;
345342 int writeBytes = 0 ;
346343 int ? lastWriteTime;
347344 int ? lastReadTime;
348345
349- _SocketStatistic (this .id);
346+ _SocketStatistic (
347+ this .id, {
348+ required this .startTime,
349+ required this .socketType,
350+ required this .address,
351+ required this .port,
352+ });
350353
351354 Map <String , dynamic > toMap () {
352- final map = < String , dynamic > {'id' : id};
353- _setIfNotNull (map, 'startTime' , startTime);
355+ final map = < String , Object > {
356+ 'id' : id,
357+ 'startTime' : startTime,
358+ 'address' : address,
359+ 'port' : port,
360+ 'socketType' : socketType,
361+ };
362+ // TODO(srawlins): Replace with null-aware elements.
354363 _setIfNotNull (map, 'endTime' , endTime);
355- _setIfNotNull (map, 'address' , address);
356- _setIfNotNull (map, 'port' , port);
357- _setIfNotNull (map, 'socketType' , socketType);
358364 _setIfNotNull (map, 'readBytes' , readBytes);
359365 _setIfNotNull (map, 'writeBytes' , writeBytes);
360366 _setIfNotNull (map, 'lastWriteTime' , lastWriteTime);
0 commit comments