Example #1
0
        private Thread CreateReadThread()
        {
            return(ThreadHelper.DoBackground(() =>
            {
                while (Context.Client.Connected)
                {
                    Context.AcquireReadLock();
                    Message message = null;

                    try
                    {
                        message = Context.Reader.ReadMessage();
                    }
                    catch (Exception e)
                    {
                        InvokeClientErrorEncountered(new TcpClientError(Context.Client, e));
                    }

                    if (message == null)
                    {
                        Context.ReleaseReadLock();
                        Thread.Sleep(1);

                        try
                        {
                            if (Context.Client.Client.Poll(0, SelectMode.SelectRead) &&
                                Context.Client.Client.Receive(new byte[1], SocketFlags.Peek) == 0)
                            {
                                break;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        catch
                        {
                            break;
                        }
                    }

                    if (MessageReceived != null)
                    {
                        MessageReceived(this, new ItemEventArgs <Message>(message));
                    }

                    HandleMessage(message);
                    Context.ReleaseReadLock();
                }

                IsConnected = false;
                InvokeDisconnecting(Context.Client);
                InvokeDisconnected(Context.Client);
            }));
        }
Example #2
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);
        }