示例#1
0
    public void SendProto(ProtoBase_C2S protoData)
    {
        List<byte> protoBytes = protoData.Encode(); //获得协议体的字节

        byte[] headerBytes = BitConverter.GetBytes(protoBytes.Count); //获取协议头来标记协议长度
        Debug.Log("header value  "+ BitConverter.ToInt32(headerBytes,0));
        Debug.Log("Header bytes " + ByteArrayToStr(headerBytes));
        //在协议最前面写入协议的长度
        protoBytes.InsertRange(0, headerBytes);
        byte[] finalBytes = protoBytes.ToArray();
        Debug.Log("after header added");
        PrintBytes(finalBytes);
        int totalBytes = finalBytes.Length;
        int sendBytes = 0;
        while (sendBytes <totalBytes)
        {
            int sendCount = mSocket.Send(finalBytes, sendBytes, totalBytes - sendBytes, SocketFlags.None);
            sendBytes += sendCount;
        }
    }
示例#2
0
    public void SendProto(ProtoBase_C2S protoData)
    {
        List <byte> protoBytes = protoData.Encode();                  //获得协议体的字节

        byte[] headerBytes = BitConverter.GetBytes(protoBytes.Count); //获取协议头来标记协议长度
        Debug.Log("header value  " + BitConverter.ToInt32(headerBytes, 0));
        Debug.Log("Header bytes " + ByteArrayToStr(headerBytes));
        //在协议最前面写入协议的长度
        protoBytes.InsertRange(0, headerBytes);
        byte[] finalBytes = protoBytes.ToArray();
        Debug.Log("after header added");
        PrintBytes(finalBytes);
        int totalBytes = finalBytes.Length;
        int sendBytes  = 0;

        while (sendBytes < totalBytes)
        {
            int sendCount = mSocket.Send(finalBytes, sendBytes, totalBytes - sendBytes, SocketFlags.None);
            sendBytes += sendCount;
        }
    }