コード例 #1
0
ファイル: TcpServer.cs プロジェクト: dvmorozov/sgs
        // Remove a connection from the list of active connections
        private void RemoveConnection(SockWrapper s)
        {
            try
            {
                s.Client.Shutdown(SocketShutdown.Both);
                s.Client.Close();
            }
            catch {}

            // Remove it from the array
            try
            {
                m_aryClients.Remove(s);
            }
            catch {}

            // Fire the Disconnected event
            if (Disconnected != null)
            {
                Disconnected(this, ref s.obj);
            }
        }
コード例 #2
0
ファイル: TcpServer.cs プロジェクト: dvmorozov/sgs
        // Client has sent data, or has disconnected
        private void OnReceivedData(IAsyncResult ar)
        {
            // Socket was the passed in object
            SockWrapper s = (SockWrapper)ar.AsyncState;

            lock (this)
            {
                if (!m_bShuttingDown)
                {
                    // Check if we got any data
                    try
                    {
                        int nBytesRec = s.Client.EndReceive(ar);
                        if (nBytesRec > 0)
                        {
                            if (DataReceived != null)
                            {
                                DataReceived(this, ref s.obj, ref s.byBuff, nBytesRec);
                            }

                            // Restablish the callback
                            AsyncCallback receiveData = new AsyncCallback(OnReceivedData);
                            s.Client.BeginReceive(s.byBuff, 0, s.byBuff.Length, SocketFlags.None, receiveData, s);
                        }
                        else
                        {
                            // If no data was received then the connection is probably dead
                            RemoveConnection(s);
                        }
                    }
                    catch
                    {
                        RemoveConnection(s);
                    }
                }
            }
        }
コード例 #3
0
ファイル: TcpServer.cs プロジェクト: dvmorozov/sgs
        // Client has connected
        private void OnConnectRequest(IAsyncResult ar)
        {
            // Get the listener and client
            Socket listener = (Socket)ar.AsyncState;
            Socket client   = listener.EndAccept(ar);

            lock (this)
            {
                if (!m_bShuttingDown)
                {
                    // Wrap the client and add it to the array
                    SockWrapper s = new SockWrapper(client);
                    m_aryClients.Add(s);

                    // Fire the Connected event
                    if (Connected != null)
                    {
                        Connected(this, ref s.obj);
                    }

                    // Set up an async wait for packets from the client
                    AsyncCallback receiveData = new AsyncCallback(OnReceivedData);
                    s.Client.BeginReceive(s.byBuff, 0, s.byBuff.Length, SocketFlags.None, receiveData, s);

                    // (Re)Setup a callback to be notified of connection requests
                    listener.BeginAccept(new AsyncCallback(OnConnectRequest), listener);
                }
                else
                {
                    // If we are in shutdown mode, DON'T add
                    // the connection to the array, DON'T setup
                    // the async listen, and DO set the event
                    // to say we are done.
                    ShutDownReady.Set();
                }
            }
        }
コード例 #4
0
ファイル: TcpServer.cs プロジェクト: dvmorozov/sgs
        private void _SendOne(SockWrapper s, byte [] b, int iLength)
        {
            try
            {
                bool bSend = true;

                if (Send != null)
                {
                    Send(this, ref s.obj, ref bSend);
                }

                if (bSend)
                {
                    s.Client.Send(b, iLength, SocketFlags.None);
                }
            }
            catch
            {
                // Ignore the error.  If the client is dead, OnReceiveData
                // will be called to close the connection.  I would remove it
                // anyway, except bad things happen if you remove an entry
                // from a list while using foreach.
            }
        }
コード例 #5
0
        private void _SendOne(SockWrapper s, byte [] b, int iLength)
        {
            try
            {
                bool bSend = true;

                if (Send != null)
                    Send(this, ref s.obj, ref bSend);

                if (bSend)
                    s.Client.Send(b, iLength, SocketFlags.None);
            }
            catch
            {
                // Ignore the error.  If the client is dead, OnReceiveData
                // will be called to close the connection.  I would remove it
                // anyway, except bad things happen if you remove an entry
                // from a list while using foreach.
            }
        }
コード例 #6
0
        // Remove a connection from the list of active connections
        private void RemoveConnection(SockWrapper s)
        {
            try
            {
                s.Client.Shutdown( SocketShutdown.Both );
                s.Client.Close();
            }
            catch {}

            // Remove it from the array
            try
            {
                m_aryClients.Remove( s );
            }
            catch {}

            // Fire the Disconnected event
            if (Disconnected != null)
                Disconnected(this, ref s.obj);
        }
コード例 #7
0
        // Client has connected
        private void OnConnectRequest( IAsyncResult ar )
        {
            // Get the listener and client
            Socket listener = (Socket)ar.AsyncState;
            Socket client = listener.EndAccept( ar );

            lock (this)
            {
                if (!m_bShuttingDown)
                {
                    // Wrap the client and add it to the array
                    SockWrapper s = new SockWrapper(client);
                    m_aryClients.Add( s );

                    // Fire the Connected event
                    if (Connected != null)
                        Connected(this, ref s.obj);

                    // Set up an async wait for packets from the client
                    AsyncCallback receiveData = new AsyncCallback( OnReceivedData );
                    s.Client.BeginReceive( s.byBuff, 0, s.byBuff.Length, SocketFlags.None, receiveData, s );

                    // (Re)Setup a callback to be notified of connection requests
                    listener.BeginAccept(new AsyncCallback( OnConnectRequest ) , listener );
                }
                else
                {
                    // If we are in shutdown mode, DON'T add
                    // the connection to the array, DON'T setup
                    // the async listen, and DO set the event
                    // to say we are done.
                    ShutDownReady.Set();
                }
            }
        }