Ejemplo n.º 1
0
        private void Read()
        {
            var length    = new byte[sizeof(int)];
            var recovered = Stream.Read(length, 0, length.Length);

            if (recovered == length.Length)
            {
                var dataLength = BitConverter.ToInt32(length, 0);
                var buffer     = new byte[dataLength];
                recovered = Stream.Read(buffer, 0, buffer.Length);
                if (recovered == buffer.Length)
                {
                    var message = MessageTranslator.DecodeMessage(buffer);
                    MessageReceived?.Invoke(this, message);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 2
0
        public void Enqueue(Message message)
        {
            // Direct Sending
            var messageBytes = MessageTranslator.EncodeMessage(message);
            var bytes        = new byte[sizeof(int) + messageBytes.Length];
            var length       = BitConverter.GetBytes(messageBytes.Length);

            length.CopyTo(bytes, 0);
            messageBytes.CopyTo(bytes, 4);
            Stream.Write(bytes, 0, bytes.Length);
            // Queue
            //SendBuffers.Enqueue(MessageTranslator.EncodeMessage(message));
        }