private void CreateSession(Packet p) { bool foundOpening = false; uint recId; _sessionLock.EnterUpgradeableReadLock(); // Try to find the first open slot for (recId = 0; recId < _sessions.Length; recId++) { if (_sessions[recId] == null) { foundOpening = true; break; } } // Create our session, regardless of if we found one, we'll need it to send a failure response ServerNetworkSession session = null; if (Type == NetworkType.Server) { session = new ServerNetworkSession(p.IPAddress, recId); } else // Client Session // We didn't find a open slot. Reject with error. if (!foundOpening) { session.SendError(); session = null; } // We found a session with the same IP and Port. Reject with error. else if (_sessions.FirstOrDefault(x => x.IPAddress == p.IPAddress) != null) { session.SendError(); session = null; } if (session != null) { _sessionLock.EnterWriteLock(); _sessions[recId] = session; _sessionLock.ExitWriteLock(); } _sessionLock.ExitUpgradeableReadLock(); if (session != null) { SessionCreated?.Invoke(this, new SessionCreatedEventArgs(session)); session.ProcessPacket(p); } }
public SessionCreatedEventArgs(ServerNetworkSession session) { Session = session; }