Example #1
0
        public Object WaitForEvent(string eventName)
        {
            while (true)
            {
                this.WaitForEvents();

                while (this.EventsStack.Count > 0)
                {
                    ServerMessages.ReceivedEvent <Object> receivedEvent = this.EventsStack.Pop();
                    if (eventName != null && receivedEvent.@event == eventName)
                    {
                        return(receivedEvent.data);
                    }
                    else
                    {
                        this.AutoHandle(receivedEvent.@event, receivedEvent.data);
                    }
                }
            }
        }
Example #2
0
        private void WaitForEvents()
        {
            if (this.EventsStack.Count > 0)
            {
                return; // as we already have events to handle, no need to wait for more
            }

            while (true)
            {
                String responseData = String.Empty;

                // Try to read the TcpServer response bytes.
                int bytes = -2;
                if (this.Stream.DataAvailable)
                {
                    try
                    {
                        bytes = Stream.Read(this.TempData, 0, this.TempData.Length);
                    }
                    catch (Exception exception)
                    {
                        ErrorHandler.HandleError(ErrorHandler.ErrorCode.CANNOT_READ_SOCKET, exception, "Cannot read socket while waiting for events.");
                    }
                }

                if (bytes == -2)
                {
                    continue; // as no bytes were read
                }

                responseData = System.Text.Encoding.ASCII.GetString(this.TempData, 0, bytes);

                if (this.PrintIO)
                {
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine("FROM SERVER --> " + responseData);
                    Console.ResetColor();
                }

                string   total = this.ReceivedBuffer + responseData;
                string[] split = total.Split(EOT_CHAR);

                this.ReceivedBuffer = split.Last();         // this is either an empty string because of the EOT_CHAR split, or an incomplete json string so store it in the buffer

                for (int i = split.Length - 2; i >= 0; i--) // iterate through in reverse, skipping the over the very last item because we stored it in the receivedBuffer
                {
                    JObject deserialized = null;
                    string  jsonStr      = split[i];
                    try
                    {
                        deserialized = JObject.Parse(jsonStr);
                    }
                    catch (Exception exception)
                    {
                        ErrorHandler.HandleError(ErrorHandler.ErrorCode.MALFORMED_JSON, exception, "Could not parse json '" + jsonStr + "'");
                    }

                    var receivedEvent = new ServerMessages.ReceivedEvent <Object>();
                    receivedEvent.@event = deserialized.GetValue("event").ToString();

                    switch (receivedEvent.@event)
                    {
                    case "lobbied":
                        receivedEvent.data = deserialized["data"].ToObject <ServerMessages.LobbiedData>();
                        break;

                    case "start":
                        receivedEvent.data = deserialized["data"].ToObject <ServerMessages.StartData>();
                        break;

                    case "order":
                        receivedEvent.data = deserialized["data"].ToObject <ServerMessages.OrderData>();
                        break;

                    case "over":
                        receivedEvent.data = deserialized["data"].ToObject <ServerMessages.OverData>();
                        break;

                    case "invalid":
                    case "fatal":
                        receivedEvent.data = deserialized["data"].ToObject <ServerMessages.InvalidData>();
                        break;

                    default:
                        if (deserialized["data"] != null)
                        {
                            receivedEvent.data = deserialized["data"].ToObject <JToken>();
                        }
                        break;
                    }

                    this.EventsStack.Push(receivedEvent);
                }

                if (this.EventsStack.Count > 0)
                {
                    return;
                }
            }
        }