Пример #1
0
    void SendChat2()
    {
        NetStructManager.TestStruct chatstr;
        chatstr.header = 0;
        chatstr.msgid = (ushort)MessageIdentifiers.ID.ID_CHAT2;
        chatstr.m = 0.1f;
        chatstr.n = 1000;
        chatstr.str = _inputString;

        byte[] bytes = NetStructManager.GetStructBytes(chatstr);

        NetBitStream stream = new NetBitStream();
        stream.CopyBytes(bytes);

        mGNManager.Send(stream);

        //清空_inputString
        _inputString = "";
    }
Пример #2
0
    void SendChat2()
    {
        NetStructManager.TestStruct chatstr;
        chatstr.header = 0;
        chatstr.msgid  = (ushort)MessageIdentifiers.ID.ID_CHAT2;
        chatstr.m      = 0.1f;
        chatstr.n      = 1000;
        chatstr.str    = m_inputString;

        byte[] bytes = NetStructManager.getBytes(chatstr);

        NetBitStream stream = new NetBitStream();

        stream.CopyBytes(bytes);

        m_netManager.Send(stream);

        //清空m_inputString
        m_inputString = "";
    }
Пример #3
0
            public void Input()
            {
                while (true)
                {
                    string str = System.Console.ReadLine();
                    if (str.CompareTo("quit") == 0)
                    {
                        client.Disconnect(0);
                        NetThread.Abort();
                        break;
                    }
                    else
                    {
                        /*
                         * NetBitStream stream = new NetBitStream();
                         * stream.BeginWrite((ushort)MessageIdentifiers.ID.ID_CHAT);
                         * stream.WriteString(str);
                         * stream.EncodeHeader();
                         *
                         * client.Send(stream);
                         */

                        NetStructManager.TestStruct chatstr;
                        chatstr.header = 0;
                        chatstr.msgid  = (ushort)MessageIdentifiers.ID.ID_CHAT2;
                        chatstr.m      = 0.1f;
                        chatstr.n      = 100;
                        chatstr.str    = str;

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

                        NetBitStream stream = new NetBitStream();
                        stream.CopyBytes(bs);

                        client.Send(stream);
                    }
                }
            }
Пример #4
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
            }
Пример #5
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
    }