public static void SetKeepAlive(this Socket handler, bool on = true) { var keepAliveInterval_InMilliseconds = ConfigManager.GetValue <int>("KeepAliveInterval_InMilliseconds", 15000).Value; var keepAliveTime_InMilliseconds = ConfigManager.GetValue <int>("KeepAliveTime_InMilliseconds", 15000).Value; var size = Marshal.SizeOf(new uint()); if (Environment.OSVersion.Platform == PlatformID.Unix) { setsockopt((int)handler.Handle, /* SOL_SOCKET */ 0x01, /* SO_KEEPALIVE */ 0x09, BitConverter.GetBytes(on ? (int)1 : (int)0), size); setsockopt((int)handler.Handle, /* IPPROTO_TCP */ 0x06, /* TCP_KEEPIDLE */ 0x04, BitConverter.GetBytes(keepAliveInterval_InMilliseconds), size); setsockopt((int)handler.Handle, /* IPPROTO_TCP */ 0x06, /* TCP_KEEPINTVL */ 0x05, BitConverter.GetBytes(keepAliveInterval_InMilliseconds), size); } else { var inOptionValues = new byte[size * 3]; BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0); BitConverter.GetBytes((uint)keepAliveTime_InMilliseconds).CopyTo(inOptionValues, size); BitConverter.GetBytes((uint)keepAliveInterval_InMilliseconds).CopyTo(inOptionValues, size * 2); handler.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); } }
public static Int16 FindRoomID(this ThePalaceEntities dbContext, Int16 roomID = (Int16)0, bool isAuthorized = false) { var maxRoomOccupancy = ConfigManager.GetValue <int>("MaxRoomOccupancy", 45); var entryRoomIDKeys = ServerState.entryRoomIDs.ToArray(); var roomCacheKeys = ServerState.roomsCache.Keys.ToArray(); var fullRoomIDs = new List <Int16>(); var roomIDs = dbContext.Rooms.AsNoTracking() .OrderBy(r => r.OrderID) .Select(r => r.RoomId) .ToList(); Func <bool> findRoom = () => { if (roomID != 0) { var roomCount = isAuthorized ? 0 : SessionManager.GetRoomUserCount(roomID); try { if ( isAuthorized || (entryRoomIDKeys.Contains(roomID) && !roomCacheKeys.Contains(roomID)) || (roomCacheKeys.Contains(roomID) && ServerState.roomsCache[roomID].MaxOccupancy == 0 && roomCount < maxRoomOccupancy) || (roomCacheKeys.Contains(roomID) && (ServerState.roomsCache[roomID].MaxOccupancy > 0) && roomCount < ServerState.roomsCache[roomID].MaxOccupancy)) { return(true); } } catch (Exception ex) { ex.DebugLog(); } fullRoomIDs.Add(roomID); } return(false); }; if (roomID > 0) { if (findRoom()) { return(roomID); } } while (true) { entryRoomIDKeys = ServerState.entryRoomIDs .Where(r => !fullRoomIDs.Contains(r)) .ToArray(); roomCacheKeys = ServerState.roomsCache.Keys .Where(r => !fullRoomIDs.Contains(r)) .ToArray(); if (entryRoomIDKeys.Length > 0) { roomID = entryRoomIDKeys[RndGenerator.NextSecure((uint)entryRoomIDKeys.Length)]; if (findRoom()) { return(roomID); } } else if (roomCacheKeys.Length > 0) { roomID = roomCacheKeys[RndGenerator.NextSecure((uint)roomCacheKeys.Length)]; if (findRoom()) { return(roomID); } } else { roomID = roomIDs .Where(ID => !fullRoomIDs.Contains(ID)) .FirstOrDefault(); if (roomID == 0) { break; } else { var room = dbContext.GetRoom(roomID); if (!room.NotFound) { if (findRoom()) { return(roomID); } } } } } return(0); }