Exemplo n.º 1
0
 public static ByteArray encode(Message vo)
 {
     //result.endian = Endian.BIG_ENDIAN;
     ByteArray result = new ByteArray();
     result.writeByte(131);
     pack(vo, result, ProtoAliasUtil.getAliasByClassName(vo.getClassName()));
     if (isEncrypt == false)
     {
         return result;
     }
     if (encryptHandler != null)
     {
         return encryptHandler(result);
     }
     return result;
 }
Exemplo n.º 2
0
 private void send(Message message)
 {
     if (message == null)
     {
         return;
     }
     Debug.Log("发送协议 " + message.getClassName());
     int packetHeader;
     byte id;
     ByteArray dataByte = new ByteArray();
     //dataByte.position = 0;
     dataByte = Message.encode(message);
     if (dataByte.Length > 512)
     {
         dataByte.Compress();
         id = 1;
     }
     else
     {
         id = 0;
     }
     packetHeader = 1 + dataByte.Length;
     ByteArray sendByte = new ByteArray();
     sendByte.writeInt(packetHeader);
     sendByte.writeByte(id);
     sendByte.writeBytes(dataByte.Buffer, 0, dataByte.Length);
     byte[] sendByte2 = new byte[sendByte.Length];
     Array.Copy(sendByte.Buffer, 0, sendByte2, 0, sendByte.Length);
     //stateObj.workSocket.Send(sendByte2);
     //Debug.Log("SendDATA" + sendByte2.Length);
     string msg = "";
     for (int i = 0; i < sendByte2.Length; i++)
     {
         msg += sendByte2[i] + ",";
     }
     //Debug.Log(msg);
     stateObj.workSocket.BeginSend(sendByte2, 0, sendByte2.Length, SocketFlags.None, new AsyncCallback(SendCallback), stateObj.workSocket);
 }
Exemplo n.º 3
0
    public static byte[] pack(Message vo, ByteArray result, int alias = 0)
    {
        string[][] attrs = vo.getAttributes();
        int lenT = attrs.Length;
        int len = 0;
        int i = 0;
        int j = 0;
        string type;
        object voValue;
        if (lenT > 255)
        {
            result.writeByte(105);
            result.writeInt(lenT + 1);
        }
        else
        {
            result.writeByte(104);
            result.writeByte((byte)(lenT + 1));
        }
        if (alias > 0)
        {
            result.writeByte(98);
            result.writeInt(alias);
        }
        else
        {
            result.writeByte(100);
            result.writeUTF(vo.getClassName());
        }

        for (i = 0; i < lenT; i++)
        {

            type = attrs[i][1].ToString().ToLower();
            voValue = vo.getValue(attrs[i][0]);
            if (type == "int")
            {
                result.writeByte(98);
                result.writeInt((int)voValue);
            }
            else if (type == "double" || type == "number")
            {
                if ((double)voValue < 2147483647 && (double)voValue > -2147483647)
                {
                    result.writeByte(98);
                    int small = (int)((double)voValue);
                    result.writeInt(small);
                }
                else
                {
                    result.writeByte(110);
                    ByteArray bigB = new ByteArray();
                    float big = (float)voValue;
                    while (big > 1)
                    {
                        bigB.writeByte(Convert.ToByte((int)big & 0xFF));
                        big = big / 256;
                    }
                    result.writeByte(Convert.ToByte(bigB.Length));
                    if ((int)voValue > 0)
                    {
                        result.writeByte(0);
                    }
                    else
                    {
                        result.writeByte(1);
                    }
                    for (int bigL = 0; bigL <= bigB.Length - 1; bigL++)
                    {
                        result.writeByte(bigB.Buffer[bigL]);
                    }
                }
            }
            else if (type == "String" || type == "string")
            {
                result.writeByte(107);
                result.writeUTF((string)voValue);
            }
            else if (type == "bool" || type == "boolean")
            {
                result.writeByte(100);
                if ((bool)voValue)
                {
                    result.writeUTF("true");
                }
                else
                {
                    result.writeUTF("false");
                }
            }
            else if (type == "array")
            {
                if ((object[])voValue != null)
                {
                    len = ((object[])voValue).Length;
                    if (len > 0)
                    {
                        result.writeByte(108);
                        result.writeUnsignedInt((uint)len);
                        string subType = (string)attrs[i][2].ToLower();
                        for (j = 0; j < len; j++)
                        {
                            if (subType == "int")
                            {
                                result.writeByte(98); // 不做优化
                                result.writeInt((int)((object[])voValue)[j]);
                            }
                            else if (subType == "double" || subType == "number")
                            {
                                result.writeByte(110);
                                ByteArray bigB2 = new ByteArray();
                                uint big2 = (uint)((object[])voValue)[j];
                                while (big2 > 1)
                                {
                                    bigB2.writeByte(Convert.ToByte((int)big2 & 0xFF));
                                    big2 = big2 / 256;
                                }
                                result.writeByte(Convert.ToByte(bigB2.Length));
                                if ((int)((object[])voValue)[j] > 0)
                                {
                                    result.writeByte(0);
                                }
                                else
                                {
                                    result.writeByte(1);
                                }
                                for (int bigL2 = 0; bigL2 < bigB2.Length; bigL2++)
                                {
                                    result.writeByte(bigB2.Buffer[bigL2]);
                                }
                            }
                            else if (subType == "String" || type == "string")
                            {
                                result.writeByte(107);
                                result.writeUTF((string)((object[])voValue)[j]);
                            }
                            else if (subType == "bool" || subType == "Boolean")
                            {
                                result.writeByte(100);
                                if ((bool)((object[])voValue)[j])
                                {
                                    result.writeUTF("true");
                                }
                                else
                                {
                                    result.writeUTF("false");
                                }
                            }
                            else
                            {
                                pack((Message)((object[])voValue)[j], result);
                            }
                        }
                    }
                }
                result.writeByte(106);
            }
            else if (type == "bytes")
            {
                result.writeByte(109);
                byte[] tttByte = voValue as byte[];
                result.writeInt(tttByte.Length);
                result.writeBytes(tttByte, 0, tttByte.Length);
            }
            else
            {
                pack((Message)voValue, result);
            }
        }
        return result.Buffer;
    }