/// <summary> /// 启动服务器! /// </summary> /// <param name="message"></param> public async void Run(string message = "服务器运行起来了!") { if (Instance is null) { Instance = this; } else { Logger.Log("只允许同时启动一个服务器!", LogLevel.Warn); return; } // 处理未观察到的异常 TaskScheduler.UnobservedTaskException += (sender, eventArgs) => { eventArgs.SetObserved(); (eventArgs.Exception).Handle(e => { Logger.Log($"Exception type: {e.GetType()}\n{e}", LogLevel.Error); return(true); }); }; // 加载解析器 Parser = new Parser(RequestHandleAsync, ResponseHandleAsync); // 加载中间件 RequestMiddleware = new RequestMiddleware(this); ResponseMiddleware = new ResponseMiddleware(this); // 初始化连接池 与最大连接数保持一致 foreach (var i in Enumerable.Range(0, MaxConnection)) { FreeClients.Enqueue(new Client(this)); } // 初始化房间 foreach (var i in Enumerable.Range(0, MaxRoomNumbers)) { FreeRooms.Enqueue(new Room(i)); } Logger.Log($"初始化了{FreeClients.Count}个对等客户端"); // 设置请求队列的最大值 Listener.Start(MaxConnection); Logger.Log(message); // Test for (int i = 1; i < 3; i++) { var room = GetRoom(); room.Init(GetClient(), i, i * 2, $"TestRoom{i}"); } // // 异步接受客户端连接请求 await AcceptAsync(); }
public void RefillEverything() { CurrentRoomType = RoomTypes.Find(i => i.TypeId == completeCheckIn.RoomType.TypeId); Roominess = AvailableRoominess.Find(i => i.Number == completeCheckIn.Roominess); StartDate = completeCheckIn.CheckIn.StartDate; EndDate = completeCheckIn.CheckIn.EndDate; GetFreeRooms(); CurrentRoom = FreeRooms.Find(i => i.RoomId == completeCheckIn.CheckIn.RoomId); Services = dbCrud.GetAllServices().Select(i => new ServiceData(i)).ToList(); foreach (ServiceData service in completeCheckIn.Services) { Services.Find(i => i.ServiceId == service.ServiceId).NumberOfProvision = service.NumberOfProvision; } }
public Room GetRoom() { Room room; lock (FreeRooms) { room = FreeRooms.Any() ? FreeRooms.Dequeue() : null; } if (room != null) { lock (UsingRooms) { UsingRooms.Add(room); } } return(room); }
public ActionResult Index() // sets the defualy properties { FreeRooms FreeRoom = new FreeRooms(); // You can set default properties here (e.g. model.GuestNumbers = 2;) FreeRoom.BookingFrom = DateTime.Today.Date; FreeRoom.BookingTo = DateTime.Today.AddDays(1).Date; FreeRoom.GuestNumbers = "1"; //FreeRoom.RoomNum = " "; FreeRoom.Name = ""; FreeRoom.Email = ""; FreeRoom.Add(); FreeRoom.RoomDetails(); FreeRoom.combinelist(); ViewBag.error = " Now"; return(View(FreeRoom)); //sends the FreeRoom class to the form }
public void GetFreeRooms() { try { if (EndDate <= StartDate) { throw new Exception("Начальная дата должна быть меньше конечной"); } if (CurrentRoomType == null || Roominess == null) { throw new Exception(""); } Error = ""; FreeRooms = dbInfo.GetFreeRooms(StartDate, EndDate, CurrentRoomType.TypeName, Roominess.Number); if (FreeRooms == null) { FreeRooms = new List <RoomCheckInData>(); } if (lastRoom != null) { if (dbInfo.IsOldRoomFree(oldStart, oldEnd, StartDate, EndDate, lastRoom.RoomId, completeCheckIn.CheckIn.CheckInId, Roominess.Number, CurrentRoomType)) { FreeRooms.Add(lastRoom); FreeRooms = new List <RoomCheckInData>(FreeRooms); } } } catch (Exception e) { Error = e.Message; FreeRooms = null; return; } finally { SetPrices(); } }
[HttpPost] //run this method when its posting back information public ActionResult Index(FreeRooms FreeRoom, FreeRooms model) { if (!ModelState.IsValid) { ////mvc automatically checkes the state of any values coming in // // return the view to correct validation errors ViewBag.error = " Error"; FreeRoom.BookingFrom = DateTime.Today.Date; FreeRoom.BookingTo = DateTime.Today.Date.AddDays(1).Date; FreeRoom.GuestNumbers = "1"; return(View(FreeRoom)); } //sets the values if there are no errors // FreeRoom.BookingFrom = model.BookingFrom; // FreeRoom.BookingTo = model.BookingTo2; // FreeRoom.GuestNumbers = model.GuestNumbers; FreeRoom.Name = model.Name; FreeRoom.Email = model.Email; //FreeRoom.RoomNum = model.RoomNum; //FreeRoom.CostOfRoom = model.CostOfRoom; FreeRoom.Add(); FreeRoom.RoomDetails(); FreeRoom.combinelist(); ViewBag.error = " Working"; if (!string.IsNullOrWhiteSpace(FreeRoom.Name) && !string.IsNullOrWhiteSpace(FreeRoom.Email) && !string.IsNullOrWhiteSpace(model.RoomNumandCost)) // checks if there is text in name & email and if a room number is selected fore sending data { FreeRoom.NewBooking(); //need to add the booking before the guest FreeRoom.NewGuest(); } return(View(FreeRoom)); }
public void FreeRoom(Room room) { lock (UsingRooms) { UsingRooms.Remove(room); } lock (FreeRooms) { FreeRooms.Enqueue(room); } }