public static async Task <GetCoursesResult> GetCoursesDesktop() { await FindServer(); if (Server == null) { return(null); } GetCoursesResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <GetCoursesResult>(GetCoursesResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(GetCoursesResult.GetHeader()); result = i; }); await new GetCoursesDesktop().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(GetCoursesResult.GetHeader()); return(null); }
public Boolean CloseServer() { if (runState == State.Server || runState == State.None) { foreach (Player player in playerList.GetPlayers()) { try { NetworkComms.SendObject("ServerStopped", player.IP, player.Port, new Player("Server", -1)); Thread.Sleep(SLEEP_TIME); } catch (Exception e) { Console.WriteLine("[CloseServer] " + e.Message); } } // if (MainGame.Instance.HostOnly) baseClass.ServerStopped(); playerList.ClearPlayers(); Connection.StopListening(); NetworkComms.RemoveGlobalIncomingPacketHandler(); NetworkComms.CloseAllConnections(); // NetworkComms.Shutdown (); baseClass.NetworkStopped(); return(true); } else { return(false); } }
/// <summary> /// Removes all public and private RPC objects and removes all related packet handlers from NetworkComms /// </summary> public static void ShutdownAllRPC() { lock (locker) { //We first remove all objects and the comms handlers specifically associated with them. This will include all connection specific handlers RemoveRPCObjects(RPCObjectsById.Keys.ToList()); // foreach (var handlerPair in newConnectionByNewInstanceHandlers) { string name = handlerPair.Key.Name + "-NEW-INSTANCE-RPC-CONNECTION"; NetworkComms.RemoveGlobalIncomingPacketHandler(name, handlerPair.Value as NetworkComms.PacketHandlerCallBackDelegate <string>); } foreach (var handlerPair in newConnectionByNameHandlers) { string name = handlerPair.Key.Name + "-NEW-RPC-CONNECTION-BY-NAME"; NetworkComms.RemoveGlobalIncomingPacketHandler(name, handlerPair.Value as NetworkComms.PacketHandlerCallBackDelegate <string>); } foreach (var handlerPair in newConnectionByIdHandlers) { string name = handlerPair.Key.Name + "-NEW-RPC-CONNECTION-BY-ID"; NetworkComms.RemoveGlobalIncomingPacketHandler(name, handlerPair.Value as NetworkComms.PacketHandlerCallBackDelegate <string>); } newConnectionByNewInstanceHandlers = new Dictionary <Type, Delegate>(); newConnectionByNameHandlers = new Dictionary <Type, Delegate>(); newConnectionByIdHandlers = new Dictionary <Type, Delegate>(); } }
private async Task <SchedulesResult> _GetSchedules(string subject) { if (string.IsNullOrWhiteSpace(_subjectRequested) && _subjectRequested != subject) { NetworkComms.RemoveGlobalIncomingPacketHandler($"{SchedulesResult.GetHeader()}{_subjectRequested}"); } if (string.IsNullOrWhiteSpace(subject)) { return(null); } _subjectRequested = subject; if (Server == null) { await FindServer(); } if (Server == null) { return(null); } if (_subjectRequested != subject) { return(null); } SchedulesResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <SchedulesResult>(SchedulesResult.GetHeader() + subject, (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(SchedulesResult.GetHeader() + subject); result = i; }); await new SchedulesRequest() { SubjectCode = subject, StudentId = CurrentStudent.Id } .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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(SchedulesResult.GetHeader() + subject); return(null); }
/// <summary> /// Stop listening for new connections /// </summary> public static void StopServerListener() { //Stop listening for connections Connection.StopListening(); //Remove the handler that called this function NetworkComms.RemoveGlobalIncomingPacketHandler <string>("Initialize-Connection", GetConnection); ServerListening = false; }
private async Task <CommitEnrollmentResult> _CommitEnrollment() { if (CurrentStudent == null) { return(null); } if (TransactionId == 0) { return(null); } if (Server == null) { await FindServer(); } if (Server == null) { return(null); } CommitEnrollmentResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <CommitEnrollmentResult>(CommitEnrollmentResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(CommitEnrollmentResult.GetHeader()); if (i.Success) { RequestStatus = i.RequestStatus; } result = i; }); await new CommitEnrollment() { ClassIds = ClassSchedules.Select(x => x.ClassId).ToList(), StudentId = CurrentStudent.StudentId, TransactionId = TransactionId }.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(CommitEnrollmentResult.GetHeader()); return(null); }
public static async Task <EnrollResult> Enroll(string studentId, List <ClassSchedule> schedules) { if (schedules == null || schedules.Count == 0) { return(null); } if (Server == null) { await FindServer(); } if (Server == null) { return(new EnrollResult(ResultCodes.Offline)); } var request = new EnrollRequest() { StudentId = studentId, ClassSchedules = schedules }; EnrollResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <EnrollResult>(EnrollResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(EnrollResult.GetHeader()); result = i; }); await request.Send(new IPEndPoint(IPAddress.Parse(Server.IP), Server.Port)); // foreach (var sched in schedules) // { // sched.Sent = true; // await Db.Save(sched); // } var start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < 17) { if (result != null) { return(result); } await Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(EnrollResult.GetHeader()); return(new EnrollResult(ResultCodes.Timeout)); }
public static async Task <CancelEnrollmentResult> CancelEnrollment() { if (CurrentStudent == null) { return(null); } if (RequestStatus == null) { return(null); } if (Server == null) { await FindServer(); } if (Server == null) { return(null); } var request = new CancelEnrollment() { StudentId = CurrentStudent.Id, RequestId = RequestStatus.Id, }; CancelEnrollmentResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <CancelEnrollmentResult>(CancelEnrollmentResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(CancelEnrollmentResult.GetHeader()); if (i.Success) { RequestStatus.IsSubmitted = false; } result = i; }); await request.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(CancelEnrollmentResult.GetHeader()); return(null); }
/// <summary> /// Initializes RPC. Called when receiving a "Initialize-Connection" message /// </summary> static void InitializeServerRPC(PacketHeader header, Connection connection, string message) { //Add a event handler to handle a shutdown of the connection connection.AppendShutdownHandler(new NetworkComms.ConnectionEstablishShutdownDelegate((c) => MDI_Container.staticMdi_Container.BeginInvoke(MDI_Container.staticMdi_Container.DLostConnection))); //Remove the handler that called this function NetworkComms.RemoveGlobalIncomingPacketHandler <string>("Initialize-Connection", InitializeServerRPC); //Get the servers RPC interface RemoteServerInterface = RemoteProcedureCalls.Client.CreateProxyToPublicNamedInstance <IServerInterface>(connection, "Server", out RPCInstanceID, new SendReceiveOptions <ProtobufSerializer>()); MDI_Container.staticMdi_Container.mdi_Join.Invoke(MDI_Forms.MDI_Join.DJoinResult, new object[] { true }); }
public void Closeconn() { //newUDPConn.CloseConnection(false); NetworkComms.CloseAllConnections(); NetworkComms.RemoveGlobalIncomingPacketHandler <byte[]>("icecream", Unamangedbytevoice); NetworkComms.RemoveGlobalIncomingPacketHandler(); Debug.WriteLine(NetworkComms.GlobalIncomingPacketHandlerExists("icecream")); NetworkComms.Shutdown(); }
/// <summary> /// Closes any listening connections if exists and removes handler for hello command. /// </summary> public void Close() { NetworkComms.RemoveGlobalIncomingPacketHandler("Discover"); __listeners.ToList().ForEach(@base => { try { Connection.StopListening(@base); } catch (Exception) {} }); __listeners.RemoveWhere(@base => [email protected]); }
private async Task <StartEnrollmentResult> _StartEnrollment(string receipt) { if (CurrentStudent == null) { return(null); } if (string.IsNullOrWhiteSpace(receipt)) { return(null); } if (Server == null) { await FindServer(); } if (Server == null) { return(null); } StartEnrollmentResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <StartEnrollmentResult>(StartEnrollmentResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(StartEnrollmentResult.GetHeader()); ClassSchedules = i.ClassSchedules; TransactionId = i.TransactionId; EnrollmentCommited = i.Submitted; result = i; }); await new StartEnrollment() { Receipt = receipt, StudentId = CurrentStudent.StudentId }.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(StartEnrollmentResult.GetHeader()); return(null); }
protected virtual void RestartAsHost(EventArgs e) { EventHandler handler = RestartAsHostEventHandler; NetworkComms.RemoveGlobalIncomingPacketHandler(); NetworkComms.CloseAllConnections(); if (handler != null) { handler(this, e); } }
private void MidiEventListener_DoWork(object sender, DoWorkEventArgs e) { if ("Midi events from network".Equals(inputDeviceName, System.StringComparison.InvariantCulture)) { try { NetworkComms.AppendGlobalIncomingPacketHandler <byte[]>("midiEvent", OnNetworkEvent); Connection.StartListening(ConnectionType.TCP, new System.Net.IPEndPoint(System.Net.IPAddress.Any, Int32.Parse(MidiForwarder.Properties.Settings.Default.receivePort))); listening = true; while (listeningMidiEvents) { System.Threading.Thread.Sleep(250); } NetworkComms.RemoveGlobalIncomingPacketHandler <byte[]>("midiEvent", OnNetworkEvent); } catch (CommsException ce) { // set connection to red MessageBox.Show("CommsException occurs : " + ce.Message); } finally { try { NetworkComms.Shutdown(); } catch (CommsException ce) { Console.WriteLine("CommsException occurs : " + ce.ToString()); } finally { listening = false; } } } else if (inputDeviceName != null) { using (var inputDevice = InputDevice.GetByName(inputDeviceName)) { inputDevice.EventReceived += OnEventReceived; inputDevice.StartEventsListening(); while (listeningMidiEvents) { System.Threading.Thread.Sleep(250); } inputDevice.StopEventsListening(); inputDevice.EventReceived -= OnEventReceived; } } }
//public NetManager(string passMessage, string passAddress, bool peerSend, int peerExitCode) //{ // peerMessage = passMessage; // peerAddress = passAddress; // sendMessage = peerSend; // exitCode = peerExitCode; //} public void netShutdown() { MessagesClass netClose = new MessagesClass(); netClose.netShutdown1(); NetworkComms.RemoveGlobalIncomingPacketHandler(); //exitCode = 1; NetworkComms.Shutdown(); }
public async Task <StatusResult> GetStatus() { if (Server == null) { await FindServer(); } if (Server == null) { return(null); } var request = new StatusRequest() { StudentId = CurrentStudent?.Id ?? 0, Receipt = RequestStatus?.Receipt, RequestId = RequestStatus?.Id ?? 0, }; StatusResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <StatusResult>(StatusResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(StatusResult.GetHeader()); if (i.Success) { RequestStatus = i.RequestStatus; ClassSchedules = i.ClassSchedules; } result = i; }); await request.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(StatusResult.GetHeader()); return(null); }
public static void RegisterListener(string messageName, Action <string> callback, bool keepExistingListeners = false) { if (!keepExistingListeners) { NetworkComms.RemoveGlobalIncomingPacketHandler(); } NetworkComms.AppendGlobalIncomingPacketHandler <string>(messageName, (packetHeader, connection, incomingSring) => { callback(incomingSring); connection.SendObject(messageName.ToUpper(), Serialize(new ResponseWrapper())); }); }
private async Task <StudentInfoResult> _GetStudentInfo(string studentId, string password) { if (Server == null) { await FindServer(); } if (Server == null) { return(null); } var request = new StudentInfoRequest() { StudentId = studentId, Password = password }; StudentInfoResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <StudentInfoResult>(StudentInfoResult.GetHeader(), (h, c, res) => { NetworkComms.RemoveGlobalIncomingPacketHandler(StudentInfoResult.GetHeader()); result = res; if (result?.Success ?? false) { CurrentStudent = res.Student; RequestStatus = res.RequestStatus; } }); await request.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(StudentInfoResult.GetHeader()); return(null); }
private async Task <AddScheduleResult> _AddSchedule(ClassSchedule schedule) { if (CurrentStudent == null) { return(null); } if (Server == null) { await FindServer(); } if (Server == null) { return(null); } AddScheduleResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <AddScheduleResult>(AddScheduleResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(AddScheduleResult.GetHeader()); result = i; }); await new AddSchedule() { ClassId = schedule.ClassId, StudentId = CurrentStudent.StudentId, TransactionId = TransactionId }.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(AddScheduleResult.GetHeader()); return(null); }
public Boolean CloseClient() { if (runState == State.Client || runState == State.None) { playerList.ClearPlayers(); Connection.StopListening(); NetworkComms.RemoveGlobalIncomingPacketHandler(); NetworkComms.CloseAllConnections(); // NetworkComms.Shutdown (); baseClass.NetworkStopped(); return(true); } else { return(false); } }
public static async Task <bool> Ping(IPEndPoint ep) { var head = $"PONG{ep.Address}"; var pong = false; NetworkComms.AppendGlobalIncomingPacketHandler <string>(head, (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(head); pong = true; }); var sent = false; while (!sent) { try { UDPConnection.SendObject("PING", "https://github.com/awooo-ph", ep, NetworkComms.DefaultSendReceiveOptions, ApplicationLayerProtocolStatus.Enabled); sent = true; break; } catch (Exception) { await TaskEx.Delay(100); } } var start = DateTime.Now; while ((DateTime.Now - start).TotalMilliseconds < 4710) { if (pong) { return(pong); } await TaskEx.Delay(TimeSpan.FromMilliseconds(100)); } return(pong); }
public static async Task <LoginResult> Login(string username, string password) { await FindServer(); if (Server == null) { return(new LoginResult(ResultCodes.Offline)); } var login = new Login() { Username = username, Password = password }; LoginResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <LoginResult>(LoginResult.GetHeader(), (h, c, res) => { NetworkComms.RemoveGlobalIncomingPacketHandler(LoginResult.GetHeader()); result = res; Encoder = res?.Encoder; }); await login.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(LoginResult.GetHeader()); return(new LoginResult(ResultCodes.Timeout)); }
private async Task <Courses> _GetCourses() { if (_courses != null) { return(_courses); } if (Server == null) { await FindServer(); } if (Server == null) { return(null); } var request = new GetCourses(); Courses result = null; NetworkComms.AppendGlobalIncomingPacketHandler <Courses>(Courses.GetHeader(), (h, c, r) => { NetworkComms.RemoveGlobalIncomingPacketHandler(Courses.GetHeader()); result = r; _courses = r; }); await request.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 Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(Courses.GetHeader()); return(null); }
private async Task <ResultCodes> _Register(Student student) { if (Server == null) { await FindServer(); } if (Server == null) { return(ResultCodes.Offline); } RegisterStudentResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <RegisterStudentResult>(RegisterStudentResult.GetHeader(), async(h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(RegisterStudentResult.GetHeader()); student.Id = i.StudentId; await Db.Save(student); result = i; }); await new RegisterStudent(student).Send(new IPEndPoint(IPAddress.Parse(Server.IP), Server.Port)); var start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < 17) { if (result != null) { return(result.Result); } await Task.Delay(TimeSpan.FromSeconds(1)); } Server = null; NetworkComms.RemoveGlobalIncomingPacketHandler(RegisterStudentResult.GetHeader()); return(ResultCodes.Timeout); }
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); }
public static async Task <GetWorkResult> GetNextWork(string username = "") { await FindServer(); if (Server == null) { return(new GetWorkResult(ResultCodes.Offline)); } var request = new GetWork(username); GetWorkResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <GetWorkResult>(GetWorkResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(GetWorkResult.GetHeader()); result = i; }); await request.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(GetWorkResult.GetHeader()); return(new GetWorkResult(ResultCodes.Timeout)); }
protected override void OnDestroy() { NetworkComms.RemoveGlobalIncomingPacketHandler(StatusResult.GetHeader() + "update", _statusUpdateHandler); base.OnDestroy(); }
/// <summary> /// Closes Handlers for remote code execution on this machine /// </summary> public static void Stop() { NetworkComms.RemoveGlobalIncomingPacketHandler("RemoteExecution"); NetworkComms.RemoveGlobalIncomingPacketHandler("RemoteReturnExecution"); }
public static void RemoveMessageListener <T>(string mType, NetworkComms.PacketHandlerCallBackDelegate <T> onReceived) { NetworkComms.RemoveGlobalIncomingPacketHandler(mType, onReceived); }