示例#1
0
    ShortNetPack ParseMsgPack(byte[] data)
    {
        if (data == null || data.Length < 8)
        {
            return(null);
        }

        System.IO.MemoryStream stream       = new System.IO.MemoryStream(data);
        System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(stream);
        int packLen   = (int)binaryReader.ReadUInt32();
        int headLen   = (int)binaryReader.ReadUInt32();
        int nobodyLen = 8 + headLen;
        int bodyLen   = data.Length - nobodyLen;

        if (bodyLen < 0)
        {
            return(null);
        }

        stream.Position = nobodyLen;
        byte[]       bodyData = binaryReader.ReadBytes(bodyLen);
        ShortNetPack pack     = new ShortNetPack(bodyData.Length);

        pack.Write(bodyData, 0, bodyData.Length);
        return(pack);
    }
示例#2
0
    byte[] BuildMsgPack(ShortNetPack sendPack)
    {
        System.IO.MemoryStream stream       = new System.IO.MemoryStream();
        System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(stream);

        // 创建包头
        protocol.Header header = new protocol.Header();
        header.seq           = ProtocolDGlUtility.GetNextSequeue();
        header.sessionid     = LoginMgr.Instance.Roleid.ToString();
        header.roleid        = LoginMgr.Instance.Roleid;
        header.uid           = LoginMgr.Instance.s_uid;
        header.msg_full_name = sendPack.Name;

        // 写入包头
        stream.Position = sizeof(uint) * 2;
        //ProtoBuf.Serializer.Serialize(stream, header);
        uint headerLength = (uint)(stream.Length - sizeof(uint) * 2);

        // 写入包体
        binaryWriter.Write(sendPack.GetBytes());
        uint totalLength = (uint)stream.Length;

        // 写入长度
        stream.Position = 0;
        binaryWriter.Write(totalLength);
        binaryWriter.Write(headerLength);
        binaryWriter.Flush();

        byte[] result = stream.ToArray();
        return(result);
    }
示例#3
0
    public static void CreateSesion(ShortNetPack sendPack, Action <ShortNetPack> callback)
    {
        if (m_parentGameObject == null)
        {
            m_parentGameObject = new GameObject("ShortConnectMgr");
            GameObject.DontDestroyOnLoad(m_parentGameObject);
        }

        string     sesionName = "Sesion" + (m_parentGameObject.transform.childCount + 1).ToString();
        GameObject sesionGo   = new GameObject(sesionName);

        sesionGo.transform.parent = m_parentGameObject.transform;

        ShortConnect connect = sesionGo.AddComponent <ShortConnect>();

        byte[] msgData = connect.BuildMsgPack(sendPack);
        connect.StartConnect(msgData, callback);
    }
示例#4
0
    void OnRecvCallBack(IAsyncResult ar)
    {
        try
        {
            int nRecvLength = m_Socket.EndReceive(ar);
            if (nRecvLength == 0)
            {
                return;
            }

            if (m_NetPack == null)
            {
                int nHeadSize = sizeof(uint) * 1;
                if (nRecvLength < nHeadSize)
                {
                    m_Socket.BeginReceive(m_RecvBuffer, nRecvLength, m_RecvBuffer.Length, 0, new AsyncCallback(OnRecvCallBack), 0);
                    return;
                }

                System.IO.MemoryStream stream       = new System.IO.MemoryStream(m_RecvBuffer, 0, nHeadSize);
                System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(stream);
                int totalLength = binaryReader.ReadInt32();
                m_NetPack = new ShortNetPack(totalLength);
                stream.Dispose();
                binaryReader.Close();
                binaryReader = null;
                stream       = null;
            }

            m_NetPack.Write(m_RecvBuffer, 0, nRecvLength);

            if (m_NetPack.IsFull() == false)
            {
                m_Socket.BeginReceive(m_RecvBuffer, 0, m_RecvBuffer.Length, 0, new AsyncCallback(OnRecvCallBack), 0);
            }
        }
        catch (System.Net.Sockets.SocketException ex)
        {
            // SetErrorState(ex.SocketErrorCode);
        }
    }
示例#5
0
    void Shutdown()
    {
        try
        {
            if (m_Socket != null)
            {
                m_Socket.Shutdown(SocketShutdown.Both);
                m_Socket.Close();
            }
            m_Socket = null;
        }
        catch (System.Net.Sockets.SocketException)
        {
            m_Socket = null;
        }

        m_SendBuffer = null;
        m_RecvBuffer = null;
        m_NetPack    = null;

        Destroy(gameObject);
    }
示例#6
0
    void Update()
    {
        if (m_NetPack != null && m_NetPack.IsFull())
        {
            try
            {
                ShortNetPack pack = ParseMsgPack(m_NetPack.GetBytes());
                m_RspCallback(pack);
                pack = null;
            }
            finally
            {
                m_NetPack = null;
                Shutdown();
            }
            return;
        }

        if ((m_timeOut -= Time.deltaTime) <= 0)
        {
            Shutdown();
        }
    }