Exemple #1
0
        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);
        }
Exemple #2
0
        /// <summary>
        /// Letar efter server och upprättar en anslutning
        /// </summary>
        private void ServerFinder()
        {
            //Broadcasta ping-meddelande
            client = new UdpClient();
            ipep   = new IPEndPoint(IPAddress.Any, 0);
            client.EnableBroadcast = true;

            SimhoppMessage msg = SendReceive(SimhoppMessage.PingMessage(), true);

            switch (msg.Action)
            {
            case SimhoppMessage.ClientAction.List:
                PopulateJudgeList(msg);
                break;
            }

            LogMessage(msg);

            //Klient-tråd
            //Tar emot meddelanden från servern
            while (true)
            {
                try
                {
                    Thread.Sleep(100);

                    msg = SendReceive();

                    if (msg == null)
                    {
                        continue;
                    }

                    switch (msg.Action)
                    {
                    case SimhoppMessage.ClientAction.List:
                        PopulateJudgeList(msg);
                        break;

                    case SimhoppMessage.ClientAction.NotAccepted:
                        NotAccepted(msg);
                        break;

                    case SimhoppMessage.ClientAction.AssignId:
                        AssignLogin(msg);
                        break;

                    case SimhoppMessage.ClientAction.SubmitScore:
                        if (Presenter != null)
                        {
                            Presenter.SubmitClientScore(msg.Value, msg.Id, msg.Status.RoundIndex, msg.Status.DiverIndex);
                        }
                        break;

                    case SimhoppMessage.ClientAction.RequestScore:
                        if (Presenter != null)
                        {
                            Presenter.ScoreRequested(msg);
                        }
                        break;

                    case SimhoppMessage.ClientAction.StatusUpdate:
                        if (Presenter != null)
                        {
                            Presenter.StatusUpdated(msg);
                        }
                        break;

                    case SimhoppMessage.ClientAction.ServerTerminating:
                        if (Presenter != null)
                        {
                            Presenter.Close(true);
                        }
                        break;
                    }

                    LogMessage(msg);
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(ex);
                }
            }
        }