Exemplo n.º 1
0
        void OnReadPacketHeader(IAsyncResult result)
        {
            try
            {
                if (mSocket == null)
                {
                    return;
                }
                ReceiveBuffer buffer = (ReceiveBuffer)result.AsyncState;
                int           size   = mSocket.EndReceive(result);
                if (size < 0)
                {
                    return;
                }

                if (size == 0)
                { //remote server is shutdown
                    throw new Exception("remote server shutdown");
                }
                else if (size == buffer.size)
                { //package's header is received
                    int len = Packet.ReadInt32(buffer.buffer, 0);
                    if (len > Packet.MaxSize)
                    {
                        throw new Exception("packet size error");
                    }
                    ReceiveBuffer body = new ReceiveBuffer(len);
                    Packet.WriteInt32(len, body.buffer, 0);
                    mSocket.BeginReceive(body.buffer, Packet.SizeBytes, body.size - Packet.SizeBytes, SocketFlags.None, OnReadPacketBody, body);
                }
                else
                {
                    buffer.offset = buffer.offset + size;
                    mSocket.BeginReceive(buffer.buffer, buffer.offset, buffer.size, SocketFlags.None, OnReadPacketHeader, buffer);
                }
            }
            catch (SocketException e)
            {
                Log.e(e.Message, Log.Tag.Net);
                EventMgr.single.SendEvent(NetworkMgr.EventRecvError, this);
            }
            catch (ObjectDisposedException e)
            {
                Log.e(e.Message, Log.Tag.Net);
                EventMgr.single.SendEvent(NetworkMgr.EventRecvError, this);
            }
            catch (Exception e)
            {
                Log.e(e.Message, Log.Tag.Net);
                EventMgr.single.SendEvent(NetworkMgr.EventRecvError, this);
            }
        }
Exemplo n.º 2
0
        void OnReadPacketBody(IAsyncResult result)
        {
            try
            {
                if (mSocket == null)
                {
                    return;
                }
                ReceiveBuffer buffer = (ReceiveBuffer)result.AsyncState;
                int           size   = mSocket.EndReceive(result);
                if (size < 0)
                {
                    return;
                }

                if (size == 0)
                { //remote server is shutdown
                    throw new Exception("remote server shutdown");
                }
                else if (size == buffer.size - Packet.SizeBytes)
                { //package's body is recevied
                    //BufferReader msg = new BufferReader(buffer.buffer, 0);
                    AddPacketResult(buffer.buffer);
                    ReceiveBuffer head = new ReceiveBuffer(Packet.SizeBytes);
                    mSocket.BeginReceive(head.buffer, head.offset, head.size, SocketFlags.None, OnReadPacketHeader, head);
                }
                else
                {
                    buffer.offset = buffer.offset + size;
                    mSocket.BeginReceive(buffer.buffer, buffer.offset, buffer.size, SocketFlags.None, OnReadPacketBody, buffer);
                }
            }
            catch (SocketException)
            {
                EventMgr.single.SendEvent(NetworkMgr.EventRecvError, this);
            }
            catch (ObjectDisposedException)
            {
                EventMgr.single.SendEvent(NetworkMgr.EventRecvError, this);
            }
            catch (Exception)
            {
                EventMgr.single.SendEvent(NetworkMgr.EventRecvError, this);
            }
        }
Exemplo n.º 3
0
        void ConnectCallback(IAsyncResult asyncConnect)
        {
            Socket client = (Socket)asyncConnect.AsyncState;

            try
            {
                client.EndConnect(asyncConnect);
                if (!client.Connected)
                {
                    throw new Exception("connect failed");
                }
                //开始收包
                Log.d("Socket Connect End. Begin Read", Log.Tag.Net);
                ReceiveBuffer buffer = new ReceiveBuffer(Packet.SizeBytes);
                mSocket.BeginReceive(buffer.buffer, buffer.offset, buffer.size, SocketFlags.None, OnReadPacketHeader, buffer);
                //========
                mReadySend = true;
                netState   = State.Ok;
                EventMgr.single.SendEvent(NetworkMgr.EventConnect, this);
            }
            catch (SocketException e)
            {
                Log.e(e, Log.Tag.Net);
                netState = State.Reset;
                EventMgr.single.SendEvent(NetworkMgr.EventDisconnect, this);
            }
            catch (ObjectDisposedException e)
            {
                Log.e(e, Log.Tag.Net);
                netState = State.Reset;
                EventMgr.single.SendEvent(NetworkMgr.EventDisconnect, this);
            }
            catch (Exception e)
            {
                Log.e(e, Log.Tag.Net);
                netState = State.Reset;
                EventMgr.single.SendEvent(NetworkMgr.EventDisconnect, this);
            }
        }