Esempio n. 1
0
        //-------------------------------------------------------------------------------------------------------------------------
        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);
        }
Esempio n. 2
0
        //-------------------------------------------------------------------------------------------------------------------------
        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"); }
        }