Beispiel #1
0
    private void MSG_CLIENT_LINK_WORLD()
    {
        NetBitStream stream = new NetBitStream();

        stream.BeginRead2(packet);

        // 从流中读取新登陆玩家状态信息, 并写入
        string name;
        bool   first;
        float  pos_x;
        float  pos_y;
        float  hp;
        int    ctype;

        stream.ReadString(out name);
        stream.ReadBool(out first);
        stream.ReadFloat(out pos_x);
        stream.ReadFloat(out pos_y);
        stream.ReadFloat(out hp);
        stream.ReadInt(out ctype);

        Player p = new Player(name, first, pos_x, pos_y, hp, ctype);

        playerList.Add(p);
        CGameManager.Instance.AddPlayer(p);
    }
Beispiel #2
0
    // Update is called once per frame
    public override void Update()
    {
        NetPacket packet = null;

        for (packet = GetPacket(); packet != null;)
        {
            ushort msgid = 0;
            packet.TOID(out msgid);

            switch (msgid)
            {
            case (ushort)MessageIdentifiers.ID.CONNECTION_REQUEST_ACCEPTED:
            {
                Debug.Log("连接到服务器");
                break;
            }

            case (ushort)MessageIdentifiers.ID.CONNECTION_ATTEMPT_FAILED:
            {
                Debug.Log("连接服务器失败,请退出");
                break;
            }

            case (ushort)MessageIdentifiers.ID.CONNCET_LOST:
            {
                Debug.Log("丢失服务器的连接,请按任意键退出");
                break;
            }

            case (ushort)Protocol.ID.LOGIN:
            {
                NetBitStream stream = new NetBitStream();

                stream.BeginRead2(packet);

                stream.DecodeHeader();

                byte[] bs = stream.ReadBodyBytes();
                System.IO.MemoryStream mstream  = new System.IO.MemoryStream(bs);
                CommonProto.Response   response = ProtoBuf.Serializer.Deserialize <CommonProto.Response>(mstream);

                Debug.Log("responseid = " + response.id + "; " + response.desc);

                if (response.id == 0)
                {
                    LoadingScene.LoadNewScene("demo");
                }
                break;
            }

            default:
            {
                break;
            }
            }
            packet = null;
        }
    }
