Exemplo n.º 1
0
        protected override void OnReceived(byte[] buffer, long offset, long size)
        {
            // Turn into string
            var serverMessage = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);

            // JSON deserialize into server result model
            var serverResult = JsonSerializer.Deserialize <ServerResult>(serverMessage);

            switch (serverResult.Status)
            {
            case StatusCode.Failure:
                var failedItem = AwaitingResponseItems.Find(e => e.Id == serverResult.ElementId.Value);
                ToBeSentQueue.Enqueue(failedItem);
                AwaitingResponseItems.Remove(failedItem);
                Console.BackgroundColor = ConsoleColor.Red;
                break;

            case StatusCode.Success:
                var successfullItem = AwaitingResponseItems.Find(e => e.Id == serverResult.ElementId.Value);
                AwaitingResponseItems.Remove(successfullItem);
                break;
            }

            Console.WriteLine($"Server message:\n{serverResult}");
            Console.BackgroundColor = ConsoleColor.White;
        }
Exemplo n.º 2
0
        public void SendAsync(DataModel model)
        {
            // Enqueue the current model that's about to be sent
            ToBeSentQueue.Enqueue(model);

            // If there are items in the queue, send some of them first
            for (int i = 0; i < ITEMS_TO_DEQUEUE; i++)
            {
                if (ToBeSentQueue.TryDequeue(out DataModel dequeuedModel))
                {
                    // And send the model that has been waiting in the queue
                    AwaitingResponseItems.Add(dequeuedModel);

                    var serializedModel = JsonSerializer.Serialize(dequeuedModel);

                    base.SendAsync(serializedModel);
                }
                else
                {
                    break;
                }
            }
        }