public static void KickJudge(int judgeIndex) { try { _judges.Remove(judgeIndex); _presenter.LogoutClient(judgeIndex); Console.WriteLine("Kick index: " + judgeIndex); foreach (IPEndPoint ipep in _judgeClients.Keys) { Console.WriteLine("ipep: " + ipep.ToString()); Console.WriteLine("Index: " + _judgeClients[ipep]); if (_judgeClients[ipep] == judgeIndex) { //Skapa terminate message... SimhoppMessage msg; msg = new SimhoppMessage(-2, SimhoppMessage.ClientAction.ServerTerminating); var sendData = Encoding.ASCII.GetBytes(msg.Serialize()); //...och skicka _server.Send(sendData, sendData.Length, ipep); //Och ta bort den _judgeClients.Remove(ipep); //Sen break break; } } } catch(Exception ex) { /* Ignore */ } }
/// <summary> /// Inloggnings-svar från servern. Loggar in som vald domare /// </summary> /// <param name="msg"></param> public void AssignLogin(SimhoppMessage msg) { EventPresenter presenter = new EventPresenter(null, msg.Status.Contest); presenter.SetMode(EventPresenter.ViewMode.Client, (int)msg.Value, judgeClient, this.Icon); Console.WriteLine("Assigning login: " + msg.Serialize()); judgeClient.Presenter = presenter; this.Hide(); presenter.ShowView(); }
public void SendLogout(int judgeIndex) { try { SimhoppMessage msg = new SimhoppMessage(judgeIndex, SimhoppMessage.ClientAction.Logout, ""); byte[] data = Encoding.ASCII.GetBytes(msg.Serialize()); client.Send(data, data.Length, ipep); _view.Close(); } catch (Exception ex) { ExceptionHandler.Handle(ex); } }
private SimhoppMessage SendReceive(SimhoppMessage msg = null, bool broadcast = false) { byte[] data; string responseData; SimhoppMessage responseMsg; if (msg == null && !Messages.TryDequeue(out msg)) { try { client.Client.ReceiveTimeout = 1000; data = client.Receive(ref ipep); responseData = Encoding.ASCII.GetString(data); responseMsg = SimhoppMessage.Deserialize(responseData); return responseMsg; } catch (Exception) { return null; } } if (msg == null) return null; data = Encoding.ASCII.GetBytes(msg.Serialize()); int tries = 0; while (true) { tries++; try { //Öka timeout för vaje försök client.Client.ReceiveTimeout = 1000 + Math.Min(tries * 100, 10000); if (broadcast) { data = Encoding.ASCII.GetBytes(SimhoppMessage.PingMessage().Serialize()); client.Send(data, data.Length, new IPEndPoint(IPAddress.Broadcast, 60069)); } else { client.Send(data, data.Length, ipep); } data = client.Receive(ref ipep); break; } catch (SocketException ex) { LogMessage(SimhoppMessage.ErrorMessage(Encoding.ASCII.GetString(data))); ExceptionHandler.Handle(ex); } } responseData = Encoding.ASCII.GetString(data); responseMsg = SimhoppMessage.Deserialize(responseData); return responseMsg; }
private SimhoppMessage SendReceive(SimhoppMessage msg = null, bool broadcast = false) { byte[] data; string responseData; SimhoppMessage responseMsg; if (msg == null && !Messages.TryDequeue(out msg)) { try { client.Client.ReceiveTimeout = 1000; data = client.Receive(ref ipep); responseData = Encoding.ASCII.GetString(data); responseMsg = SimhoppMessage.Deserialize(responseData); return(responseMsg); } catch (Exception) { return(null); } } if (msg == null) { return(null); } data = Encoding.ASCII.GetBytes(msg.Serialize()); int tries = 0; while (true) { tries++; try { //Öka timeout för vaje försök client.Client.ReceiveTimeout = 1000 + Math.Min(tries * 100, 10000); if (broadcast) { data = Encoding.ASCII.GetBytes(SimhoppMessage.PingMessage().Serialize()); client.Send(data, data.Length, new IPEndPoint(IPAddress.Broadcast, 60069)); } else { client.Send(data, data.Length, ipep); } data = client.Receive(ref ipep); break; } catch (SocketException ex) { LogMessage(SimhoppMessage.ErrorMessage(Encoding.ASCII.GetString(data))); ExceptionHandler.Handle(ex); } } responseData = Encoding.ASCII.GetString(data); responseMsg = SimhoppMessage.Deserialize(responseData); return(responseMsg); }
public void LogMessage(SimhoppMessage msg) { textBoxLog.Text = msg.Serialize() + "\r\n" + textBoxLog.Text; }
private static void UdpListener() { try { _server = new UdpClient(60069); //60069 _server.EnableBroadcast = true; } catch (Exception ex) { ExceptionHandler.Handle(ex); } while ( true ) { try { if (_server == null) return; //Vänta på broadcast från klient IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 60069); //Ta emot msg från klienten byte[] data = _server.Receive(ref ipep); string receiveMessage = Encoding.ASCII.GetString(data, 0, data.Length); SimhoppMessage msg = SimhoppMessage.Deserialize(receiveMessage); SimhoppMessage response; switch (msg.Action) { case SimhoppMessage.ClientAction.Ping: response = SendContestStatus(); break; case SimhoppMessage.ClientAction.Login: if (_presenter.NewConnections) response = AssignIdToJudge(msg, ipep); else response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.NotAccepted); break; case SimhoppMessage.ClientAction.SubmitScore: SubmitScore(msg); response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.Confirm); break; case SimhoppMessage.ClientAction.Logout: LogoutClient(msg); response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.Confirm); break; default: response = SimhoppMessage.ErrorMessage("Not implemented"); break; } //Svara var responseData = Encoding.ASCII.GetBytes(response.Serialize()); _server.Send(responseData, responseData.Length, ipep); } catch (Exception ex) { ExceptionHandler.Handle(ex); } } }