Beispiel #3
0
	// Update is called once per frame
	public override void Update () {
        NetPacket packet = null;

        for ( packet = GetPacket(); packet != null; )
        {
            ushort msgid = 0;
            packet.TOID( out msgid );

            switch(msgid)
            {
                case (ushort)MessageIdentifiers.ID.CONNECTION_REQUEST_ACCEPTED:
                    {
                        Debug.Log( "连接到服务器" );
                        break;
                    }
                case (ushort)MessageIdentifiers.ID.CONNECTION_ATTEMPT_FAILED:
                    {
                        Debug.Log( "连接服务器失败,请退出" );
                        break;
                    }
                case (ushort)MessageIdentifiers.ID.CONNCET_LOST:
                    {
                        Debug.Log( "丢失服务器的连接,请按任意键退出" );
                        break;
                    }
                case (ushort)Protocol.ID.LOGIN:
                    {
                        NetBitStream stream = new NetBitStream();

                        stream.BeginRead2( packet );

                        stream.DecodeHeader();

                        byte[] bs = stream.ReadBodyBytes();
                        System.IO.MemoryStream mstream = new System.IO.MemoryStream( bs );
                        CommonProto.Response response = ProtoBuf.Serializer.Deserialize<CommonProto.Response>( mstream );

                        Debug.Log( "responseid = "+response.id + "; " + response.desc );

                        if ( response.id == 0 ) { 
                            LoadingScene.LoadNewScene( "demo" );
                        }
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            packet = null;
        }
	}
Beispiel #4
0
    private void MSG_CLIENT_LOGIN_RESPONSE()
    {
        bool         success;
        string       msg = "";
        bool         first;
        float        pos_x;
        float        pos_y;
        float        hp;
        int          ctype;
        NetBitStream stream = new NetBitStream();

        stream.BeginRead2(packet);
        stream.ReadBool(out success);
        stream.ReadString(out msg);

        //读取用户基本状态信息,并更新信息
        stream.ReadBool(out first);
        stream.ReadFloat(out pos_x);
        stream.ReadFloat(out pos_y);
        stream.ReadFloat(out hp);
        stream.ReadInt(out ctype);
        //更新用户基本信息
        player.name  = strUsername;
        player.first = first;
        player.pos_x = pos_x;
        player.pos_y = pos_y;
        player.hp    = hp;
        player.ctype = ctype;

        Debug.Log("$$$" + strUsername);
        Debug.Log(first);
        Debug.Log(pos_x);
        Debug.Log(pos_y);
        Debug.Log(hp);
        Debug.Log(ctype);

        if (success)
        {
            Debug.Log("登录成功!");
            iLoginState = 1;
        }
        else
        {
            Debug.Log("登录失败!");
            iLoginState = 2;
        }
        strMessage = msg;
    }
Beispiel #5
0
    private void MSG_ID_CHAT()
    {
        NetBitStream stream  = new NetBitStream();
        string       recvStr = "";

        stream.BeginRead2(packet);
        stream.ReadString(out recvStr);
        NetClient.Instance.RecvString = recvStr;
        //string _name = "";
        //foreach (Player p in playerList)
        //{
        //    if (p.peer.RemoteEndPoint.ToString().Equals(packet.peer.RemoteEndPoint.ToString()))
        //    {
        //        _name = p.name;
        //    }
        //}
        CMenuCallback.Instance.textlist.Add(packet.peer.RemoteEndPoint.ToString() + ": " + recvStr);
    }
Beispiel #6
0
    private void MSG_CLIENT_EXIT_WORLD_RESPONSE()
    {
        //to do ,读取流信息, 跟新玩家列表与信息
        NetBitStream stream = new NetBitStream();

        stream.BeginRead2(packet);
        string name = "";

        stream.ReadString(out name);
        foreach (Player p in playerList)
        {
            if (p.name.Equals(name))
            {
                playerList.Remove(p);
                CGameManager.Instance.RemovePlayer(name);
                break;
            }
        }
    }
Beispiel #7
0
    private void MSG_CLIENT_LINK_WORLD_RESPONSE()
    {
        bool         success;
        string       msg    = "";
        NetBitStream stream = new NetBitStream();

        stream.BeginRead2(packet);
        stream.ReadBool(out success);
        stream.ReadString(out msg);
        if (success)
        {
            Debug.Log("登入游戏世界成功**");
            iGameState = 1;
            // 从流中读取世界状态信息, 并写入
            int    count;
            string name;
            bool   first;
            float  pos_x;
            float  pos_y;
            float  hp;
            int    ctype;
            stream.ReadInt(out count);
            for (int i = 0; i < count; i++)
            {
                stream.ReadString(out name);
                stream.ReadBool(out first);
                stream.ReadFloat(out pos_x);
                stream.ReadFloat(out pos_y);
                stream.ReadFloat(out hp);
                stream.ReadInt(out ctype);

                Player p = new Player(name, first, pos_x, pos_y, hp, ctype);
                playerList.Add(p);
                CGameManager.Instance.InitPlayer(p);
            }
        }
        else
        {
            Debug.Log("登录游戏世界失**");
            iGameState = 2;
        }
        strMessage = msg;
    }
Beispiel #8
0
    private void MSG_CLIENT_REGISTER_RESPONSE()
    {
        bool         success;
        string       msg    = "";
        NetBitStream stream = new NetBitStream();

        stream.BeginRead2(packet);
        stream.ReadBool(out success);
        stream.ReadString(out msg);
        if (success)
        {
            Debug.Log("注册成功!");
            iRegisterState = 1;
        }
        else
        {
            Debug.Log("注册失败!");
            iRegisterState = 2;
        }
        strMessage = msg;
    }
Beispiel #9
0
            public override void Update()
            {
                NetPacket packet = null;

                while (true)
                {
                    for (packet = GetPacket(); packet != null;)
                    {
                        // 获得消息ID
                        ushort msgid = 0;
                        packet.TOID(out msgid);

                        switch (msgid)
                        {
                        case (ushort)MessageIdentifiers.ID.NEW_INCOMING_CONNECTION:
                        {
                            System.Console.WriteLine("新的连接:" + packet._peer.RemoteEndPoint.ToString());

                            _socketList.Add(packet._peer);
                            break;
                        }

                        case (ushort)MessageIdentifiers.ID.CONNECTION_LOST:
                        {
                            System.Console.WriteLine("一个用户退出");

                            _socketList.Remove(packet._peer);
                            break;
                        }

                        case (ushort)MessageIdentifiers.ID.ID_CHAT:
                        {
                            string chatdata = "";

                            // 读取聊天消息
                            NetBitStream stream = new NetBitStream();

                            stream.BeginRead2(packet);
                            stream.ReadString(out chatdata);
                            stream.EncodeHeader();

                            // 群发聊天消息
                            for (int i = 0; i < _socketList.Count; i++)
                            {
                                Socket sk = (Socket)_socketList[i];
                                if (sk == packet._peer)
                                {
                                    continue;
                                }
                                _server.Send(stream, sk);
                            }

                            //System.Console.WriteLine("收到消息:" + chatdata);
                            break;
                        }

                        case (ushort)MessageIdentifiers.ID.ID_CHAT2:
                        {
                            // 读取聊天消息
                            NetStructManager.TestStruct chatstr;
                            chatstr = (NetStructManager.TestStruct)NetStructManager.fromBytes(packet._bytes, typeof(NetStructManager.TestStruct));
                            System.Console.WriteLine("header:" + chatstr.header);
                            System.Console.WriteLine("msgid:" + chatstr.msgid);
                            System.Console.WriteLine("m:" + chatstr.n);
                            System.Console.WriteLine("n:" + chatstr.m);
                            System.Console.WriteLine("str:" + chatstr.str);

                            NetBitStream stream = new NetBitStream();
                            stream.CopyBytes(packet._bytes);

                            // 群发聊天消息
                            for (int i = 0; i < _socketList.Count; i++)
                            {
                                Socket sk = (Socket)_socketList[i];
                                if (sk == packet._peer)
                                {
                                    continue;
                                }
                                _server.Send(stream, sk);
                            }

                            break;
                        }

                        default:
                        {
                            // 错误
                            break;
                        }
                        }

                        packet = null;
                    } // end fore
                }     // end while
            }
    public override void Update()
    {
        NetPacket packet = null;
        while (true)
        {

            for (packet = GetPacket(); packet != null; )
            {
                // 获得消息ID
                ushort msgid = 0;
                packet.TOID(out msgid);

                switch (msgid)
                {
                    case (ushort)MessageIdentifiers.ID.NEW_INCOMING_CONNECTION:
                        {
                            System.Console.WriteLine("新的连接:" + packet._peer.RemoteEndPoint.ToString());

                            _socketList.Add(packet._peer);
                            break;
                        }
                    case (ushort)MessageIdentifiers.ID.CONNECTION_LOST:
                        {
                            System.Console.WriteLine("一个用户退出");

                            _socketList.Remove(packet._peer);
                            break;
                        }
                    case (ushort)MessageIdentifiers.ID.ID_CHAT:
                        {
                            string chatdata = "";

                            // 读取聊天消息
                            NetBitStream stream = new NetBitStream();

                            stream.BeginRead2(packet);
                            stream.ReadString(out chatdata);
                            stream.EncodeHeader();

                            // 群发聊天消息
                            for (int i = 0; i < _socketList.Count; i++)
                            {
                                Socket sk = (Socket)_socketList[i];
                                if (sk == packet._peer)
                                    continue;
                                _server.Send(stream, sk);
                            }

                            System.Console.WriteLine("收到消息:" + chatdata);
                            break;
                        }
                    case (ushort)MessageIdentifiers.ID.ID_CHAT2:
                        {

                            // 读取聊天消息
                            NetStructManager.TestStruct chatstr;
                            chatstr = (NetStructManager.TestStruct)NetStructManager.fromBytes(packet._bytes, typeof(NetStructManager.TestStruct));
                            System.Console.WriteLine("header:" + chatstr.header);
                            System.Console.WriteLine("msgid:" + chatstr.msgid);
                            System.Console.WriteLine("m:" + chatstr.n);
                            System.Console.WriteLine("n:" + chatstr.m);
                            System.Console.WriteLine("str:" + chatstr.str);

                            NetBitStream stream = new NetBitStream();
                            stream.CopyBytes(packet._bytes);

                            // 群发聊天消息
                            for (int i = 0; i < _socketList.Count; i++)
                            {
                                Socket sk = (Socket)_socketList[i];
                                if (sk == packet._peer)
                                    continue;
                                _server.Send(stream, sk);
                            }

                            break;
                        }

                    default:
                        {
                            // 错误
                            break;
                        }
                }

                packet = null;

            }// end fore
        }// end while
    }
Beispiel #11
0
    private void MSG_PLAYER_ACTION()
    {
        //读取流信息,更新玩家状态
        NetBitStream stream = new NetBitStream();

        stream.BeginRead2(packet);

        string name;
        bool   first;
        float  pos_x;
        float  pos_y;
        float  hp;
        int    ctype;

        stream.ReadString(out name);
        stream.ReadBool(out first);
        stream.ReadFloat(out pos_x);
        stream.ReadFloat(out pos_y);
        stream.ReadFloat(out hp);
        stream.ReadInt(out ctype);

        Player player = null;

        foreach (Player p in playerList)
        {
            if (p.name.Equals(name))
            {
                player = p;
            }
        }

        player.name  = name;
        player.first = first;
        player.pos_x = pos_x;
        player.pos_y = pos_y;
        player.hp    = hp;
        player.ctype = ctype;

        List <byte> lc = new List <byte>();
        int         count;

        stream.ReadInt(out count);
        for (int i = 0; i < count; i++)
        {
            byte b;
            stream.ReadByte(out b);
            lc.Add(b);
        }
        //传递控制信息
        CGameManager.Instance.UpdateControl(name, lc);
        Debug.Log("recv control ****");
        Debug.Log(name);
        Debug.Log(first);
        Debug.Log(pos_x);
        Debug.Log(pos_y);
        Debug.Log(hp);
        Debug.Log(ctype);
        string str = "";

        foreach (byte b in lc)
        {
            str += b + "-";
        }
        Debug.Log(str);
    }
    public override void Update()
    {
        NetPacket packet = null;

        for (packet = GetPacket(); packet != null; )
        {
            ushort msgid = 0;
            packet.TOID(out msgid);

            switch (msgid)
            {
                case (ushort)MessageIdentifiers.ID.CONNECTION_REQUEST_ACCEPTED:
                    {
                        Debug.Log("连接到服务器");
                        break;
                    }
                case (ushort)MessageIdentifiers.ID.CONNECTION_ATTEMPT_FAILED:
                    {
                        Debug.Log("连接服务器失败,请退出" + packet._error);
                        break;
                    }
                case (ushort)MessageIdentifiers.ID.CONNECTION_LOST:
                    {
                        Debug.Log("失与服务器的连接,请按任意键退出" + packet._error);
                        break;
                    }
                case (ushort)MessageIdentifiers.ID.ID_CHAT:
                    {

                        NetBitStream stream = new NetBitStream();
                        stream.BeginRead2(packet);
                        stream.ReadString(out GameClient.Instance._revString);

                        break;
                    }
                case (ushort)MessageIdentifiers.ID.ID_CHAT2:
                    {
                        NetStructManager.TestStruct chatstr;
                        chatstr = (NetStructManager.TestStruct)NetStructManager.fromBytes(packet._bytes, typeof(NetStructManager.TestStruct));

                        GameClient.Instance._revString = chatstr.str;

                        Debug.Log(chatstr.header);
                        Debug.Log(chatstr.msgid);
                        Debug.Log(chatstr.m);
                        Debug.Log(chatstr.n);
                        Debug.Log(chatstr.str);

                        break;
                    }
                default:
                    {
                        // 错误
                        break;
                    }
            }// end switch

            // 销毁数据包
            packet = null;

        }// end for
    }
Beispiel #13
0
            public override void Update()
            {
                NetPacket packet = null;

                while (true)
                {
                    for (packet = GetPacket(); packet != null;)
                    {
                        ushort msgid = 0;
                        packet.TOID(out msgid);

                        switch (msgid)
                        {
                        case (ushort)MessageIdentifiers.ID.CONNECTION_REQUEST_ACCEPTED:
                        {
                            System.Console.WriteLine("连接到服务器");
                            NetThread = new System.Threading.Thread(new System.Threading.ThreadStart(Input));
                            NetThread.Start();
                            break;
                        }

                        case (ushort)MessageIdentifiers.ID.CONNECTION_ATTEMPT_FAILED:
                        {
                            System.Console.WriteLine("连接服务器失败,请按任意键退出");
                            System.Console.Read();
                            return;
                        }

                        case (ushort)MessageIdentifiers.ID.CONNECTION_LOST:
                        {
                            System.Console.WriteLine("丢失与服务器的连接,请按任意键退出");
                            System.Console.Read();
                            return;
                        }

                        case (ushort)MessageIdentifiers.ID.ID_CHAT:
                        {
                            string chatdata = "";

                            NetBitStream stream = new NetBitStream();
                            stream.BeginRead2(packet);
                            stream.ReadString(out chatdata);

                            System.Console.WriteLine("收到消息:" + chatdata);
                            break;
                        }

                        case (ushort)MessageIdentifiers.ID.ID_CHAT2:
                        {
                            NetStructManager.TestStruct chatstr;

                            chatstr = (NetStructManager.TestStruct)NetStructManager.fromBytes(packet._bytes, typeof(NetStructManager.TestStruct));

                            System.Console.WriteLine("收到消息:" + chatstr.str);
                            break;
                        }

                        default:
                        {
                            // 错误
                            break;
                        }
                        }

                        packet = null;
                    } // end fore
                }     // end while
            }