//------------------------------------------------------------------------------------------------------------------------- public bool Connect() { YPChannel.Transport.Sockets.Client _client = null; lock (this) try { //check that is not already connecting/conntected var _tmpypc = ClientChannel; if (_tmpypc != null && _tmpypc.State != YPChannel.ChannelStates.Closed) { return(_tmpypc.State == YPChannel.ChannelStates.Open); } //dispose previous ypchannel if (_tmpypc != null) { try { _tmpypc.Close("Resetting channel"); } catch { } try { _tmpypc.Dispose(); } catch { } } //create ypchannel client _client = new YPChannel.Transport.Sockets.Client(NodeDiscoverManager.Protocol); SetupChannel(_client); //connect DebugEx.TraceLog($"RemoteNode : Connecting to node. (ip:{IPAddress} port:{RemotePort} nodekey:{RemoteNodeKey})"); lastConnectionAttempt = DateTime.Now; var conRes = _client.Connect(IPAddress, RemotePort, false); if (conRes) { DebugEx.TraceLog($"RemoteNode : Connected to node. (ip:{IPAddress} port:{RemotePort} nodekey:{RemoteNodeKey})"); return(true); } else { try { _client.Close("error(1)"); } catch { } try { _client.Dispose(); } catch { } return(false); } } catch (Exception ex) { DebugEx.TraceError(ex, "Error while connecting to discovered node"); //close channel try { _client?.Close("erro(2)r"); } catch { } try { _client?.Dispose(); } catch { } //switch state to reconnection sleep lastConnectionAttempt = DateTime.Now; return(false); } }
//------------------------------------------------------------------------------------------------------------------------ public StatusCode TearDown() { try { audiosource.Stop(); yclient.Close(); return(StatusCode.Success); } catch (Exception ex) { return(StatusCode.Error); } }
//------------------------------------------------------------------------------------------------------------------------- internal void _disconnect() { try { //close client and channel (if server) try { ClientChannel?.Close("disconnecting"); unhookChannel(ClientChannel); ClientChannel?.Dispose(); } catch { } try { ServerChannel?.Close("disconnecting"); unhookChannel(ServerChannel); ServerChannel?.Dispose(); } catch { } //let go of references ClientChannel = null; ServerChannel = null; } catch (Exception ex) { DebugEx.TraceError(ex, "Unhandled exception in disconnection"); } }
//------------------------------------------------------------------------------------------------------------------------- public void SetupChannel(YPChannel.Channel _channel) { if (_channel == null) { DebugEx.Assert("null channel"); return; } //check for previous channels if (_channel.ChannelRole == YPChannel.ChannelRole.Client && ClientChannel != null) { try { ClientChannel.Close("channel setup (1)"); } catch { } try { ClientChannel.Dispose(); } catch { } unhookChannel(ClientChannel); ClientChannel = null; } else if (_channel.ChannelRole == YPChannel.ChannelRole.Server && ServerChannel != null) { try { ServerChannel.Close("channel setup (2)"); } catch { } try { ServerChannel.Dispose(); } catch { } unhookChannel(ServerChannel); ServerChannel = null; } //keep new channel if (_channel is YPChannel.Transport.Sockets.ServerChannel) { ServerChannel = _channel as YPChannel.Transport.Sockets.ServerChannel; } else if (_channel is YPChannel.Transport.Sockets.Client) { ClientChannel = _channel as YPChannel.Transport.Sockets.Client; } else { DebugEx.Assert("Should not be here"); } //hook channel events hookChannel(_channel); }