示例#1
0
        private void writeMessageToStream(NetworkStream networkStream, TcpStreamMessage message)
        {
            byte[] dataToWrite = prepareMessageBytes(message);

            writeMessageLength(networkStream, dataToWrite.Length);
            writeMessageData(networkStream, dataToWrite);
        }
示例#2
0
        private byte[] prepareMessageBytes(TcpStreamMessage message)
        {
            MemoryStream dataStream = new MemoryStream();

            byte[] msgType = BitConverter.GetBytes(Convert.ToInt32(message.MessageType));

            setByteOrderForEndianMode(ref msgType);
            dataStream.Write(msgType, 0, msgType.Length);

            // copy the TcpStreamMessage data from the start position
            message.DataStream.Position = 0;
            byte[] msgData = message.DataStream.ToArray();

            setByteOrderForEndianMode(ref msgData);
            dataStream.Write(msgData, 0, msgData.Length);

            return(dataStream.ToArray());
        }
示例#3
0
        /// <summary>
        /// sends a tcp stream message
        /// </summary>
        /// <param name="message"></param>
        public bool SendMessage(TcpStreamMessage message)
        {
            bool success = true;

            try
            {
                TcpClient     tcpClient     = createTcpClient(message);
                NetworkStream networkStream = tcpClient.GetStream();

                writeMessageToStream(networkStream, message);

                networkStream.Close();
            }
            catch
            {
                //todo: log the exception
                success = false;
            }

            return(success);
        }
示例#4
0
 private TcpClient createTcpClient(TcpStreamMessage message)
 {
     return(new TcpClient(message.IpAddress.ToString(), message.PortNumber));
 }