private async Task HandleSession(ServerSession session) { _sessionManager.InsertSession(session); try { _logger.LogInformation($"A new session connected: {session.SessionId}"); session.OnSessionConnected(); OnSessionConnected?.Invoke(session); try { await session.RunAsync(); } catch (Exception e) { _logger.LogError(e, $"Failed to handle the session {session.SessionId}."); session.Close(); } if (_udpSocket != null) { _udpSocket.RemoveSession(session); } _logger.LogInformation($"The session disconnected: {session.SessionId}"); OnSessionClosed?.Invoke(session); await session.OnSessionClosed(); } catch (Exception e) { _logger.LogError(e, $"Failed to handle the session {session.SessionId}."); } finally { _sessionManager.RemoveSession(session); _sessionFactory.Release(session); } }
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); }