Beispiel #1
0
        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 */
            }
        }
Beispiel #2
0
        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);
                }
            }
        }