Ejemplo n.º 1
0
 private void InitStreamBuffer(ProtocolHeader pHeader)
 {
     m_protocolHeader            = pHeader;
     m_protocolHeader.BodyLength = m_iSize;
 }
Ejemplo n.º 2
0
        protected void ReceiveLoop()
        {
            while (m_SocketState == SocketState.Connected)
            {
                try
                {
                    int    recHeaderSize = 0;
                    byte[] headerStream  = new byte[ProtocolHeader.HeadLength];
                    do
                    {
                        int recCount = this.m_cSocket.Receive(headerStream, recHeaderSize, ProtocolHeader.HeadLength - recHeaderSize, SocketFlags.None);
                        recHeaderSize += recCount;

                        if (recCount == 0)
                        {
                            WriteFiles.WritFile.Log(LogerType.INFO, string.Format("session be closed by peer when receive header"));

                            throw new SocketException((int)SocketError.ConnectionReset);
                        }
                    } while (recHeaderSize < ProtocolHeader.HeadLength);

                    ProtocolHeader pHeader = new ProtocolHeader(headerStream);
                    if (pHeader.BodyLength > ProtocolHeader.MaxHeadLength || pHeader.BodyLength < 0)
                    {
                        WriteFiles.WritFile.Log(LogerType.WARN, string.Format("session be closed by peer when receive, bodyLenght error {0}", pHeader.BodyLength));
                        continue;
                    }
                    byte[] bodyStream = null;
                    if (pHeader.BodyLength > 0)
                    {
                        int recBodySize = 0;
                        bodyStream = new byte[pHeader.BodyLength];
                        do
                        {
                            int recCount = this.m_cSocket.Receive(bodyStream, recBodySize, pHeader.BodyLength - recBodySize, SocketFlags.None);
                            recBodySize += recCount;

                            if (recCount == 0)
                            {
                                WriteFiles.WritFile.Log(LogerType.INFO, string.Format("session be closed by peer when receive body"));
                                throw new SocketException((int)SocketError.ConnectionReset);
                            }
                        } while (recBodySize < pHeader.BodyLength);
                    }

                    StreamBuffer recSB = new StreamBuffer(pHeader, bodyStream);
                    m_cDispatch.AckPacket(recSB);
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.Interrupted)
                    {
                        WriteFiles.WritFile.Log(LogerType.INFO, string.Format("session be closed by peer when receive (interruped)"));
                        HandleException(SocketState.Disconnecting);
                    }
                    else
                    {
                        WriteFiles.WritFile.Log(e);
                        HandleException(SocketState.Disconnecting);
                    }
                }
                catch (Exception ex)
                {
                    WriteFiles.WritFile.Log(ex);
                    HandleException(SocketState.Disconnecting);
                }
            }
        }
Ejemplo n.º 3
0
 internal StreamBuffer(ProtocolHeader pHeader, byte[] buffer)
     : base(buffer)
 {
     InitStreamBuffer(pHeader);
 }