예제 #1
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();
            }
        }
예제 #2
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 { }
        }
예제 #3
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);
            }
        }
예제 #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
        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);
            }
        }
예제 #6
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 { }
 }
예제 #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 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;

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

            if (doSendMore)
            {
                DoSend();
            }
        }
예제 #9
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();
            }
        }
예제 #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
 public void SendTo(byte[] data, RemoteHostState client)
 {
     client.Send(data);
 }
예제 #16
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();
        }
예제 #17
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);
 }
예제 #18
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 { }
 }
예제 #19
0
        private bool DoCloseConnection()
        {
            if(RemoteState != null)
            {
                RemoteState.GotDataToSend -= RemoteState_GotDataToSend;
                RemoteState.Close();
                RemoteState = null;

                return true;
            }
            else
                return false;
        }
예제 #20
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();
            }
        }
예제 #21
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);
            }
        }
예제 #22
0
 public void Disconnect()
 {
     if(RemoteState != null && RemoteState.Close())
     {
         RemoteState = null;
         try
         {
             //Signal that we got disconnected
             if (Disconnected != null)
                 Disconnected(this, new NetworkEventArgs(Hostname, Port));
         }
         catch { }
     }
 }
예제 #23
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);
            }
        }
예제 #24
0
 void CloseConnection(RemoteHostState state, Exception ex)
 {
     CloseConnection(state, false, ex);
 }
예제 #25
0
        void state_GotDataToSend(object sender, EventArgs e)
        {
            RemoteHostState state = (RemoteHostState)sender;

            DoSend(state);
        }
예제 #26
0
 void CloseConnection(RemoteHostState state, bool remote)
 {
     CloseConnection(state, remote, null);
 }
예제 #27
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 { }
        }
예제 #28
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 { }
        }
예제 #29
0
 public void SendTo(string str, RemoteHostState client)
 {
     SendTo(EncodeString(str), client);
 }