コード例 #1
0
    private void SendControl()
    {
        NetBitStream stream = new NetBitStream();

        stream.BeginWrite((ushort)MessageIdentifiers.ID.PLAYER_ACTION);
        Player p = m_netManager.player;

        stream.WriteString(p.name);
        stream.WriteBool(p.first);
        stream.WriteFloat(p.pos_x);
        stream.WriteFloat(p.pos_y);
        stream.WriteFloat(p.hp);
        stream.WriteInt(p.ctype);
        int count = CGameManager.Instance.ControlList().Count;

        stream.WriteInt(count);

        List <byte> lc = CGameManager.Instance.ControlList();

        for (int i = 0; i < count; i++)
        {
            stream.WriteByte(lc[i]);
        }

        stream.EncodeHeader();

        m_netManager.Send(stream);
        CGameManager.Instance.ControlList().Clear();
    }
コード例 #2
0
    public void SendChat(string message)
    {
        NetBitStream stream = new NetBitStream();

        stream.BeginWrite((ushort)MessageIdentifiers.ID.ID_CHAT);
        stream.WriteString(message);
        stream.EncodeHeader();

        m_netManager.Send(stream);
    }
コード例 #3
0
ファイル: GameClient.cs プロジェクト: 5941120/UnitySample
    // 发送聊天消息
    void SendChat()
    {
        // 将聊天消息写入NetBitStream对象
        NetBitStream stream = new NetBitStream();
        stream.BeginWrite((ushort)MessageIdentifiers.ID.ID_CHAT);
        stream.WriteString(_inputString);
        stream.EncodeHeader();

        // 发送给服务器端
        mGNManager.Send(stream);

        //清空_inputString
        _inputString = "";
    }
コード例 #4
0
    // 发送聊天消息
    void SendChat()
    {
        // 将聊天消息写入NetBitStream对象
        NetBitStream stream = new NetBitStream();

        stream.BeginWrite((ushort)MessageIdentifiers.ID.ID_CHAT);
        stream.WriteString(m_inputString);
        stream.EncodeHeader();

        // 发送给服务器端
        m_netManager.Send(stream);

        //清空m_inputString
        m_inputString = "";
    }
コード例 #5
0
ファイル: Server.cs プロジェクト: ukins/UnityFPS
    public void Login(string name,string pwd)
    {
        Debug.Log( "name = " + name + "; pwd = " + pwd );

        NetBitStream stream = new NetBitStream();
        stream.BeginWrite( (ushort)Protocol.ID.LOGIN );

        LoginProto.Login login = new LoginProto.Login();
        login.name = name;
        login.pwd = pwd;

        System.IO.MemoryStream mstream = new System.IO.MemoryStream();
        ProtoBuf.Serializer.Serialize<LoginProto.Login>( mstream, login );
        byte[] bs = mstream.ToArray();

        stream.WriteBodyBytes( bs );
        stream.EncodeHeader();

        _SvrMgr.Send( stream );
    }
コード例 #6
0
ファイル: Server.cs プロジェクト: xiaogeformax/UnityFPS
    public void Login(string name, string pwd)
    {
        Debug.Log("name = " + name + "; pwd = " + pwd);

        NetBitStream stream = new NetBitStream();

        stream.BeginWrite((ushort)Protocol.ID.LOGIN);

        LoginProto.Login login = new LoginProto.Login();
        login.name = name;
        login.pwd  = pwd;

        System.IO.MemoryStream mstream = new System.IO.MemoryStream();
        ProtoBuf.Serializer.Serialize <LoginProto.Login>(mstream, login);
        byte[] bs = mstream.ToArray();

        stream.WriteBodyBytes(bs);
        stream.EncodeHeader();

        _SvrMgr.Send(stream);
    }
