示例#1
0
        /// <summary>
        /// Send the given message
        /// </summary>
        public void Send(Message decoded, params byte[] msg)
        {
            Message.UpdateChecksum(msg, msg.Length);
            if (SendMessage != null)
            {
                SendMessage(msg, decoded);
            }
            var length = Message.GetMessageLength(msg);

            try
            {
                Send(msg, length);
            }
            catch (InvalidOperationException)
            {
                Close();
                throw;
            }
        }
示例#2
0
        /// <summary>
        /// Read a single message
        /// </summary>
        protected override byte[] ReadMessage()
        {
            // Check for an open connection
            var port = this.tcpClient;

            if (port == null)
            {
                return(null);
            }

            // Try to read the message
            var data = new byte[16];

            while (true)
            {
                // Read a potential opcode

                Read(port, data, 0, 1);
                if (Message.IsOpcode(data[0]))
                {
                    break;
                }
            }

            // Now read further data
            Read(port, data, 1, 1); // Read at least the second byte

            var length = Message.GetMessageLength(data);

            if (length > 2)
            {
                // Read remaining data
                Read(port, data, 2, length - 2);
            }

            return(data);
        }