Beispiel #1
0
    //将大缓冲的字节转移到此包
    public void Swap()
    {
        m_ReadBytes = PooledClassManager <DataBytes> .CreateClass();

        m_ReadBytes.SetBytes(m_Offset);
        Array.Copy(s_WriteBytes, 0, m_ReadBytes.GetBytes(), 0, m_Offset);
    }
Beispiel #2
0
    private string RecvAll(ref HeaderBytes msgHeader, ref DataBytes msgData, ref int nRecved, ref bool bWaiting)
    {
        string result;

        if (msgData == null)
        {
            SocketError socketError;
            int         recveNum = m_Socket.Receive(msgHeader.Bytes, nRecved, msgHeader.Bytes.Length - nRecved, SocketFlags.None, out socketError);
            if (recveNum < 0)
            {
                result = "消息头小于0";
                return(result);
            }
            if (socketError != SocketError.Success && socketError != SocketError.WouldBlock)
            {
                result             = "网络错误:" + socketError.ToString();
                m_ConnectState     = EConnectState.PostConnecting;
                m_bNetStateChanged = true;
                //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                return(result);
            }
            if (recveNum == 0)
            {
                bWaiting = true;
                result   = null;
                return(result);
            }
            nRecved += recveNum;
            if (nRecved == msgHeader.Bytes.Length)
            {
                ushort msgType   = 0;
                ushort msgLength = 0;
                if (!WfPacket.ParseHeader(msgHeader.Bytes, ref msgType, ref msgLength))
                {
                    result = string.Concat(new object[]
                    {
                        "error ParseHeader type:",
                        msgType,
                        "len:",
                        msgLength
                    });
                    return(result);
                }
                if (msgLength < msgHeader.Bytes.Length)
                {
                    result = string.Concat(new object[]
                    {
                        "error ParseHeader < msglen:", msgLength, "headerLength:", msgHeader.Bytes.Length
                    });
                    return(result);
                }
                if (msgLength == msgHeader.Bytes.Length)
                {
                    //这里虽然只有一个是一个很简单的只带有消息头的消息,例如心跳,但是我依然用了完整消息大小
                    msgData = PooledClassManager <DataBytes> .CreateClass();

                    msgData.SetBytes(msgLength + 1);
                    Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length);
                    WfPacket item = PooledClassManager <WfPacket> .CreateClass();

                    item.InitRead(msgData);
                    msgData = null;
                    nRecved = 0;
                    object privateLockObject;
                    Monitor.Enter(privateLockObject = m_PrivateLockObject);
                    try
                    {
                        m_RecvQueue.Enqueue(item);
                    }
                    finally
                    {
                        Monitor.Exit(privateLockObject);
                    }
                }
                else
                {
                    msgData = PooledClassManager <DataBytes> .CreateClass();

                    msgData.SetBytes(msgLength);
                    Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length);
                    nRecved = msgHeader.Bytes.Length;
                }
            }
        }
        if (msgData != null)
        {
            SocketError socketError;
            int         recveNum = m_Socket.Receive(msgData.GetBytes(), nRecved, msgData.GetLength() - nRecved, SocketFlags.None, out socketError);
            //Debug.Log("底层函数接收数据:" + socketError.ToString());
            if (recveNum < 0)
            {
                result = "ReceiveData < 0";
                return(result);
            }
            if (socketError != SocketError.Success && socketError != SocketError.WouldBlock)
            {
                result = "ReceiveData Failed";
                return(result);
            }
            if (recveNum == 0)
            {
                bWaiting = true;
                result   = null;
                return(result);
            }
            nRecved += recveNum;
            if (nRecved > msgData.GetLength())
            {
                result = "ReceiveData IO error";
                return(result);
            }
            if (nRecved == msgData.GetLength())
            {
                WfPacket item = PooledClassManager <WfPacket> .CreateClass();

                item.InitRead(msgData);
                msgData = null;
                nRecved = 0;
                object privateLockObject;
                Monitor.Enter(privateLockObject = m_PrivateLockObject);
                try
                {
                    m_RecvQueue.Enqueue(item);
                }
                finally
                {
                    Monitor.Exit(privateLockObject);
                }
            }
            else
            {
                bWaiting = true;
            }
        }
        result = null;
        return(result);
    }
Beispiel #3
0
 public byte[] GetReadBytes()
 {
     return(m_ReadBytes.GetBytes());
 }