コード例 #1
0
    public void Analysis(Client pClient)
    {
        int Count = pClient.bufferCount;

        try
        {
            pClient.msgLength = pClient.readBuffer.Length;

            object  cmdmsgs = ProtocolByt.ByteToStruct(pClient.readBuffer, typeof(MsgHead));
            MsgHead head    = (MsgHead)cmdmsgs;
            pClient.bufferCount -= Marshal.SizeOf(head);
            string msg = "";
            if (pClient.bufferCount > 0)
            {
                msg         = System.Text.Encoding.UTF8.GetString(pClient.readBuffer, Count - pClient.bufferCount, head.len);
                bufferCount = 0;
            }

            EDebug.LogFormat("Analysis {0} {1}", head.cmd_id, msg);

            ServerMsgObj serverMsgPair = new ServerMsgObj
            {
                MsgId = (int)head.cmd_id,
                SubId = (int)head.sub_id,
                Msg   = msg
            };
            _msgReceived.AddLast(serverMsgPair);
        }
        catch (Exception e)
        {
            EDebug.Log(e.ToString());
            OnDisconnect();
            //throw;
        }
    }
コード例 #2
0
    /// <summary>
    /// 注册和登录都走这里,根据AutoLoginOrRegister区分
    /// </summary>
    public void Login()
    {
        if (string.IsNullOrEmpty(_userName) || string.IsNullOrEmpty(_passWd))
        {
            return;
        }
        EDebug.LogFormat("Login {0} {1} Login Or Regist {2}", _userName, _passWd, AutoLoginOrRegist);
        MsgHead head = new MsgHead
        {
            cmd_id = AutoLoginOrRegist ? (short)ServerMsgId.CCMD_ROLE_AUTH : (short)ServerMsgId.CCMD_ROLE_REG
        };
        LoginMsg loginMsg = new LoginMsg
        {
            type   = 0,
            name   = _userName,
            passwd = _passWd
        };
        string loginInfo = JsonUtility.ToJson(loginMsg);

        byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(loginInfo);
        head.len = bMsg.Length;
        byte[] bHead  = ProtocolByt.StructToBytes(head, 16);
        byte[] buffer = new byte[bHead.Length + bMsg.Length];
        System.Array.Copy(bHead, buffer, bHead.Length);
        System.Array.Copy(bMsg, 0, buffer, bHead.Length, bMsg.Length);
        try
        {
            client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null);
        }
        catch (Exception e)
        {
            EDebug.LogErrorFormat("Client.Login Socket Exception {0}", e.ToString());
            OnDisconnect();
        }
    }