Пример #1
0
        private async Task SendClientHandler(OwnTcpServerConnection connection)
        {
            uint count = 0;

            try
            {
                while (connection.Client.Connected)
                {
                    OwnTcpSendMessage send = connection.SendQueue.Dequeue();
                    if (send == null || !connection.Client.Connected)
                    {
                        break;
                    }

                    if (!send.Message.HasID)
                    {
                        send.Message.ID = count++;
                    }

                    byte[] data = GetBytes(send.Message).ToArray();
                    await connection.Stream.WriteAsync(data, 0, data.Length);

                    await connection.Stream.FlushAsync();

                    send.SetResult(true);
                }
            }
            catch (Exception e)
            {
                await CloseConnection(connection);
            }
        }
        private static async Task SendMessagesHandler(OwnTcpClientConnection connection)
        {
            try
            {
                uint count = 0;
                while (!connection.SendQueue.IsEnded)
                {
                    OwnTcpSendMessage send = connection.SendQueue.Dequeue();
                    if (connection.SendQueue.IsEnded)
                    {
                        break;
                    }

                    send.Message.ID = count++;

                    if (!send.Message.IsFireAndForget)
                    {
                        connection.Waits.Add(send.Message.ID, send);
                    }

                    byte[] data = GetBytes(send.Message).ToArray();
                    await connection.Stream.WriteAsync(data, 0, data.Length);

                    await connection.Stream.FlushAsync();

                    if (send.Message.IsFireAndForget)
                    {
                        send.SetResult(true);
                    }
                    if (send.Message.Topic == CloseCmd)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                await connection.CloseAsync(new Exception("SendMessageHandler error", e), false);
            }
        }