コード例 #1
0
    public byte[] GetLuaProtocolBytes(ReqLuaMessage protocol)
    {
        try
        {
            var bytes = new List <byte>();
            var props = propsJsonDict[protocol.ID];
            var data  = JObject.Parse(protocol.Data);
            for (int i = 1; i < props.Table.Length; i++)
            {
                bytes.AddRange(SerializeJsonTools.GetObjectBytes(data, props.Table[i]));
            }
            var result = bytes.ToArray();

            /**
             * 暂时先不用吧
             */
            // if (props.NeedCompress)
            // {
            //     result = CompressionHelper.ZipCompress(result);
            // }
            return(result);
        }
        catch (Exception e)
        {
            UnityEngine.Debug.Log("slf GetLuaProtocolBytes ID:" + protocol.ID + " e:" + e.ToString());
            return(null);
        }
    }
コード例 #2
0
    public void SendLuaProto(ushort ID, string Data)
    {
        if (!CheckConnect())
        {
            return;
        }
        ReqLuaMessage msg = new ReqLuaMessage();

        msg.ID   = ID;
        msg.Data = Data;
        TcpClient.SendMessage(msg);
    }
コード例 #3
0
 public DGTcpClient(string ip, int port, LuaFunction onConnectFunc, LuaFunction onMessageFunc, LuaFunction onDisconnectFunc, LuaFunction setServerTimeFunc, bool needHeartBeat = true, ushort heartBeatProtoID = 5, ushort rHeartBeatProtoID = 6, float interval = 5f, float timeout = 15f, bool needLuaHeartbeatCallback = false, int threadSleepTime = 50, string tag = "DGTcpClient")
 {
     _ip                       = ip;
     _port                     = port;
     _onConnectFunc            = onConnectFunc;
     _onMessageFunc            = onMessageFunc;
     _onDisconnectFunc         = onDisconnectFunc;
     _setServerTimeFun         = setServerTimeFunc;
     _needHeartBeat            = needHeartBeat;
     _heartBeatInterval        = interval;
     _heartBeatTimeOut         = timeout;
     _rltHeartBeatProtoID      = rHeartBeatProtoID;
     _tag                      = tag;
     heartBeatProto            = new ReqLuaMessage();
     heartBeatProto.ID         = heartBeatProtoID;
     heartBeatProto.Data       = "{\"ID\":" + heartBeatProtoID + "}";
     _needLuaHeartbeatCallback = needLuaHeartbeatCallback;
     _threadSleepTime          = threadSleepTime;
 }
