public static void SendMsg(this SyncSession session, string key, Dictionary <string, object> data)
        {
            if (session == null)
            {
                //Debug.LogError("Session 已经断开连接!");
                return;
            }

            ByteArray ba = new ByteArray();

            List <byte> message = GetSendByte(key, data);

            int len    = 3 + message.Count;
            int method = GetMethodIndex(key);

            ba.WriteShort(len);
            ba.WriteByte((byte)(method / 100));
            ba.WriteShort(method);

            if (message != null)
            {
                ba.bytes.AddRange(message);
            }
            else
            {
                ba.WriteInt(0);
            }

            byte[] buffer = ba.Buffer;

            //Debug.Log("消息头 " + method + " 消息名 " + key + " --> " + BitConverter.ToString(buffer));

            try
            {
                int  time   = ServiceTime.GetServiceTime();
                bool result = session.TrySend(buffer, 0, buffer.Length);
                if (!result)
                {
                    session.Close();
                }

                if (ServiceTime.GetServiceTime() - time > 10)
                {
                    Debug.Log("发送时间 " + (ServiceTime.GetServiceTime() - time));
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Send Messge Exception " + e.ToString());
            }
        }
Beispiel #2
0
        public static void Send(this SyncSession session, ProtocolRequestBase msg)
        {
            ByteArray ba = new ByteArray();

            ba.bytes.AddRange(GetSendByte(msg.Key, msg.m_data));

            byte[] buffer = ba.Buffer;

            bool result = session.TrySend(buffer, 0, buffer.Length);

            if (!result)
            {
                session.Close();
            }
        }