コード例 #1
0
        public static void SendBytes(ushort protoID, byte[] message, ushort rpcId = 0)
        {
            try
            {
                MemoryStream ms            = null;
                int          messageLength = message == null ? 2 : message.Length + 2;
                using (ms = new MemoryStream())
                {
                    ms.Position = 0;
                    BinaryWriter writer = new BinaryWriter(ms);
                    if (rpcId > 0)
                    {
                        ushort msglen = BytesUtility.SwapUInt16((ushort)(messageLength + 2));
                        writer.Write(msglen);
                        ushort protoid = BytesUtility.SwapUInt16(protoID);
                        writer.Write(protoid);
                        ushort rpcid = BytesUtility.SwapUInt16(rpcId);
                        writer.Write(rpcid);
                    }
                    else
                    {
                        ushort msglen = BytesUtility.SwapUInt16((ushort)messageLength);
                        writer.Write(msglen);
                        ushort protoid = BytesUtility.SwapUInt16(protoID);
                        writer.Write(protoid);
                    }

                    if (message != null)
                    {
                        writer.Write(message);
                    }
                    writer.Flush();
                    if (client != null && client.Connected)
                    {
                        byte[] payload = ms.ToArray();
                        outStream.BeginWrite(payload, 0, payload.Length, new AsyncCallback(OnWrite), null);
                    }
                    else
                    {
                        // Debug.LogError("client.connected----->>false");
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
        }
コード例 #2
0
 public static void OnReceive(byte[] bytes, int length)
 {
     memStream.Seek(0, SeekOrigin.End);
     memStream.Write(bytes, 0, length);
     memStream.Seek(0, SeekOrigin.Begin);
     while (RemainingBytes() >= 4)
     {
         ushort messageLen = reader.ReadUInt16();
         messageLen = (ushort)(BytesUtility.SwapUInt16(messageLen) - 2);
         ushort protoId = reader.ReadUInt16();
         protoId = BytesUtility.SwapUInt16(protoId);
         ushort rpcId = 0;
         if (protoId > 20000)
         {
             rpcId      = reader.ReadUInt16();
             rpcId      = BytesUtility.SwapUInt16(rpcId);
             messageLen = (ushort)(messageLen - 2);
         }
         if (RemainingBytes() >= messageLen)
         {
             byte[]          bytes1 = reader.ReadBytes(messageLen);
             Action <byte[]> callback;
             if (m_ListenerMap.TryGetValue(protoId, out callback))
             {
                 callback.Invoke(bytes1);
             }
             else
             {
                 OnReceivedMessage(protoId, rpcId, bytes1);
             }
             //Debug.LogError($"{RemainingBytes()}:{messageLen}");
         }
         else
         {
             memStream.Position = memStream.Position;
             break;
         }
     }
     byte[] leftover = reader.ReadBytes((int)RemainingBytes());
     memStream.SetLength(0);
     memStream.Write(leftover, 0, leftover.Length);
 }