Exemplo n.º 1
0
        public void Send()
        {
            if (m_socket == null)
            {
                return;
            }
            IOSocket io = new IOSocket();

            io.WriteInt32(NetMsgDefine.HeadBeat);
            io.WriteByte(1);
            m_sendEventArgs.SetBuffer(io.GetBuffer(), 0, io.GetLength());
            m_socket.SendAsync(m_sendEventArgs);
        }
Exemplo n.º 2
0
        public void SendHello(string str)
        {
            IOSocket       io  = new IOSocket();
            SayHelloStruct say = new SayHelloStruct();

            say.msgtype = NetMsgDefine.sayhello;
            say.str     = str;
            io.WriteInt32(say.msgtype);
            io.WriteString8(say.str);
            //var  buffer = new byte[1000];
            //Array.Copy(buffer2, buffer, buffer2.Length);
            m_sendEventArgs.SetBuffer(io.GetBuffer(), 0, io.GetLength());

            m_socket.SendAsync(m_sendEventArgs);
        }
Exemplo n.º 3
0
        public void OnRecv(object target, SocketAsyncEventArgs e)
        {
            IOSocket io = new IOSocket(e.Buffer); // 思考下这里怎么避免重复new对象 避免gc

            io.Seek(0);
            int type = io.ReadInt32();

            switch (type)
            {
            case NetMsgDefine.HeadBeat:
            {
                lastrecvTime = Environment.TickCount;
                Console.WriteLine("收到客户端心跳包" + io.ReadByte().ToString());
                Recv();
            }

            break;

            default:
                break;
            }
        }
Exemplo n.º 4
0
        public void OnRecv(object sender, SocketAsyncEventArgs e)
        {
            IOSocket io = new IOSocket(e.Buffer);

            io.Seek(0);

            int type = io.ReadInt32();

            switch (type)
            {
            case NetMsgDefine.sayhello:
            {
                string str = io.ReadString16();
                Console.WriteLine(str);
            }

            break;

            case NetMsgDefine.HeadBeat:
            {
                Console.WriteLine("心跳到这里来了吗?");
                string str = io.ReadByte().ToString();
                Console.WriteLine(str);
            }

            break;

            case NetMsgDefine.GameLogic:
                break;

            default:
                break;
            }



            this.Recv(); // 成功recv之后继续recv 我们测试客户
        }