public static CrowdRequest Recieve(ControlClient client, Socket socket)
        {
            byte[] buf     = new byte[RECV_BUF];
            string content = "";
            int    read    = 0;

            do
            {
                if (!client.IsRunning())
                {
                    return(null);
                }

                if (socket.Poll(RECV_TIME, SelectMode.SelectRead))
                {
                    read = socket.Receive(buf);
                    if (read < 0)
                    {
                        return(null);
                    }

                    content += Encoding.ASCII.GetString(buf);
                }
                else
                {
                    CrowdResponse.KeepAlive(socket);
                }
            } while (read == 0 || (read == RECV_BUF && buf[RECV_BUF - 1] != 0));

            return(JsonConvert.DeserializeObject <CrowdRequest>(content));
        }
示例#2
0
        public void RequestLoop()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            while (Running)
            {
                try
                {
                    while (Saving || Game1.isTimePaused)
                    {
                        Thread.Yield();
                    }

                    CrowdRequest req = null;
                    lock (Requests)
                    {
                        if (Requests.Count == 0)
                        {
                            continue;
                        }
                        req = Requests.Dequeue();
                    }

                    string code = req.GetReqCode();
                    try
                    {
                        CrowdResponse res = Delegate[code](this, req);
                        if (res == null)
                        {
                            new CrowdResponse(req.GetReqID(), CrowdResponse.Status.STATUS_FAILURE, $"Request error for '{code}'").Send(Socket);
                        }

                        res.Send(Socket);
                    }
                    catch (KeyNotFoundException)
                    {
                        new CrowdResponse(req.GetReqID(), CrowdResponse.Status.STATUS_FAILURE, $"Request error for '{code}'").Send(Socket);
                    }
                }
                catch (Exception)
                {
                    UI.ShowError("Disconnected from Crowd Control");
                    Socket.Close();
                }
            }
        }