11import 'package:sip_ua/sip_ua.dart' ;
22import 'package:sip_ua/src/transports/socket_interface.dart' ;
33import 'package:sip_ua/src/transports/tcp_socket_impl.dart' ;
4- import '../grammar.dart' ;
54import '../logger.dart' ;
65
7- import 'websocket_dart_impl.dart'
8- if (dart.library.js) 'websocket_web_impl.dart' ;
9-
106class SIPUATcpSocket extends SIPUASocketInterface {
117 SIPUATcpSocket (String host, String port,
128 {required int messageDelay,
@@ -35,6 +31,7 @@ class SIPUATcpSocket extends SIPUASocketInterface {
3531 SIPUATcpSocketImpl ? _tcpSocketImpl;
3632 bool _closed = false ;
3733 bool _connected = false ;
34+ bool _connecting = false ;
3835 int ? _weight;
3936 int ? status;
4037 late TcpSocketSettings _tcpSocketSettings;
@@ -68,17 +65,26 @@ class SIPUATcpSocket extends SIPUASocketInterface {
6865 throw AssertionError ('Invalid argument: _port' );
6966 }
7067
68+ if (isConnected ()) {
69+ logger.d ('TCPSocket $_host :$_port is already connected' );
70+ return ;
71+ } else if (isConnecting ()) {
72+ logger.d ('TCPSocket $_host :$_port is connecting' );
73+ return ;
74+ }
7175 if (_tcpSocketImpl != null ) {
7276 disconnect ();
7377 }
7478 logger.d ('connecting to TcpSocket $_host :$_port ' );
79+ _connecting = true ;
7580 try {
7681 _tcpSocketImpl = SIPUATcpSocketImpl (
7782 _messageDelay, _host ?? '0.0.0.0' , _port ?? '5060' );
7883
7984 _tcpSocketImpl! .onOpen = () {
8085 _closed = false ;
8186 _connected = true ;
87+ _connecting = false ;
8288 logger.d ('Tcp Socket is now connected?' );
8389 _onOpen ();
8490 };
@@ -90,6 +96,7 @@ class SIPUATcpSocket extends SIPUASocketInterface {
9096 _tcpSocketImpl! .onClose = (int ? closeCode, String ? closeReason) {
9197 logger.d ('Closed [$closeCode , $closeReason ]!' );
9298 _connected = false ;
99+ _connecting = false ;
93100 _onClose (true , closeCode, closeReason);
94101 };
95102
@@ -99,6 +106,7 @@ class SIPUATcpSocket extends SIPUASocketInterface {
99106 } catch (e, s) {
100107 logger.e (e.toString (), stackTrace: s);
101108 _connected = false ;
109+ _connecting = false ;
102110 logger.e ('TcpSocket error: $e ' );
103111 }
104112 }
@@ -110,6 +118,7 @@ class SIPUATcpSocket extends SIPUASocketInterface {
110118 // Don't wait for the WebSocket 'close' event, do it now.
111119 _closed = true ;
112120 _connected = false ;
121+ _connecting = false ;
113122 _onClose (true , 0 , 'Client send disconnect' );
114123 try {
115124 if (_tcpSocketImpl != null ) {
@@ -136,9 +145,7 @@ class SIPUATcpSocket extends SIPUASocketInterface {
136145 }
137146
138147 @override
139- bool isConnected () {
140- return _connected;
141- }
148+ bool isConnected () => _connected;
142149
143150 /**
144151 * TcpSocket Event Handlers
@@ -168,12 +175,13 @@ class SIPUATcpSocket extends SIPUASocketInterface {
168175 }
169176
170177 @override
171- bool isConnecting () {
172- // TODO(cloudwebrtc): implement isConnecting
173- throw UnimplementedError ();
174- }
178+ bool isConnecting () => _connecting;
175179
176180 @override
177- // TODO(cloudwebrtc): implement url
178- String ? get url => throw UnimplementedError ();
181+ String ? get url {
182+ if (_host == null || _port == null ) {
183+ return null ;
184+ }
185+ return '$_host :$_port ' ;
186+ }
179187}
0 commit comments