@@ -244,51 +244,43 @@ abstract class _SocketProfile {
244
244
InternetAddress addr,
245
245
int port,
246
246
) {
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
+ );
251
264
}
252
265
253
266
static void collectStatistic (
254
267
int id,
255
268
_SocketProfileType type, [
256
- dynamic object,
269
+ Object ? object,
257
270
]) {
258
271
final idKey = id.toString ();
259
272
if (! _enableSocketProfiling) {
260
273
return ;
261
274
}
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 ;
268
280
switch (type) {
269
- case _SocketProfileType .startTime:
270
- stats.startTime = Timeline .now;
271
- break ;
272
281
case _SocketProfileType .endTime:
273
282
stats.endTime = Timeline .now;
274
283
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 ;
292
284
case _SocketProfileType .readBytes:
293
285
if (object == null ) return ;
294
286
stats.readBytes += object as int ;
@@ -299,8 +291,13 @@ abstract class _SocketProfile {
299
291
stats.writeBytes += object as int ;
300
292
stats.lastWriteTime = Timeline .now;
301
293
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
+ );
304
301
}
305
302
}
306
303
@@ -336,25 +333,34 @@ enum _SocketProfileType {
336
333
/// Socket statistic
337
334
class _SocketStatistic {
338
335
final String id;
339
- int ? startTime;
336
+ final int startTime;
340
337
int ? endTime;
341
- String ? address;
342
- int ? port;
343
- String ? socketType;
338
+ final String address;
339
+ final int port;
340
+ final String socketType;
344
341
int readBytes = 0 ;
345
342
int writeBytes = 0 ;
346
343
int ? lastWriteTime;
347
344
int ? lastReadTime;
348
345
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
+ });
350
353
351
354
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.
354
363
_setIfNotNull (map, 'endTime' , endTime);
355
- _setIfNotNull (map, 'address' , address);
356
- _setIfNotNull (map, 'port' , port);
357
- _setIfNotNull (map, 'socketType' , socketType);
358
364
_setIfNotNull (map, 'readBytes' , readBytes);
359
365
_setIfNotNull (map, 'writeBytes' , writeBytes);
360
366
_setIfNotNull (map, 'lastWriteTime' , lastWriteTime);
0 commit comments