public static void Start() { if (_started) { return; } _started = true; NetworkComms.DisableLogging(); var serializer = DPSManager.GetDataSerializer <ProtobufSerializer>(); NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(serializer, NetworkComms.DefaultSendReceiveOptions.DataProcessors, NetworkComms.DefaultSendReceiveOptions.Options); PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.UDPBroadcast); NetworkComms.AppendGlobalIncomingPacketHandler <AndroidInfo>(AndroidInfo.GetHeader(), AndroidHandler.HandShakeHandler); NetworkComms.AppendGlobalIncomingPacketHandler <StudentInfoRequest>(StudentInfoRequest.GetHeader(), AndroidHandler.StudentInfoRequested); NetworkComms.AppendGlobalIncomingPacketHandler <EndPointInfo>(EndPointInfo.GetHeader(), HandShakeHandler); NetworkComms.AppendGlobalIncomingPacketHandler <GetWork>(GetWork.GetHeader(), GetWorkHandler); NetworkComms.AppendGlobalIncomingPacketHandler <Login>(Login.GetHeader(), LoginHandler); NetworkComms.AppendGlobalIncomingPacketHandler <Logout>(Logout.GetHeader(), LogoutHandler); NetworkComms.AppendGlobalIncomingPacketHandler <SchedulesRequest>(SchedulesRequest.GetHeader(), AndroidHandler.ScheduleRequestHandler); NetworkComms.AppendGlobalIncomingPacketHandler <EnrollRequest>(EnrollRequest.GetHeader(), AndroidHandler.EnrollRequestHandler); NetworkComms.AppendGlobalIncomingPacketHandler <RegisterStudent>(RegisterStudent.GetHeader(), AndroidHandler.RegisterStudentHandler); NetworkComms.AppendGlobalIncomingPacketHandler <SaveWork>(SaveWork.GetHeader(), SaveWorkHandler); NetworkComms.AppendGlobalIncomingPacketHandler <GetCourses>(GetCourses.GetHeader(), AndroidHandler.GetCoursesHandler); NetworkComms.AppendGlobalIncomingPacketHandler <GetCoursesDesktop>(GetCoursesDesktop.GetHeader(), GetCoursesHandler); NetworkComms.AppendGlobalIncomingPacketHandler <Pong>(Pong.GetHeader(), PongHandler); NetworkComms.AppendGlobalIncomingPacketHandler <EnrollStudent>(EnrollStudent.GetHeader(), EnrollStudentHandler); NetworkComms.AppendGlobalIncomingPacketHandler <StartEnrollment>(StartEnrollment.GetHeader(), AndroidHandler.StartEnrollmentHandler); NetworkComms.AppendGlobalIncomingPacketHandler <AddSchedule>(AddSchedule.GetHeader(), AndroidHandler.AddScheduleHandler); NetworkComms.AppendGlobalIncomingPacketHandler <CommitEnrollment>(CommitEnrollment.GetHeader(), AndroidHandler.CommitEnrollmentHandler); NetworkComms.AppendGlobalIncomingPacketHandler <StatusRequest>(StatusRequest.GetHeader(), AndroidHandler.StatusRequestHandler); NetworkComms.AppendGlobalIncomingPacketHandler <CancelEnrollment>(CancelEnrollment.GetHeader(), AndroidHandler.CancelEnrollmentHandler); // try // { Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 0), true); // } // catch (Exception e) // { // // } }
public static async Task <SaveWorkResult> SaveWork(SaveWork work) { await FindServer(); if (Server == null) { return(null); } SaveWorkResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <SaveWorkResult>(SaveWorkResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(SaveWorkResult.GetHeader()); result = i; if (!i.Success) { Encoder = null; } }); await work.Send(new IPEndPoint(IPAddress.Parse(Server.IP), Server.Port)); var start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < 17) { if (result != null) { return(result); } await TaskEx.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(SaveWorkResult.GetHeader()); return(null); }
private static async void SaveWorkHandler(PacketHeader packetheader, Connection connection, SaveWork i) { var ip = ((IPEndPoint)connection.ConnectionInfo.RemoteEndPoint).Address.ToString(); var client = Client.Cache.FirstOrDefault(x => x.IP == ip); if (!(client?.IsEnabled ?? false)) { return; } client.IsOnline = true; client.LastHeartBeat = DateTime.Now; if (client.Encoder == null) { var res = new SaveWorkResult(); res.Success = false; res.ErrorMessage = "Request Denied"; await res.Send(new IPEndPoint(IPAddress.Parse(client.IP), client.Port)); return; } TerminalLog.Add(client.Id, "Enrollment item completed."); var req = Request.Cache.FirstOrDefault(x => string.Equals(x.StudentId, i.StudentId, StringComparison.CurrentCultureIgnoreCase)); if (req == null) { return; } foreach (var sched in i.ClassSchedules) { var s = RequestDetail.Cache.FirstOrDefault(x => x.RequestId == req.Id && x.ScheduleId == sched.ClassId); if (s == null) { continue; } var stat = Request.Statuses.Pending; switch (sched.EnrollmentStatus) { case ScheduleStatuses.Accepted: stat = Request.Statuses.Accepted; break; case ScheduleStatuses.Conflict: stat = Request.Statuses.Conflict; break; case ScheduleStatuses.Closed: stat = Request.Statuses.Closed; break; } if (stat > req.Status) { req.Status = stat; } s.Update(nameof(s.Status), stat); } req.EncoderId = client.Encoder.Id; req.Save(); client.Encoder.EndWork(); var result = new SaveWorkResult { Success = true, WorkCount = Request.Cache.Count(x => x.Status > Request.Statuses.Proccessing && x.EncoderId == client.Encoder.Id), BestTime = TimeSpan.FromSeconds(client.Encoder.BestTime).ToString(), AverageTime = TimeSpan.FromSeconds(client.Encoder.AverageTime).ToString() }; await result.Send(new IPEndPoint(IPAddress.Parse(client.IP), client.Port)); await SendEncoderUpdates(Client.Cache.ToList()); }