Пример #1
0
 public static void Disconnect()
 {
     IsConnected = false;
     _messageListener = null;
     try
     {
         try
         {
             if (_serverObject != null)
                 _serverObject.Detach(_clientObject);
         }
         catch (Exception)
         {
             _serverObject = null;
         }
     }
     finally
     {
         if (_channel != null)
         {
             ChannelServices.UnregisterChannel(_channel);
             _channel = null;
         }
     }
 }
Пример #2
0
 private static void HandleError()
 {
     _messageListener = null;
     _serverObject = null;
     _clientObject = null;
     if (_channel != null)
     {
         ChannelServices.UnregisterChannel(_channel);
         _channel = null;
     }
     IsConnected = false;
 }
Пример #3
0
        public static void Connect(IMessageListener messageListener)
        {
            Timer.Change(0, Timeout.Infinite);
            if (messageListener == null) return;
            if (string.IsNullOrWhiteSpace(LocalSettings.MessagingServerName)) return;

            _messageListener = messageListener;
            var serverProv = new BinaryServerFormatterSinkProvider
                                 {
                                     TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
                                 };

            var clientProv = new BinaryClientFormatterSinkProvider();

            IDictionary props = new Hashtable();
            props["port"] = 0;

            _channel = new TcpChannel(props, clientProv, serverProv);

            ChannelServices.RegisterChannel(_channel, false);

            var url = String.Format("tcp://{0}:{1}/ChatServer", LocalSettings.MessagingServerName, LocalSettings.MessagingServerPort);

            try
            {
                _serverObject = (MessagingServerObject)Activator.GetObject(typeof(MessagingServerObject), url);
                _clientObject = new MessagingClientObject();
                _serverObject.Attach(_clientObject);
            }
            catch
            {
                HandleError();
                return;
            }
            IsConnected = true;
            Timer.Change(0, 1000);
        }