|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:sip_ua/src/transports/tls_socket_impl.dart'; |
| 3 | + |
| 4 | +import '../../sip_ua.dart'; |
| 5 | +import '../logger.dart'; |
| 6 | +import 'socket_interface.dart'; |
| 7 | + |
| 8 | +class SIPUATlsSocket extends SIPUASocketInterface { |
| 9 | + SIPUATlsSocket(String host, String port, |
| 10 | + {required int messageDelay, |
| 11 | + TcpSocketSettings? tcpSocketSettings, |
| 12 | + int? weight}) |
| 13 | + : _messageDelay = messageDelay { |
| 14 | + |
| 15 | + String transportScheme = 'tls'; |
| 16 | + _weight = weight; |
| 17 | + _host = host; |
| 18 | + _port = port; |
| 19 | + |
| 20 | + _sip_uri = '$_tlsSocketProtocol:$host:$port;transport=$transportScheme'; |
| 21 | + _via_transport = transportScheme.toUpperCase(); |
| 22 | + _tcpSocketSettings = tcpSocketSettings ?? TcpSocketSettings(); |
| 23 | + } |
| 24 | + |
| 25 | + final int _messageDelay; |
| 26 | + |
| 27 | + String? _host; |
| 28 | + String? _port; |
| 29 | + String? _sip_uri; |
| 30 | + late String _via_transport; |
| 31 | + final String _tlsSocketProtocol = 'sip'; |
| 32 | + SIPUATlsSocketImpl? _tlsSocketImpl; |
| 33 | + bool _closed = false; |
| 34 | + bool _connected = false; |
| 35 | + bool _connecting = false; |
| 36 | + int? _weight; |
| 37 | + int? status; |
| 38 | + late TcpSocketSettings _tcpSocketSettings; |
| 39 | + |
| 40 | + @override |
| 41 | + String get via_transport => _via_transport; |
| 42 | + |
| 43 | + @override |
| 44 | + set via_transport(String value) { |
| 45 | + _via_transport = value.toUpperCase(); |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + int? get weight => _weight; |
| 50 | + |
| 51 | + @override |
| 52 | + String? get sip_uri => _sip_uri; |
| 53 | + |
| 54 | + String? get host => _host; |
| 55 | + |
| 56 | + String? get port => _port; |
| 57 | + |
| 58 | + @override |
| 59 | + void connect() async { |
| 60 | + print('connect()'); |
| 61 | + |
| 62 | + if (_host == null) { |
| 63 | + throw AssertionError('Invalid argument: _host'); |
| 64 | + } |
| 65 | + if (_port == null) { |
| 66 | + throw AssertionError('Invalid argument: _port'); |
| 67 | + } |
| 68 | + |
| 69 | + if (isConnected()) { |
| 70 | + print('TLSSocket $_host:$_port is already connected'); |
| 71 | + return; |
| 72 | + } else if (isConnecting()) { |
| 73 | + print('TLSSocket $_host:$_port is connecting'); |
| 74 | + return; |
| 75 | + } |
| 76 | + if (_tlsSocketImpl != null) { |
| 77 | + disconnect(); |
| 78 | + } |
| 79 | + print('connecting to TLSSocket $_host:$_port'); |
| 80 | + _connecting = true; |
| 81 | + try { |
| 82 | + _tlsSocketImpl = |
| 83 | + SIPUATlsSocketImpl(_messageDelay, _host ?? '0.0.0.0', _port ?? '5061'); |
| 84 | + |
| 85 | + _tlsSocketImpl!.onOpen = () { |
| 86 | + _closed = false; |
| 87 | + _connected = true; |
| 88 | + _connecting = false; |
| 89 | + print('TLS Socket is now connected'); |
| 90 | + _onOpen(); |
| 91 | + }; |
| 92 | + |
| 93 | + _tlsSocketImpl!.onData = (dynamic data) { |
| 94 | + _onMessage(data); |
| 95 | + }; |
| 96 | + |
| 97 | + _tlsSocketImpl!.onClose = (int? closeCode, String? closeReason) { |
| 98 | + print('Closed [$closeCode, $closeReason]!'); |
| 99 | + _connected = false; |
| 100 | + _connecting = false; |
| 101 | + _onClose(true, closeCode, closeReason); |
| 102 | + }; |
| 103 | + |
| 104 | + _tlsSocketImpl!.connect( |
| 105 | + protocols: <String>[_tlsSocketProtocol], |
| 106 | + tcpSocketSettings: _tcpSocketSettings); |
| 107 | + } catch (e, s) { |
| 108 | + logger.e(e.toString(), stackTrace: s); |
| 109 | + _connected = false; |
| 110 | + _connecting = false; |
| 111 | + print('TLSSocket error: $e'); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @override |
| 116 | + void disconnect() { |
| 117 | + print('disconnect()'); |
| 118 | + if (_closed) return; |
| 119 | + _closed = true; |
| 120 | + _connected = false; |
| 121 | + _connecting = false; |
| 122 | + _onClose(true, 0, 'Client sent disconnect'); |
| 123 | + try { |
| 124 | + if (_tlsSocketImpl != null) { |
| 125 | + _tlsSocketImpl!.close(); |
| 126 | + } |
| 127 | + } catch (error) { |
| 128 | + logger.e('close() | error closing the TLSSocket: $error'); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @override |
| 133 | + bool send(dynamic message) { |
| 134 | + print('send() $message'); |
| 135 | + if (_closed) { |
| 136 | + throw 'transport closed'; |
| 137 | + } |
| 138 | + try { |
| 139 | + _tlsSocketImpl!.send(message); |
| 140 | + return true; |
| 141 | + } catch (error) { |
| 142 | + logger.e('send() | error sending message: $error'); |
| 143 | + rethrow; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @override |
| 148 | + bool isConnected() => _connected; |
| 149 | + |
| 150 | + void _onOpen() { |
| 151 | + print('TLSSocket $_host:$port connected'); |
| 152 | + onconnect!(); |
| 153 | + } |
| 154 | + |
| 155 | + void _onClose(bool wasClean, int? code, String? reason) { |
| 156 | + print('TLSSocket $_host:$port closed $reason'); |
| 157 | + if (wasClean == false) { |
| 158 | + print('TLSSocket abrupt disconnection'); |
| 159 | + } |
| 160 | + ondisconnect!(this, !wasClean, code, reason); |
| 161 | + } |
| 162 | + |
| 163 | + void _onMessage(dynamic data) { |
| 164 | + print('Received TLSSocket data'); |
| 165 | + if (data != null) { |
| 166 | + if (data.toString().trim().isNotEmpty) { |
| 167 | + ondata!(data); |
| 168 | + } else { |
| 169 | + print('Received and ignored empty packet'); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @override |
| 175 | + bool isConnecting() => _connecting; |
| 176 | + |
| 177 | + @override |
| 178 | + String? get url { |
| 179 | + if (_host == null || _port == null) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + return '$_host:$_port'; |
| 183 | + } |
| 184 | +} |
0 commit comments