コード例 #4
0
    public static byte[] GetObjectBytes(object data, ProtoDataType table, bool isJObject = true)
    {
        string name = table.Name;
        int    type = table.Type;
        object obj  = data;

        if (isJObject)
        {
            obj = (data as JObject)[name].Value <object>();
        }

        if (type == (int)TypeCode.Int32)
        {
            return(BitConverter.GetBytes(Convert.ToInt32(obj)));
        }
        else if (type == (int)TypeCode.UInt32)
        {
            return(BitConverter.GetBytes(Convert.ToUInt32(obj)));
        }
        else if (type == (int)TypeCode.UInt16)
        {
            return(BitConverter.GetBytes(Convert.ToUInt16(obj)));
        }
        else if (type == (int)TypeCode.Int16)
        {
            return(BitConverter.GetBytes(Convert.ToInt16(obj)));
        }
        else if (type == (int)TypeCode.Byte)
        {
            return(new byte[] { Convert.ToByte(obj) });
//                return BitConverter.GetBytes(Convert.ToByte(obj));
        }
        else if (type == (int)TypeCode.SByte)
        {
            return(new byte[] { (byte)Convert.ToSByte(obj) });
        }
        else if (type == (int)TypeCode.Single)
        {
            return(BitConverter.GetBytes(Convert.ToSingle(obj)));
        }
        else if (type == (int)TypeCode.Boolean)
        {
            return(BitConverter.GetBytes(Convert.ToBoolean(obj)));
        }
        else if (type == (int)TypeCode.Int64)
        {
            return(BitConverter.GetBytes(Convert.ToInt64(obj)));
        }
        else if (type == (int)TypeCode.String)
        {
            var str  = Convert.ToString(obj);
            var temp = UTFEncoding.GetBytes(str);
            var ret  = new byte[2 + temp.Length];
            Buffer.BlockCopy(BitConverter.GetBytes((ushort)temp.Length), 0, ret, 0, 2);
            Buffer.BlockCopy(temp, 0, ret, 2, temp.Length);
            return(ret);
        }
        else if (type == TypeCodeList1)
        {
            var arr    = obj as JArray;
            var table1 = table.Table[0];
            var bytes  = new List <byte>();
            // var table1 = table[KeyTable].Value<JArray>()[0].Value<JObject>();
            if (arr == null)
            {
                bytes.AddRange(BitConverter.GetBytes((ushort)0));
            }
            else
            {
                bytes.AddRange(BitConverter.GetBytes((ushort)arr.Count));
                for (int i = 0; i < arr.Count; i++)
                {
                    bytes.AddRange(GetObjectBytes(arr[i].Value <object>(), table1, false));
                }
            }
            return(bytes.ToArray());
        }
        else if (type == TypeCodeList2)
        {
            var arr = obj as JArray;
            // var table1 = table[KeyTable].Value<JArray>();
            var table1 = table.Table;
            var bytes  = new List <byte>();
            if (arr == null)
            {
                bytes.AddRange(BitConverter.GetBytes((ushort)0));
            }
            else
            {
                bytes.AddRange(BitConverter.GetBytes((ushort)arr.Count));
                for (int i = 0; i < arr.Count; i++)
                {
                    for (int j = 0; j < table1.Length; j++)
                    {
                        bytes.AddRange(GetObjectBytes(arr[i].Value <JObject>(), table1[j]));
                    }
                }
            }
            return(bytes.ToArray());
        }
        else if (type == TypeCodeList3)
        {
            var arr       = obj as JArray;
            var enumTable = table.Table[0];
            var bytes     = new List <byte>();
            if (arr == null)
            {
                bytes.AddRange(BitConverter.GetBytes((ushort)0));
            }
            else
            {
                bytes.AddRange(BitConverter.GetBytes((ushort)arr.Count));
                for (int i = 0; i < arr.Count; i++)
                {
                    var enumValue = arr[i][enumTable.Name].Value <int>();
                    var enumIndex = enumTable.EnumList.IndexOf(enumValue);
                    if (enumIndex == -1)
                    {
                        continue;
                    }
                    if (enumTable.Type == (int)TypeCode.Byte)
                    {
                        bytes.Add((byte)enumValue);
                    }
                    else if (enumTable.Type == (int)TypeCode.Int16)
                    {
                        bytes.AddRange(BitConverter.GetBytes((short)enumValue));
                    }

                    var table2 = table.Table[enumIndex + 1];
                    var item   = arr[i][table2.Name].Value <JObject>();
                    for (int j = 0; j < table2.Table.Length; j++)
                    {
                        bytes.AddRange(GetObjectBytes(item, table2.Table[j]));
                    }
                }
            }
            return(bytes.ToArray());
        }
        else if (type == TypeProtocolList)
        {
            var arr   = obj as JArray;
            var bytes = new List <byte>();
            bytes.AddRange(BitConverter.GetBytes((ushort)arr.Count));
            for (int i = 0; i < arr.Count; i++)
            {
                JObject       protoItem = arr[i] as JObject;
                ReqLuaMessage msg       = new ReqLuaMessage();
                msg.ID   = (ushort)protoItem["ID"];
                msg.Data = (string)protoItem["Data"];
                byte[] protoBytes = ProtocolFactory.Instance.GetBytes(msg);
                bytes.AddRange(protoBytes);
            }
            return(bytes.ToArray());
        }
        return(null);
    }