コード例 #1
0
        private SkynetClusterRequest UnpackLargeRequestPacket(RequestPacketTag tag, byte[] data)
        {
            SkynetClusterRequest request = null;

            switch (tag)
            {
            case RequestPacketTag.LARGE_PACKET:
            case RequestPacketTag.LARGE_PUSH:
            {
                // unpack header
                byte   tempTag    = 0;
                string name       = "";
                int    nameLength = 0;
                int    session    = 0;
                UnpackHeader(data, out tempTag, out name, out nameLength, out session);

                if (m_largeRequestPackets.ContainsKey(session))
                {
                    LoggerHelper.Info(m_sourceId, String.Format("SkynetPacketManager.UnpackLargePacket multi header for same session {0}", session));
                    m_largeRequestPackets.Remove(session);
                }

                int startIndex    = nameLength + 6;
                int totalDataSize = data[startIndex + 3] << 24 | data[startIndex + 2] << 16 | data[startIndex + 1] << 8 | data[startIndex];

                SkynetLargePacket largeRequest = new SkynetLargePacket();
                largeRequest.ServiceName   = name;
                largeRequest.Session       = session;
                largeRequest.Tag           = (int)tempTag;
                largeRequest.TotalDataSize = totalDataSize;
                largeRequest.Messages      = new List <SkynetMessage>();
                largeRequest.Type          = LargePacketType.REQUEST;

                m_largeRequestPackets.Add(session, largeRequest);
            } break;

            case RequestPacketTag.FRAGMENT:
            case RequestPacketTag.LAST_FRAGMENT:
            {
                int session = data[4] << 24 | data[3] << 16 | data[2] << 8 | data[1];

                int    size = data.Length - 5;
                byte[] msg  = new byte[size];
                Array.Copy(data, 5, msg, 0, size);

                SkynetMessage skynetMessage = new SkynetMessage();
                skynetMessage.Data = msg;
                skynetMessage.Size = size;

                SkynetLargePacket largeRequest = null;
                bool isSuccess = m_largeRequestPackets.TryGetValue(session, out largeRequest);
                if (!isSuccess)
                {
                    LoggerHelper.Info(m_sourceId, String.Format("SkynetPacketManager.UnpackLargePacket illegal FRAGMENT {0}", session));
                    return(null);
                }
                largeRequest.Messages.Add(skynetMessage);

                if (tag == RequestPacketTag.LAST_FRAGMENT)
                {
                    byte[] messageBuf = new byte[largeRequest.TotalDataSize];
                    int    startIndex = 0;
                    int    sumBytes   = 0;
                    for (int i = 0; i < largeRequest.Messages.Count; i++)
                    {
                        SkynetMessage tempMsg = largeRequest.Messages[i];
                        Array.Copy(tempMsg.Data, 0, messageBuf, startIndex, tempMsg.Size);
                        startIndex += tempMsg.Size;
                        sumBytes   += tempMsg.Size;
                    }

                    if (largeRequest.TotalDataSize != sumBytes)
                    {
                        LoggerHelper.Info(m_sourceId, String.Format("SkynetPacketManager.UnpackLargePacket large packet totalsize is not equal to real size {0}", session));
                    }

                    // unpack message: protoId and message buffer
                    startIndex = 0;
                    int byteCount = 0;
                    int protoId   = (int)UnpackInteger(messageBuf, startIndex, out byteCount);
                    startIndex += 1 + byteCount;

                    byte[] unpackMsg = UnpackString(messageBuf, startIndex);

                    request             = new SkynetClusterRequest();
                    request.ProtoId     = protoId;
                    request.ServiceName = largeRequest.ServiceName;
                    request.Session     = session;
                    request.Data        = unpackMsg;

                    m_largeRequestPackets.Remove(session);
                }
            } break;

            default: break;
            }

            return(request);
        }
コード例 #2
0
        public SkynetClusterResponse UnpackSkynetResponse(byte[] msg)
        {
            SkynetClusterResponse response = null;

            int startIndex = 0;
            int session    = msg[0] | msg[1] << 8 | msg[2] << 16 | msg[3] << 24;

            startIndex += 4;

            int tag = msg[startIndex];

            startIndex++;

            switch ((ResponsePacketTag)tag)
            {
            case ResponsePacketTag.OK:
            {
                response = new SkynetClusterResponse();

                int byteCount = 0;
                int protoId   = (int)UnpackInteger(msg, startIndex, out byteCount);
                startIndex += 1 + byteCount;
                byte[] tempData = UnpackString(msg, startIndex);

                response.ErrorCode = RPCError.OK;
                response.ProtoId   = protoId;
                response.Data      = tempData;
            }
            break;

            case ResponsePacketTag.ERROR:
            {
                response = new SkynetClusterResponse();

                response.ErrorCode = RPCError.RemoteError;
                response.ProtoId   = 0;
                response.Data      = UnpackString(msg, startIndex);
            } break;

            case ResponsePacketTag.MULTI_BEGIN:
            {
                SkynetLargePacket largePacket = null;
                bool isExist = m_largeResponsePackets.TryGetValue(session, out largePacket);
                if (!isExist)
                {
                    largePacket = new SkynetLargePacket();
                    m_largeResponsePackets.Add(session, largePacket);
                }

                largePacket.Tag     = tag;
                largePacket.Type    = LargePacketType.RESPONSE;
                largePacket.Session = session;

                int byteCount = 0;
                largePacket.TotalDataSize = (int)UnpackInteger(msg, startIndex, out byteCount);
                largePacket.Messages      = new List <SkynetMessage>();
            } break;

            case ResponsePacketTag.MULTI_PART:
            {
                SkynetLargePacket largePacket = null;
                bool isExist = m_largeResponsePackets.TryGetValue(session, out largePacket);
                if (isExist)
                {
                    SkynetMessage skynetMessage = new SkynetMessage();
                    skynetMessage.Size = msg.Length - 5;
                    skynetMessage.Data = UnpackString(msg, startIndex);

                    largePacket.Messages.Add(skynetMessage);
                }
            } break;

            case ResponsePacketTag.MULTI_END:
            {
                SkynetLargePacket largePacket = null;
                bool isExist = m_largeResponsePackets.TryGetValue(session, out largePacket);
                if (isExist)
                {
                    response = new SkynetClusterResponse();

                    byte[] tempData       = new byte[largePacket.TotalDataSize];
                    int    tempStartIndex = 0;
                    int    count          = largePacket.Messages.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SkynetMessage skynetMessage = largePacket.Messages[i];
                        Array.Copy(skynetMessage.Data, 0, tempData, tempStartIndex, skynetMessage.Size);
                        tempStartIndex += skynetMessage.Size;
                    }

                    Array.Copy(msg, 5, tempData, tempStartIndex, msg.Length - 5);

                    int byteCount = 0;
                    response.ErrorCode = RPCError.OK;
                    response.ProtoId   = (int)UnpackInteger(tempData, 0, out byteCount);
                    response.Data      = UnpackString(tempData, byteCount);
                }
            } break;

            default: break;
            }

            if (response != null)
            {
                response.Session = session;
            }

            return(response);
        }