示例#1
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = e.UserToken as AsyncUserToken;

            // close the socket associated with the client
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception) { }
            token.Socket.Close();

            release(e);
        }
示例#2
0
 // This method is invoked when an asynchronous send operation completes.
 // The method issues another receive on the socket to read any additional
 // data sent from the client
 //
 // <param name="e"></param>
 private void ProcessSend(SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         // done echoing data back to the client
         AsyncUserToken token = (AsyncUserToken)e.UserToken;
         // read the next block of data send from the client
         e.SetBuffer(e.Offset, m_receiveBufferSize);
         bool willRaiseEvent = token.Socket.ReceiveAsync(e);
         if (!willRaiseEvent)
         {
             ProcessReceive(e);
         }
     }
     else
     {
         CloseClientSocket(e);
     }
 }
示例#3
0
        // This method is invoked when an asynchronous receive operation completes.
        // If the remote host closed the connection, then the socket is closed.
        // If data was received then the data is echoed back to the client.
        //
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = (AsyncUserToken)e.UserToken;

            _logger.LogDebug($"[ProcessReceive]: {_localEndPoint.Port} | {token.RequestId}");
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                bool needRecive = false;
                var  words      = e.Buffer.GetString(e.Offset, e.BytesTransferred);
                var  sum        = token.MassgeTemp + words;

                // 只有http请求需要对已发送字节进行存储
                if (m_isHttpServer)
                {
                    if (token.Recived != null)
                    {
                        byte[] resArr = new byte[token.Recived.Length + e.BytesTransferred];
                        token.Recived.CopyTo(resArr, 0);
                        Array.Copy(e.Buffer, e.Offset, resArr, token.Recived.Length, e.BytesTransferred);
                        token.Recived = resArr;
                    }
                    else
                    {
                        byte[] resArr = new byte[e.BytesTransferred];
                        Array.Copy(e.Buffer, e.Offset, resArr, 0, e.BytesTransferred);
                        token.Recived = resArr;
                    }
                }

                if (sum.Contains(m_sectionFlag))
                {
                    var array = (sum).Split(m_sectionFlag);
                    token.MassgeTemp = null;
                    var fullMsg = words.EndsWith(m_sectionFlag);

                    if (!fullMsg)
                    {
                        token.MassgeTemp = array[array.Length - 1];
                    }

                    for (int i = 0; i < array.Length - 1; i++)
                    {
                        needRecive = m_handller(token, array[i]);
                        if (needRecive)
                        {
                            continue;
                        }
                        else
                        {
                            // ÊÍ·Å×ÊÔ´
                            release(e);
                            return;
                        }
                    }
                }
                else
                {
                    token.MassgeTemp = sum;
                }

                bool willRaiseEvent = token.Socket.ReceiveAsync(e);
                if (!willRaiseEvent)
                {
                    ProcessReceive(e);
                }
            }
            else
            {
                CloseClientSocket(e);
            }
        }