示例#1
0
 private void OnTCPRemoteClientConnectionEvent(TCPRemoteClient tTCPClient, TCP_CLIENT_STATUS tStatus)
 {
     //m_Invoker.BeginInvoke(TCPRemoteClientConnectionEvent, tTCPClient, tStatus);
     if (null == TCPRemoteClientConnectionEvent)
     {
         return;
     }
     TCPRemoteClientConnectionEvent.Invoke(tTCPClient, tStatus);
 }
示例#2
0
 private void OnTCPReport(Byte[] tStream, TCPRemoteClient tTCPClient)
 {
     if (null == TCPStreamReporterEvent)
     {
         return;
     }
     TCPStreamReporterEvent.Invoke(tStream, tTCPClient);
     //m_Invoker.BeginInvoke(TCPStreamReporterEvent, tStream, tTCPClient);
 }
示例#3
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            Socket tClientSocket = e.AcceptSocket;

            if (null == tClientSocket)
            {
                return;
            }
            //! on client connected
            do
            {
                IPEndPoint      tEndPoint     = (IPEndPoint)tClientSocket.RemoteEndPoint;
                TCPRemoteClient tRemoteClient = null;
#if false
                if (null == m_ClientSet.Find(tEndPoint.ToString()))
                {
                    tRemoteClient = new TCPRemoteClient(tEndPoint);
                    m_ClientSet.Add(tRemoteClient);
                }
                else
                {
                    tRemoteClient = m_ClientSet[tEndPoint.ToString()];
                }
#else
                tRemoteClient = m_ClientSet[tEndPoint.ToString()];
                if (null == tRemoteClient)
                {
                    return;
                }
#endif
                tRemoteClient.AsyncEventArgsIn.AcceptSocket = null;

                OnTCPRemoteClientConnectionEvent(tRemoteClient,
                                                 TCP_CLIENT_STATUS.TCP_CLIENT_DISCONNECTED);
            } while (false);

            // close the socket associated with the client
            try
            {
                tClientSocket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception) { }
            tClientSocket.Close();
        }
