예제 #1
0
        private void Callback_Receive(IAsyncResult result)
        {
            try
            {
                lock (this)
                {
                    Gamnet.Buffer buffer    = (Gamnet.Buffer)result.AsyncState;
                    int           recvBytes = _socket.EndReceive(result);
                    if (0 == recvBytes)
                    {
                        Debug.Log("socket read '0' byte. close socket");
                        Disconnect();
                        return;
                    }
                    buffer.write_index += recvBytes;

                    ReceiveEvent evt = new ReceiveEvent(this);
                    evt.buffer = buffer;
                    _event_queue.Add(evt);

                    Receive();
                }
            }
            catch (SocketException e)
            {
                Debug.LogError("[Session.Callback_Receive] session_state:" + _state + ", exception:" + e.ToString());
                if (ConnectionState.Disconnected == _state)
                {
                    return;
                }
                Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.ToString()));
                Disconnect();
            }
        }
예제 #2
0
 public static Gamnet.Buffer operator +(Gamnet.Buffer lhs, Gamnet.Buffer rhs)
 {
     Gamnet.Buffer buffer = new Gamnet.Buffer();
     buffer.Copy(lhs);
     buffer.Append(rhs);
     return(buffer);
 }
예제 #3
0
        private void Callback_Receive(IAsyncResult result)
        {
            try {
                Gamnet.Buffer buffer    = (Gamnet.Buffer)result.AsyncState;
                int           recvBytes = _socket.EndReceive(result);
                if (0 == recvBytes)
                {
                    Close();
                    return;
                }
                buffer.write_index += recvBytes;

                ReceiveEvent evt = new ReceiveEvent(this);
                evt.buffer = buffer;
                lock (_sync_obj) {
                    _event_queue.Add(evt);
                }
                Receive();
            }
            catch (SocketException e) {
                if (ConnectionState.Disconnected == _state)
                {
                    return;
                }
                Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.Message));
                Close();
            }
        }
예제 #4
0
 private void Receive()
 {
     try {
         Gamnet.Buffer buffer = new Gamnet.Buffer();
         _socket.BeginReceive(buffer.data, 0, Gamnet.Buffer.BUFFER_SIZE, 0, new AsyncCallback(Callback_Receive), buffer);
     }
     catch (SocketException e) {
         Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.Message));
         Close();
     }
 }
예제 #5
0
 private void Receive()
 {
     try {
         Gamnet.Buffer buffer = new Gamnet.Buffer();
         _socket.BeginReceive(buffer.data, 0, Gamnet.Buffer.BUFFER_SIZE, 0, new AsyncCallback(Callback_Receive), buffer);
     }
     catch (SocketException e) {
         Debug.LogError("[Session.Receive] exception:" + e.ToString());
         Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.ToString()));
         Disconnect();
     }
 }
예제 #6
0
        public override void OnReceive(Gamnet.Buffer buf)
        {
            recvBuffer += buf;
            while (recvBuffer.Size() >= PACKET_HEADER_SIZE)
            {
                ushort packetLength = BitConverter.ToUInt16(recvBuffer.buffer, recvBuffer.readIndex + PACKET_SIZE_OFFSET);
                if (packetLength > Gamnet.Buffer.BUFFER_SIZE)
                {
                    throw new Exception(string.Format("The packet length is greater than the buffer max length."));
                }

                if (packetLength > recvBuffer.Size())
                {
                    return;
                }

                Gamnet.Buffer tmpBuffer = new Gamnet.Buffer(recvBuffer);
                int           msgID     = BitConverter.ToInt32(tmpBuffer.buffer, tmpBuffer.readIndex + MSGID_OFFSET);
                if (false == _dictHandler.ContainsKey(msgID))
                {
                    UnityEngine.Debug.Log("msg_id:" + msgID + " is not registered");
                    ErrorEvent evt = new ErrorEvent();
                    evt.session = this;
                    evt.error   = new System.Collections.Generic.KeyNotFoundException();
                    eventQueue.Enqueue(evt);
                    return;
                }

                HandlerContainer container = _dictHandler[msgID];
                if (null != container.timer)
                {
                    container.timer.Stop();
                    container.timer.Close();
                }
                tmpBuffer.readIndex += PACKET_HEADER_SIZE;
                try
                {
                    container.handler(tmpBuffer);
                }
                catch (System.Exception) {}

                recvBuffer.readIndex += packetLength;
                recvBuffer            = new Gamnet.Buffer(recvBuffer);
            }
        }
예제 #7
0
 private void Callback_OnSend(IAsyncResult result)
 {
     try
     {
         SendStateObject obj         = (SendStateObject)result.AsyncState;
         int             writedBytes = _socket.EndSend(result);
         obj.buffer.readIndex += writedBytes;
         if (obj.buffer.Size() > 0)
         {
             Gamnet.Buffer newBuffer = new Gamnet.Buffer(obj.buffer);
             AsyncSend(newBuffer.buffer);
         }
     }
     catch (System.Exception error)
     {
         Error(error);
         Close();
     }
 }
