Beispiel #1
0
        public void Disconnect()
        {
            if (!IsConnected)
            {
                throw new InvalidOperationException();
            }

            Context.AcquireWriteLock();

            try
            {
                _goodbyeReset.Reset();
                _state = ClientState.WaitingForGoodbye;
                Context.Writer.WriteGoodbye();
                InvokeGoodbyeSent(Context.Client);
                _goodbyeReset.WaitOne();
                IsConnected = false;
                ReadThread.TryKill();
                InvokeGoodbyeReceived(Context.Client);
            }
            finally
            {
                Context.ReleaseWriteLock();
            }
        }
Beispiel #2
0
        public void WriteObject(TMessage message)
        {
            var s = JsonSerializer.Serialize(message);

            Context.AcquireWriteLock();

            try
            {
                Context.Writer.WriteData(s);
            }
            finally
            {
                Context.ReleaseWriteLock();
            }
        }
Beispiel #3
0
        public void WriteData(string data)
        {
            Context.AcquireWriteLock();

            try
            {
                Context.Writer.WriteData(data);
            }
            finally
            {
                Context.ReleaseWriteLock();
            }

            if (MessageSent != null)
            {
                MessageSent(this, data.GetBytes());
            }
        }
Beispiel #4
0
        public void Connect(string hostname)
        {
            if (IsConnected)
            {
                throw new InvalidOperationException();
            }

            do
            {
                var c = new TcpClient();
                InvokeConnecting(c);

                try
                {
                    c.Connect(hostname, Port);
                    Context = ClientContext.FromClient(c);
                    Context.AcquireWriteLock();
                    Context.AcquireReadLock();

                    try
                    {
                        Context.Writer.WriteHello();
                        InvokeHelloSent(Context.Client);
                        Context.Reader.ReadHello();
                    }
                    finally
                    {
                        Context.ReleaseWriteLock();
                        Context.ReleaseReadLock();
                    }

                    InvokeHelloReceived(Context.Client);
                    ReadThread  = CreateReadThread();
                    IsConnected = true;
                }
                catch (Exception e)
                {
                    if (ConnectFailed != null)
                    {
                        ConnectFailed(this, new ItemEventArgs <Exception>(e));
                    }

                    try
                    {
                        c.Client.Dispose();
                    }
                    catch { }

                    try
                    {
                        c.Client.Shutdown(SocketShutdown.Both);
                    }
                    catch { }

                    try
                    {
                        c.Close();
                    }
                    catch { }

                    Thread.Sleep(1000);
                }
            } while (!IsConnected);

            InvokeConnected(Context.Client);
        }