Exemplo n.º 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();
            }
        }
Exemplo n.º 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 { }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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 { }
 }
Exemplo n.º 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 { }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 11
0
 void CloseConnection(RemoteHostState state, Exception ex)
 {
     CloseConnection(state, false, ex);
 }
Exemplo n.º 12
0
 void CloseConnection(RemoteHostState state, bool remote)
 {
     CloseConnection(state, remote, null);
 }
Exemplo n.º 13
0
 public void SendTo(byte[] data, RemoteHostState client)
 {
     client.Send(data);
 }
Exemplo n.º 14
0
 public void SendTo(string str, RemoteHostState client)
 {
     SendTo(EncodeString(str), client);
 }
Exemplo n.º 15
0
 public void SendTo(byte[] data, RemoteHostState client)
 {
     client.Send(data);
 }
Exemplo n.º 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();
        }
Exemplo n.º 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);
 }
Exemplo n.º 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 { }
 }
Exemplo n.º 19
0
        private bool DoCloseConnection()
        {
            if(RemoteState != null)
            {
                RemoteState.GotDataToSend -= RemoteState_GotDataToSend;
                RemoteState.Close();
                RemoteState = null;

                return true;
            }
            else
                return false;
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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 { }
     }
 }
Exemplo n.º 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);
            }
        }
Exemplo n.º 24
0
 void CloseConnection(RemoteHostState state, Exception ex)
 {
     CloseConnection(state, false, ex);
 }
Exemplo n.º 25
0
        void state_GotDataToSend(object sender, EventArgs e)
        {
            RemoteHostState state = (RemoteHostState)sender;

            DoSend(state);
        }
Exemplo n.º 26
0
 void CloseConnection(RemoteHostState state, bool remote)
 {
     CloseConnection(state, remote, null);
 }
Exemplo n.º 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 { }
        }
Exemplo n.º 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 { }
        }
Exemplo n.º 29
0
 public void SendTo(string str, RemoteHostState client)
 {
     SendTo(EncodeString(str), client);
 }