public void OnDataEvent(int hostId, int connectionId, int channelId, byte[] buffer, int dataSize)
 {
     if (_serverPeer != null)
     {
         var msg = new Packet(buffer, dataSize);
         _serverPeer.OnMessage(msg);
     }
     else
     {
         Debug.LogWarning(string.Format("[OnDataEvent] Server peer not found. Id : {0}", connectionId));
     }
 }
Exemple #2
0
        public void OnMessage(Packet msg)
        {
            // active close를 위한 코딩.
            //   서버에서 종료하라고 연락이 왔는지 체크한다.
            //   만약 종료신호가 맞다면 disconnect를 호출하여 받은쪽에서 먼저 종료 요청을 보낸다.
            switch (msg.ProtocolID)
            {
            case SYS_CLOSE_REQ:
                Disconnect();
                return;

            case SYS_START_HEARTBEAT:
            {
                // 순서대로 파싱해야 하므로 프로토콜 아이디는 버린다.
                msg.PopProtocolID();
                // 전송 인터벌.
                byte interval = msg.PopByte();
                _heartbeatSender = new HeartbeatSender(this, interval);

                if (_autoHeartbeat)
                {
                    StartHeartbeat();
                }
            }
                return;

            case SYS_UPDATE_HEARTBEAT:
                LatestHeartbeatTime = DateTime.Now.Ticks;
                return;
            }


            if (_peer != null)
            {
                try
                {
                    switch (msg.ProtocolID)
                    {
                    case SYS_CLOSE_ACK:
                        _peer.OnRemoved();
                        break;

                    default:
                        _peer.OnMessage(msg);
                        break;
                    }
                }
                catch (Exception)
                {
                    Close();
                }
            }

            if (msg.ProtocolID == SYS_CLOSE_ACK)
            {
                if (onSessionClosed != null)
                {
                    onSessionClosed(this);
                }
            }
        }