Пример #1
0
        void DoSend(RemoteHostState state)
        {
            try
            {
                byte[] data = null;
                lock (state.SendQueue)
                {
                    if (state.SendQueue.Count > 0)
                    {
                        data = state.SendQueue.Peek();
                    }
                }

                if (data != null)
                {
                    state.Stream.BeginWrite(data, 0, data.Length, writeCallback, state);
                }
            }
            catch (System.IO.IOException ioe)
            {
                HandleIOException(ioe, state);
            }
            //We dont need to take care of ObjectDisposedException.
            //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already
            catch { }
        }
Пример #2
0
        private void ConnectCallback(IAsyncResult ar)
        {
            TcpClient client = null;

            try
            {
                client = (TcpClient)ar.AsyncState;
                client.EndConnect(ar);
                client.NoDelay = true;

                RemoteState = new RemoteHostState(client);
                RemoteState.GotDataToSend += RemoteState_GotDataToSend;
                RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, readCallback, RemoteState);

                OnOpenedConnection();
            }
            catch (Exception ex)
            {
                if (RemoteState != null)
                {
                    DoCloseConnection();
                }
                else
                {
                    if (client != null)
                    {
                        client.Close();
                        client = null;
                    }
                }
                OnClosedConnection(ex);
            }
        }
Пример #3
0
        private void ConnectCallback_obsolete(IAsyncResult ar)
        {
            TcpClient client = null;

            try
            {
                client = (TcpClient)ar.AsyncState;
                client.EndConnect(ar);
                client.NoDelay = true;

                RemoteState = new RemoteHostState(client);
                RemoteState.GotDataToSend += new EventHandler <EventArgs>(RemoteState_GotDataToSend);
                RemoteState.Stream.BeginRead(RemoteState.ReadBuffer, 0, RemoteState.ReadBuffer.Length, new AsyncCallback(RecvCallback_obsolete), null);

                OnConnected();
            }
            catch
            {
                if (RemoteState != null)
                {
                    RemoteState.Close();
                    RemoteState = null;
                }
                else if (client != null)
                {
                    client.Close();
                    client = null;
                }

                OnFailedConnect();
            }
        }
Пример #4
0
 void HandleIOException(System.IO.IOException ioe, RemoteHostState state)
 {
     if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError))
     {
         System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException;
         CloseConnection(state, (se.SocketErrorCode == SocketError.Interrupted) ? null : se);
     }
     else
     {
         CloseConnection(state, ioe);
     }
 }
Пример #5
0
 protected void OnClientConnected(RemoteHostState state)
 {
     try
     {
         if (ClientConnectionStateChanged != null)
         {
             ClientConnectionEventArgs eventargs = new ClientConnectionEventArgs(state.EndPoint.Address.ToString(), state.EndPoint.Port, true, null, true);
             eventargs.Client = state;
             ClientConnectionStateChanged(this, eventargs);
         }
     }
     catch { }
 }
Пример #6
0
        void CloseConnection(RemoteHostState state, bool remote, Exception ex)
        {
            if (state != null)
            {
                lock (clients)
                    clients.Remove(state);
                IPEndPoint remoteHost = state.EndPoint;
                state.GotDataToSend -= state_GotDataToSend;
                state.Close();

                OnClientConnectionStateChanged(remoteHost, false, ex, remote);
            }
        }
Пример #7
0
        void ReadCallback(IAsyncResult ar)
        {
            RemoteHostState state = (RemoteHostState)ar.AsyncState;

            try
            {
                int len = 0;
                len = state.Stream.EndRead(ar);

                if (len == 0)
                {
                    CloseConnection(state, true);
                }
                else
                {
                    try
                    {
                        if (ProtocolStrategy != null)
                        {
                            if (ProtocolStrategy.Encoding != null)
                            {
                                if (state.Decoder == null)
                                {
                                    state.Decoder = ProtocolStrategy.Encoding.GetDecoder();
                                }

                                int    charCount = state.Decoder.GetCharCount(state.ReadBuffer, 0, len);
                                char[] chars     = new char[charCount];
                                state.Decoder.GetChars(state.ReadBuffer, 0, len, chars, 0);
                                string msg = new string(chars);
                                ProtocolStrategy.Parse(msg, state);
                            }
                            else
                            {
                                ProtocolStrategy.Parse(state.ReadBuffer, len, state);
                            }
                        }
                    }
                    catch { }

                    state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state);
                }
            }
            catch (System.IO.IOException ioe)
            {
                HandleIOException(ioe, state);
            }
            //We dont need to take care of ObjectDisposedException.
            //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already
            catch { }
        }