예제 #8
0
        public void OnReceive(Gamnet.Buffer buf)
        {
            _recv_buff += buf;
            while (_recv_buff.Size() >= Packet.HEADER_SIZE)
            {
                ushort packetLength = BitConverter.ToUInt16(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_LENGTH);
                if (packetLength > Gamnet.Buffer.BUFFER_SIZE)
                {
                    Error(new Gamnet.Exception(ErrorCode.BufferOverflowError, "The packet length is greater than the buffer max length."));
                    return;
                }

                if (packetLength > _recv_buff.Size())                   // not enough
                {
                    return;
                }

                uint msgID = BitConverter.ToUInt32(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_MSGID);
                if (false == _handlers.ContainsKey(msgID))
                {
                    Error(new Gamnet.Exception(ErrorCode.UnhandledMsgError, "can't find registered msg(id:" + msgID + ")"));
                    return;
                }

                _recv_buff.read_index += Packet.HEADER_SIZE;
                _timeout_monitor.UnsetTimeout(msgID);

                Delegate_OnReceive handler = _handlers[msgID];

                try     {
                    handler(_recv_buff);
                }
                catch (System.Exception e) {
                    Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.Message));
                }

                _recv_buff.read_index += packetLength - Packet.HEADER_SIZE;
            }
        }
예제 #9
0
        public override void OnReceive(Gamnet.Buffer buf)
        {
            recvBuffer += buf;
            while (recvBuffer.Size() >= PACKET_HEADER_SIZE)
            {
                ushort packetLength = BitConverter.ToUInt16(recvBuffer.data, recvBuffer.readIndex + PACKET_SIZE_OFFSET);
                if (packetLength > Gamnet.Buffer.BUFFER_SIZE)
                {
                    throw new Exception(string.Format("The packet length is greater than the buffer max length."));
                }

                if (packetLength > recvBuffer.Size())
                { // not enough
                    return;
                }

                int msgID = BitConverter.ToInt32(recvBuffer.data, recvBuffer.readIndex + MSGID_OFFSET);
                if (false == handlers.ContainsKey(msgID))
                {
                    ErrorEvent evt = new ErrorEvent(this);
                    evt.exception = new System.Exception("can't find registered msg(id:" + msgID + ")");
                    eventQueue.Enqueue(evt);
                    return;
                }

                recvBuffer.readIndex += PACKET_HEADER_SIZE;
                Action <Gamnet.Buffer> handler = handlers[msgID];

                try
                {
                    timeoutMonitor.UnsetTimeout(msgID);
                    handler(recvBuffer);
                }
                catch (System.Exception) { }
            }
        }
예제 #10
0
        private void OnReceive(Gamnet.Buffer buf)
        {
            _recv_buff += buf;
            while (_recv_buff.Size() >= Packet.HEADER_SIZE)
            {
                ushort packetLength = BitConverter.ToUInt16(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_LENGTH);
                uint   msgID        = BitConverter.ToUInt32(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_MSGID);
                uint   msgSEQ       = BitConverter.ToUInt32(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_MSGSEQ);
                bool   reliable     = BitConverter.ToBoolean(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_RELIABLE);

                if (packetLength > Gamnet.Buffer.BUFFER_SIZE)
                {
                    Debug.LogError("[Session.OnReceive] The packet length is greater than the buffer max length.");
                    Error(new Gamnet.Exception(ErrorCode.UndefinedError, "The packet length is greater than the buffer max length."));
                    return;
                }

                if (packetLength > _recv_buff.Size())
                {                 // not enough
                    return;
                }

                _recv_buff.read_index += Packet.HEADER_SIZE;

                _timeout_monitor.UnsetTimeout(msgID);

                try
                {
                    if (false == _handlers.ContainsKey(msgID))
                    {
                        Debug.LogWarning("[Session.OnReceive] can't find registered msg(id:" + msgID + ")");
                        throw new Gamnet.Exception(ErrorCode.UnhandledMsgError, "can't find registered msg(id:" + msgID + ")");
                    }

                    if (false == reliable || _recv_seq < msgSEQ)
                    {
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        ms.Write(_recv_buff.data, _recv_buff.read_index, packetLength - Packet.HEADER_SIZE);
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
                        IMsgHandler handler = _handlers[msgID];
                        handler.OnRecvMsg(ms);
                        _recv_seq = Math.Max(_recv_seq, msgSEQ);
                        if (true == reliable)
                        {
                            Send_ReliableAck_Ntf(_recv_seq);
                        }
                    }
                }
                catch (Gamnet.Exception e)
                {
                    Debug.LogError("[Session.OnReceive] " + e.ToString());
                    Error(e);
                }
                catch (System.Exception e)
                {
                    Debug.LogError("[Session.OnReceive] " + e.ToString());
                    Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.ToString()));
                }

                _recv_buff.read_index += packetLength - Packet.HEADER_SIZE;
            }
        }
예제 #11
0
 public abstract void OnReceive(Gamnet.Buffer buf);