public void SendMessage(IMessage obj)
    {
        if (!MessageDefine.ContainProtoType(obj.GetType()))
        {
            AppDebug.Log("协议不存在:" + obj.GetType().ToString());
            return;
        }

        Message msg = new Message();

        //set header
        msg.Header = new Header();

        //set body
        {
            byte[] messageBody = obj.ToByteArray();
            byte[] body        = new byte[2 + messageBody.Length];
            using (AppMemoryStream ms = new AppMemoryStream())
            {
                int id = MessageDefine.GetProtoIdByProtoType(obj.GetType());
                ms.WriteUShort((ushort)(id));
                ms.Write(messageBody, 0, messageBody.Length);
                body = ms.ToArray();
            }
            msg.Body = ByteString.CopyFrom(body);
        }

        //发送消息
        int protoId = MessageDefine.GetProtoIdByProtoType(msg.GetType());

        SocketClient.Instance.SendMessage(protoId, msg.ToByteArray());
    }
    public static List <byte> GetSkateMessage(MessageDefine messageName)
    {
        List <byte> messageBufferList = new List <byte>()
        {
            (byte)messageName
        };

        return(messageBufferList);
    }
Example #3
0
    public override void Dispose()
    {
        m_Player = null;

        Event.Instance.RemoveListener("OnTcpConnected", OnTcpConnected);

        SocketEvent.Instance.RemoveListener((ushort)MessageDefine.GetProtoIdByProtoType(typeof(StcUserAuthentication)), OnStcAuthentication);

        base.Dispose();
    }
Example #4
0
    public void SendToGame(IMessage obj)
    {
        if (!MessageDefine.ContainProtoType(obj.GetType()))
        {
            AppDebug.Log("协议不存在:" + obj.GetType().ToString());

            return;
        }
        Header h = new Header();

        h.ServiceId0 = (int)SERVICE.G001;

        SendToService(h, obj);
    }
    private void OnRecvMessageBuffer(byte[] obj)
    {
        if (obj.Length > 0)
        {
            MessageDefine messageID   = (MessageDefine)obj[0];
            char[]        messageBody = null;
            if (obj.Length > 1)
            {
                messageBody = Encoding.ASCII.GetChars(obj.Skip(1).ToArray());
            }

            MessageHandler.Call((int)messageID, messageBody);
        }
    }
Example #6
0
    public static void DecodeMessage(byte[] data)
    {
        //解析收到的数据messge
        byte[] body = new byte[data.Length - 2];

        ushort protoCode = 0;

        using (AppMemoryStream ms = new AppMemoryStream(data))
        {
            protoCode = ms.ReadUShort();

            ms.Read(body, 0, body.Length);
        }

        Type protoType = MessageDefine.GetProtoTypeByProtoId(protoCode);

        MessageParser messageParser = MessageDefine.GetMessageParser(protoType.TypeHandle);

        object toc = messageParser.ParseFrom(body);

        Message msg = toc as Message;

        byte[] msgBody = msg.Body.ToByteArray();

        //解析messgeBody
        ushort msgId = 0;

        byte[] msgData = new byte[msgBody.Length - 2];

        using (AppMemoryStream ms1 = new AppMemoryStream(msgBody))
        {
            msgId = ms1.ReadUShort();

            ms1.Read(msgData, 0, msgData.Length);
        }

        {
            Type msgType = MessageDefine.GetProtoTypeByProtoId(msgId);

            MessageParser messageParser1 = MessageDefine.GetMessageParser(msgType.TypeHandle);

            object toc1 = messageParser1.ParseFrom(msgData);

            //派发事件
            SocketEvent.Instance.Dispatch(msgId, toc1);
        }
    }
Example #7
0
    private void SendToService(Header header, IMessage obj)
    {
        Player p = UserManager.Instance.GetPlayer();

        if (p.UserId == 0)
        {
            AppDebug.Log("无法获取玩家信息,请重新登陆!");

            return;
        }

        header.UserId = p.UserId;

        header.Token = p.Token;

        header.TokenExpiredTime = p.TokenExpireTime;

        Message msg = new Message();

        msg.Header = header;

        {//set body
            byte[] bodyData = obj.ToByteArray();

            byte[] body = new byte[2 + bodyData.Length];

            using (AppMemoryStream ms = new AppMemoryStream())
            {
                int id = MessageDefine.GetProtoIdByProtoType(obj.GetType());

                ms.WriteUShort((ushort)(id));

                ms.Write(bodyData, 0, bodyData.Length);

                body = ms.ToArray();
            }

            msg.Body = ByteString.CopyFrom(body);
        }

        //发送消息
        int protoId = MessageDefine.GetProtoIdByProtoType(msg.GetType());

        SocketClient.Instance.SendMessage(protoId, msg.ToByteArray());
    }
Example #8
0
    void Start()
    {
        SocketEvent.Instance.AddListener((ushort)MessageDefine.GetProtoIdByProtoType(typeof(StcUserEnter)), OnStcUserEnter);

        UserManager.Instance.LoginByAccount("1003", "123456");
    }
Example #9
0
    public UserManager()
    {
        Event.Instance.AddListener("OnTcpConnected", OnTcpConnected);

        SocketEvent.Instance.AddListener((ushort)MessageDefine.GetProtoIdByProtoType(typeof(StcUserAuthentication)), OnStcAuthentication);
    }