コード例 #7
0
    public bool Login(string username, string password, out string msg)
    {
        if (false == m_netManager.bConnected)
        {
            msg = "无法连接到服务器,请稍后重试!";
            return(false);
        }
        m_netManager.iLoginState = 0;
        NetBitStream stream = new NetBitStream();

        stream.BeginWrite((ushort)MessageIdentifiers.ID.CLIENT_LOGIN);
        stream.WriteString(username + "&" + password);
        stream.EncodeHeader();
        m_netManager.Send(stream);
        msg = "";

        Stopwatch sw = new Stopwatch();

        sw.Start();
        while (0 == m_netManager.iLoginState)
        {
            m_netManager.Update();
            long time = sw.ElapsedMilliseconds;
            if (time > 5000)
            {
                break;
            }
        }
        sw.Stop();
        if (1 == m_netManager.iLoginState)
        {
            msg = m_netManager.strMessage;
            m_netManager.strUsername = username;
            return(true);;
        }
        msg = m_netManager.strMessage;
        return(false);
    }
コード例 #8
0
    public bool LinkWorld(out string msg)
    {
        if (false == m_netManager.bConnected)
        {
            msg = "无法连接到服务器,请稍后重试!";
            return(false);
        }
        m_netManager.iGameState = 0;
        NetBitStream stream = new NetBitStream();

        stream.BeginWrite((ushort)MessageIdentifiers.ID.CLIENT_LINK_WORLD);
        stream.WriteString(m_netManager.strUsername);
        stream.WriteInt(m_netManager.player.ctype);

        stream.EncodeHeader();
        m_netManager.Send(stream);
        msg = "";
        Stopwatch sw = new Stopwatch();

        sw.Start();
        while (0 == m_netManager.iGameState)
        {
            m_netManager.Update();
            long time = sw.ElapsedMilliseconds;
            if (time > 5000)
            {
                break;
            }
        }
        sw.Stop();
        if (1 == m_netManager.iGameState)
        {
            msg = m_netManager.strMessage;
            return(true);;
        }
        msg = m_netManager.strMessage;
        return(false);
    }
コード例 #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
            }
コード例 #10
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
    }
コード例 #11
0
        static void Main(string[] args)
        {
            ushort id      = 10;
            byte   n       = 0;
            bool   b       = false;
            int    vint    = 100;
            uint   vint2   = 999;
            short  vshort  = 10;
            ushort vshort2 = 101;
            float  number  = 0.8f;
            string data    = "我是中国人";

            NetBitStream stream = new NetBitStream();

            stream.BeginWrite(id);
            stream.WriteByte(n);
            stream.WriteBool(b);
            stream.WriteInt(vint);
            stream.WriteUInt(vint2);
            stream.WriteShort(vshort);
            stream.WriteUShort(vshort2);
            stream.WriteFloat(number);
            stream.WriteString(data);
            stream.EncodeHeader();

            NetPacket packet = new NetPacket();

            packet.CopyBytes(stream);


            NetBitStream stream2 = new NetBitStream();

            stream2.BeginRead(packet, out id);
            stream2.ReadByte(out n);
            stream2.ReadBool(out b);
            stream2.ReadInt(out vint);
            stream2.ReadUInt(out vint2);
            stream2.ReadShort(out vshort);
            stream2.ReadUShort(out vshort2);
            stream2.ReadFloat(out number);
            stream2.ReadString(out data);

            System.Console.WriteLine(id);
            System.Console.WriteLine(n);
            System.Console.WriteLine(b);
            System.Console.WriteLine(vint);
            System.Console.WriteLine(vint2);
            System.Console.WriteLine(vshort);
            System.Console.WriteLine(vshort2);
            System.Console.WriteLine(number);
            System.Console.WriteLine(data);

            NetStructManager.TestStruct cif;
            cif.header = 10;
            cif.msgid  = 5;
            cif.m      = 0.9f;
            cif.n      = 10;
            cif.str    = "hello";


            byte[] bs = NetStructManager.getBytes(cif);
            NetStructManager.EncoderHeader(ref bs);

            NetStructManager.TestStruct cif2;
            System.Type type = typeof(NetStructManager.TestStruct);

            cif2 = (NetStructManager.TestStruct)NetStructManager.fromBytes(bs, type);

            System.Console.WriteLine(":" + bs.Length);
            System.Console.WriteLine(":" + cif2.header);
            System.Console.WriteLine(cif2.msgid);
            System.Console.WriteLine(cif2.m);
            System.Console.WriteLine(cif2.n);
            System.Console.WriteLine(cif2.str);


            ChatClient client = new ChatClient();

            client.Start();
            client.Update();
        }