示例#1
0
        /// <summary>
        /// 서버와 접속을 해제하고 리소스를 해제한다
        /// </summary>
        public void Close()
        {
            OnSessionClosed();

            try
            {
                _cts.Cancel();
            }
            catch
            {
            }

            NetPacket poolingPacket = null;

            while (_receivedPacketQueue.TryDequeue(out poolingPacket) == true &&
                   poolingPacket != null)
            {
                NetPool.PacketPool.Free(poolingPacket);
            }

            try
            {
                _tcpChannel?.Close();
                _udpChannel?.Close();

                _p2pGroup?.Close();
                _p2pGroup = null;

                if (_connectUdpLoopTask != null)
                {
                    _connectUdpLoopTask = null;
                }

                _udpSocket?.Close(false);
                _udpSocket = null;

                _request.Close();
            }
            catch
            {
            }

            _socket         = null;
            _isUdpConnected = false;
        }
示例#2
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);
        }
示例#3
0
 public P2pMember(P2pGroup p2pGroup)
 {
     _p2pGroup = p2pGroup;
     Logger    = p2pGroup.NetClient.LoggerFactory.CreateLogger(nameof(P2pMember));
 }