示例#1
0
        private async Task SendAsync(CastMessage castMessage)
        {
            await EnsureConnection();

            await SendSemaphoreSlim.WaitAsync();

            try
            {
                Debug.WriteLine($"SENT    : {castMessage.DestinationId}: {castMessage.PayloadUtf8}");

                byte[] message;
                using (var ms = new MemoryStream())
                {
                    Serializer.Serialize(ms, castMessage);
                    message = ms.ToArray();
                }
                var header = BitConverter.GetBytes(message.Length);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(header);
                }
                var networkStream = NetworkStream;
                await networkStream.WriteAsync(header, 0, header.Length);

                await networkStream.WriteAsync(message, 0, message.Length);

                await networkStream.FlushAsync();
            }
            finally
            {
                SendSemaphoreSlim.Release();
            }
        }
示例#2
0
        private async Task SendAsync(CastMessage castMessage)
        {
            await SendSemaphoreSlim.WaitAsync();

            try
            {
                Console.WriteLine($"SENT    : {castMessage.DestinationId}: {castMessage.PayloadUtf8}");

                byte[] message       = castMessage.ToProto();
                var    networkStream = _stream;
                await networkStream.WriteAsync(message, 0, message.Length);

                await networkStream.FlushAsync();
            }
            finally
            {
                SendSemaphoreSlim.Release();
            }
        }