Пример #8
0
        void AcceptCallback(IAsyncResult ar)
        {
            RemoteHostState state          = null;
            bool            beginNewAccept = true;

            try
            {
                if (ar.IsCompleted)
                {
                    state = new RemoteHostState(listener.EndAcceptTcpClient(ar));
                    state.GotDataToSend += state_GotDataToSend;
                    state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state);
                }
                else
                {
                    beginNewAccept = false;
                }
            }
            catch (SocketException se)
            {
                if (se.SocketErrorCode == SocketError.Interrupted)
                {
                    beginNewAccept = false;
                }
            }
            catch (ObjectDisposedException)
            {
                beginNewAccept = false;
            }
            catch (Exception ex)
            {
                CloseConnection(state, ex);
                state = null;
            }

            if (state != null)
            {
                lock (clients)
                    clients.Add(state);

                OnClientConnected(state);
            }

            if (beginNewAccept)
            {
                DoBeginAccept();
            }
        }
Пример #9
0
        void WriteCallback(IAsyncResult ar)
        {
            try
            {
                RemoteHostState state = ar.AsyncState as RemoteHostState;
                state.Stream.EndWrite(ar);
            }
            catch (System.IO.IOException ioe)
            {
                if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError))
                {
                    System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException;
                    if (DoCloseConnection())
                    {
                        OnClosedConnection((se.SocketErrorCode == SocketError.Interrupted) ? null : se);
                    }
                }
                else
                if (DoCloseConnection())
                {
                    OnClosedConnection(ioe);
                }

                return;
            }
            //We dont need to take care of ObjectDisposedException.
            //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already
            catch { }

            bool doSendMore = false;

            lock (RemoteState.SendQueue)
            {
                RemoteState.SendQueue.Dequeue();
                if (RemoteState.SendQueue.Count > 0)
                {
                    doSendMore = true;
                }
            }

            if (doSendMore)
            {
                DoSend();
            }
        }
Пример #10
0
        void WriteCallback(IAsyncResult ar)
        {
            RemoteHostState state = (RemoteHostState)ar.AsyncState;

            try
            {
                state.Stream.EndWrite(ar);
            }
            catch (System.IO.IOException ioe)
            {
                HandleIOException(ioe, state);
                return;
            }
            //We dont need to take care of ObjectDisposedException.
            //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already
            catch { }

            bool doSendMore = false;

            lock (state.SendQueue)
            {
                if (state.SendQueue.Count > 0)   //This should always be true, since the currently sending data is left in the queue until this point
                {
                    state.SendQueue.Dequeue();
                }

                if (state.SendQueue.Count > 0)
                {
                    doSendMore = true;
                }
            }

            if (doSendMore)
            {
                DoSend(state);
            }
        }
Пример #11
0
 void CloseConnection(RemoteHostState state, Exception ex)
 {
     CloseConnection(state, false, ex);
 }
Пример #12
0
 void CloseConnection(RemoteHostState state, bool remote)
 {
     CloseConnection(state, remote, null);
 }
Пример #13
0
 public void SendTo(byte[] data, RemoteHostState client)
 {
     client.Send(data);
 }
Пример #14
0
 public void SendTo(string str, RemoteHostState client)
 {
     SendTo(EncodeString(str), client);
 }
Пример #15
0
        void state_GotDataToSend(object sender, EventArgs e)
        {
            RemoteHostState state = (RemoteHostState)sender;

            DoSend(state);
        }
Пример #16
0
        private void ReadCallback(IAsyncResult ar)
        {
            try
            {
                RemoteHostState state = ar.AsyncState as RemoteHostState;
                int             len   = 0;
                len = state.Stream.EndRead(ar);

                if (len == 0)
                {
                    CloseConnection();
                }
                else
                {
                    try
                    {
                        if (ProtocolStrategy != null)
                        {
                            if (ProtocolStrategy.Encoding != null)
                            {
                                if (state.Decoder == null)
                                {
                                    state.Decoder = ProtocolStrategy.Encoding.GetDecoder();
                                }

                                int    charCount = state.Decoder.GetCharCount(state.ReadBuffer, 0, len);
                                char[] chars     = new char[charCount];
                                state.Decoder.GetChars(state.ReadBuffer, 0, len, chars, 0);
                                string msg = new string(chars);

                                ProtocolStrategy.Parse(msg, state);
                            }
                            else
                            {
                                ProtocolStrategy.Parse(state.ReadBuffer, len, state);
                            }
                        }
                    }
                    catch { }

                    state.Stream.BeginRead(state.ReadBuffer, 0, state.ReadBuffer.Length, readCallback, state);
                }
            }
            catch (System.IO.IOException ioe)
            {
                if (ioe.InnerException.GetType() == typeof(System.Net.Sockets.SocketError))
                {
                    System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)ioe.InnerException;

                    if (DoCloseConnection())
                    {
                        OnClosedConnection((se.SocketErrorCode == SocketError.Interrupted) ? null : se);
                    }
                }
                else
                if (DoCloseConnection())
                {
                    OnClosedConnection(ioe);
                }
            }
            //We dont need to take care of ObjectDisposedException.
            //ObjectDisposedException would indicate that the state has been closed, and that means it has been disconnected already
            catch { }
        }