Ejemplo n.º 1
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();
        }
    }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
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);
    }