Example #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;
        }
    }
Example #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();
        }
    }
Example #3
0
    public Client()
    {
        readBuffer = new byte[MAX_BUFFER];
        MsgHead keepaliveHead = new MsgHead
        {
            cmd_id = (int)ServerMsgId.CCMD_KEEP_ALIVE
        };

        keepalive = ProtocolByt.StructToBytes(keepaliveHead, 16);
        ProcessCtrl.Instance.AddUpdate(this);
    }
Example #4
0
    public void Send(ServerMsgId pMsgId, object o, short sub_id = 0, uint arg1 = 0, uint arg2 = 0)
    {
        EDebug.Log("Send " + pMsgId.ToString());
        //if (NetStatus.Disconnected == status)
        //    Connect();
        MsgHead head = new MsgHead
        {
            cmd_id = (short)pMsgId,
            sub_id = sub_id,
            param1 = (int)arg1,
            param2 = (int)arg2
        };
        string msg = JsonUtility.ToJson(o);

        byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(msg);
        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);
        _msgSend.AddLast(buffer);
    }