示例#1
0
        private async Task ProcessPacketAsync()
        {
            NetDataReader reader = new NetDataReader();

            while (_cts.IsCancellationRequested == false)
            {
                NetPacket packet = null;
                try
                {
                    packet = await _receivedPacketQueue.ReceiveAsync(_cts.Token);
                }
                catch
                {
                    break;
                }

                if (packet == null)
                {
                    continue;
                }

                do
                {
                    try
                    {
                        int headerSize = NetPacket.GetHeaderSize(packet.Property);
                        reader.SetSource(packet.RawData, headerSize, packet.Size);

                        switch (packet.Property)
                        {
                        case PacketProperty.Request:
                        {
                            await _request.OnReceive(packet.Property, packet.DeliveryMethod, reader, OnRequestReceived);
                        }
                        break;

                        case PacketProperty.ViewRequest:
                        {
                            throw new Exception("Not support ViewRequest");
                        }

                        default:
                        {
                            await OnReceive(reader);
                        }
                        break;
                        }
                    }
                    catch (Exception ex)
                    {
                        OnError(ex);
                        break;
                    }
                    finally
                    {
                        NetPool.PacketPool.Free(packet);
                    }
                }while (_receivedPacketQueue.TryReceive(out packet) == true && packet != null);
            }
        }
示例#2
0
        public void Update(int elapsedTime)
        {
            try
            {
                _udpChannel?.Update(elapsedTime);

                NetPacket packet;
                while (_receivedPacketQueue.TryDequeue(out packet) == true &&
                       packet != null)
                {
                    try
                    {
                        int headerSize = NetPacket.GetHeaderSize(packet.Property);
                        _packetReader.SetSource(packet.RawData, headerSize, packet.Size);

                        if (packet.Property == PacketProperty.Request)
                        {
                            _request.OnReceive(packet.Property, packet.DeliveryMethod, _packetReader, _netClient.OnRequestReceive).Wait();
                        }
                        else if (packet.Property == PacketProperty.ViewRequest)
                        {
                            _request.OnReceive(packet.Property, packet.DeliveryMethod, _packetReader, OnViewRequestReceived).Wait();
                        }
                        else
                        {
                            OnReceive(_packetReader).Wait();
                        }
                    }
                    catch (Exception ex)
                    {
                        OnError(ex);
                        break;
                    }
                    finally
                    {
                        NetPool.PacketPool.Free(packet);
                    }
                }
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
        }
示例#3
0
        private bool OnPreProcessPacket(NetPacket poolingPacket, NetDataReader reader)
        {
            try
            {
                switch (poolingPacket.Property)
                {
                case PacketProperty.AliveCheck:
                {
                    byte type = poolingPacket.RawData[NetPacket.HeaderSize];
                    //_logger.LogInformation($"Received PacketProperty.AliveCheck Type[{type}]");

                    if (type == 0xFF)
                    {
                        var packet = NetPool.PacketPool.Alloc(PacketProperty.AliveCheck);
                        packet.RawData[NetPacket.HeaderSize] = 0;
                        TcpChannel.SendAsync(packet);
                    }
                }
                break;

                case PacketProperty.JoinP2p:
                {
                    ushort     groupId         = reader.ReadUInt16();
                    ushort     sessionId       = reader.ReadUInt16();
                    ushort     masterSessionId = reader.ReadUInt16();
                    IPEndPoint remoteEndPoint  = reader.ReadIPEndPoint();
                    IPEndPoint localEndPoint   = reader.ReadIPEndPoint();

                    if (SessionId == sessionId)
                    {
                        // 같다면 P2P그룹을 생성한다.
                        if (_p2pGroup != null)
                        {
                            throw new Exception("already joined p2p group");
                        }

                        _udpChannel.RemoteEndPoint = remoteEndPoint;
                        _udpChannel.TempEndPoint   = remoteEndPoint;

                        _p2pGroup = new P2pGroup(this, groupId, masterSessionId);

                        // 자신을 참가 시킨다
                        var member = _p2pGroup.Join(this, masterSessionId);

                        OnP2pGroupJoined?.Invoke(sessionId, true);

                        //NetDebug.Log(string.Format("Create P2P Group {0} {1} {2} {3}", groupSessionKey, sessionKey, masterSessionKey, remoteEndPoint));
                    }
                    else
                    {
                        if (_p2pGroup == null)
                        {
                            throw new Exception("not exist join group");
                        }

                        var p2pSession = new P2pSession(this, sessionId, remoteEndPoint, localEndPoint);
                        p2pSession.OnViewRequestReceived = OnViewRequestReceive;

                        // 다르다면 조인시킨다
                        var member = _p2pGroup.Join(p2pSession, masterSessionId);

                        OnP2pGroupJoined?.Invoke(sessionId, false);

                        //NetDebug.Log(string.Format("Join P2P Group {0} {1} {2} {3}", groupSessionKey, sessionKey, masterSessionKey, remoteEndPoint));
                    }
                }
                break;

                case PacketProperty.LeaveP2p:
                {
                    ushort groupId         = reader.ReadUInt16();
                    ushort sessionId       = reader.ReadUInt16();
                    ushort masterSessionId = reader.ReadUInt16();

                    if (SessionId == sessionId)
                    {
                        // 같다면 P2P그룹을 파괴한다.
                        _p2pGroup?.Close();
                        _p2pGroup = null;

                        OnP2pGroupLeaved?.Invoke(sessionId, true);

                        _logger.LogInformation($"Destroy P2p {groupId} {sessionId}");
                    }
                    else
                    {
                        // 다르다면 떠나보낸다
                        var member = _p2pGroup.Leave(sessionId, masterSessionId);
                        if (member != null)
                        {
                            _udpSocket.RemoveSession(member.Session, true);
                            member.Close();
                            OnP2pGroupLeaved?.Invoke(sessionId, false);
                        }

                        _logger.LogInformation($"Leave P2p {groupId} {sessionId} {masterSessionId}");
                    }
                }
                break;

                case PacketProperty.ChangeP2pMaster:
                {
                }
                break;

                case PacketProperty.Request:
                {
                    _request.OnReceive(poolingPacket.Property, poolingPacket.DeliveryMethod, reader, OnRequestReceive).Wait();
                }
                break;

                case PacketProperty.ViewRequest:
                {
                    _request.OnReceive(poolingPacket.Property, poolingPacket.DeliveryMethod, reader, OnViewRequestReceive).Wait();
                }
                break;

                default:
                    return(false);
                }
            }
            catch (Exception ex)
            {
                OnErrored?.Invoke(ex);
                return(true);
            }

            return(true);
        }