示例#4
0
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            // Check if the remote host closed the connection.
            if (e.BytesTransferred > 0)
            {
                if (e.SocketError == SocketError.Success)
                {
                    //! get data here
                    Socket tClientSocket = e.AcceptSocket;

                    if (null == tClientSocket)
                    {
                        return;
                    }

                    TCPRemoteClient tRemoteClient = null;
                    //! report data arrived
                    do
                    {
                        IPEndPoint tEndPoint = (IPEndPoint)tClientSocket.RemoteEndPoint;

                        tRemoteClient = m_ClientSet[tEndPoint.ToString()];
                        if (null == tRemoteClient)
                        {
                            return;
                        }
                        tRemoteClient.UpdateTime();

                        try
                        {
                            Byte[] tReportBuffer = new Byte[e.BytesTransferred];
                            Array.Copy(e.Buffer, tReportBuffer, e.BytesTransferred);

                            OnTCPReport(tReportBuffer, tRemoteClient);
                        }
                        catch (Exception)
                        {
                        }
                    } while (false);

#if false
                    if (tClientSocket.Available == 0)
                    {
                        // Set return buffer.
                        //! set output data here
                        Byte[]  tOutput  = tRemoteClient.FetchOutputStream();
                        Boolean bSending = false;
                        do
                        {
                            if (null == tOutput)
                            {
                                break;
                            }
                            else if (0 == tOutput.Length)
                            {
                                break;
                            }
                            e.SetBuffer(tOutput, 0, tOutput.Length);

                            bSending = true;
                            if (!tClientSocket.SendAsync(e))
                            {
                                // Set the buffer to send back to the client.
                                this.ProcessSend(e);
                            }
                        } while (false);

                        if (!bSending)
                        {
                            if (!tClientSocket.ReceiveAsync(e))
                            {
                                // Read the next block of data sent by client.
                                this.ProcessReceive(e);
                            }
                        }
                    }
                    else if (!tClientSocket.ReceiveAsync(e))
                    {
                        // Read the next block of data sent by client.
                        this.ProcessReceive(e);
                    }
#else
                    if (!tClientSocket.ReceiveAsync(e))
                    {
                        // Read the next block of data sent by client.
                        this.ProcessReceive(e);
                    }
#endif
                }
                else
                {
                    //this.ProcessError(e);
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
示例#5
0
        private void ProcessSend(SocketAsyncEventArgs e)
        {
#if false
            if (e.SocketError == SocketError.Success)
            {
                // Done echoing data back to the client.
                Socket tClientSocket = e.AcceptSocket as Socket;
                e.SetBuffer(new Byte[m_BufferSize], 0, m_BufferSize);

                if (!tClientSocket.ReceiveAsync(e))
                {
                    // Read the next block of data send from the client.
                    this.ProcessReceive(e);
                }
            }
            else
            {
                //this.ProcessError(e);
            }
#else
            TCPRemoteClient tRemoteClient = e.UserToken as TCPRemoteClient;
            Socket          tClientSocket = e.AcceptSocket;
            if (null == tClientSocket)
            {
                return;
            }
            if (null == tRemoteClient)
            {
                return;
            }

            Monitor.Enter(tRemoteClient);
            Byte[] tOutput = tRemoteClient.FetchOutputStream();
            do
            {
                if (null == tOutput)
                {
                    tRemoteClient.SendingIdle = true;
                    Monitor.Exit(tRemoteClient);
                    break;
                }
                else if (0 == tOutput.Length)
                {
                    tRemoteClient.SendingIdle = true;
                    Monitor.Exit(tRemoteClient);
                    break;
                }
                e.SetBuffer(tOutput, 0, tOutput.Length);
                tRemoteClient.SendingIdle = false;

                Monitor.Exit(tRemoteClient);

                try
                {
                    if (!tClientSocket.SendAsync(e))
                    {
                        // Set the buffer to send back to the client.
                        this.ProcessSend(e);
                    }
                }
                catch (Exception)
                {
                    tRemoteClient.SendingIdle = true;
                }
                finally
                {
                }
            } while (false);
#endif
        }
示例#6
0
        // Process the accept for the socket listener.
        private void ProcessAccept(Socket tSocket, SocketAsyncEventArgs e)
        {
            Socket tClientSocket = e.AcceptSocket;

            if (tClientSocket.Connected)
            {
                TCPRemoteClient tRemoteClient = null;
                //! on client connected
                do
                {
                    IPEndPoint tEndPoint = (IPEndPoint)tClientSocket.RemoteEndPoint;

                    if (null == m_ClientSet.Find(tEndPoint.ToString()))
                    {
                        tRemoteClient = new TCPRemoteClient(tEndPoint);
                        SocketAsyncEventArgs readEventArgs = tRemoteClient.AsyncEventArgsIn;
                        readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(ReadWriteEventArgs_Completed);
                        //readEventArgs.AcceptSocket = tClientSocket;
                        readEventArgs.SetBuffer(new Byte[m_BufferSize], 0, m_BufferSize);

                        SocketAsyncEventArgs writeEventArgs = tRemoteClient.AsyncEventArgsOut;
                        writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(ReadWriteEventArgs_Completed);
                        //writeEventArgs.AcceptSocket = tClientSocket;
                        tRemoteClient.FirstSendingRequestEvent += new TCPRemoteClient.TCPRemoteClientReport(tRemoteClient_FirstSendingRequestEvent);
                        writeEventArgs.UserToken = tRemoteClient;

                        m_ClientSet.Add(tRemoteClient);
                    }
                    else
                    {
                        tRemoteClient = m_ClientSet[tEndPoint.ToString()];
                    }
                    tRemoteClient.AsyncEventArgsIn.AcceptSocket  = tClientSocket;
                    tRemoteClient.AsyncEventArgsOut.AcceptSocket = tClientSocket;

                    this.ProcessSend(tRemoteClient.AsyncEventArgsOut);

                    OnTCPRemoteClientConnectionEvent(tRemoteClient,
                                                     TCP_CLIENT_STATUS.TCP_CLIENT_CONNECTED);
                } while (false);

                do
                {
                    try
                    {
                        // Get the socket for the accepted client connection and put it into the
                        // ReadEventArg object user token.

                        if (!tClientSocket.ReceiveAsync(tRemoteClient.AsyncEventArgsIn))
                        {
                            this.ProcessReceive(tRemoteClient.AsyncEventArgsIn);
                        }
                    }
                    catch (SocketException)
                    {
                    }
                    catch (Exception)
                    {
                    }
                } while (false);
            }


            // Accept the next connection request.
            this.StartAccept(tSocket, e);
        }