Ejemplo n.º 1
0
        protected string ReceiveMessage()
        {
            var  buffer = new MemoryStream();
            int  @byte;
            bool possibleDelimiterIsComing;
            var  possibleDelimiterCount = 0;

            while ((@byte = stream.ReadByte()) != -1)
            {
                buffer.WriteByte((byte)@byte);

                if (@byte == this.delimiterBytes[possibleDelimiterCount])
                {
                    possibleDelimiterIsComing = true;
                    possibleDelimiterCount++;
                }
                else
                {
                    possibleDelimiterIsComing = false;
                    possibleDelimiterCount    = 0;
                }

                if (possibleDelimiterIsComing && this.delimiterBytes.Length == possibleDelimiterCount)
                {
                    // complete message received, return removing the delimiters
                    return(StreamEncoding.GetString(
                               buffer.GetBuffer(),
                               0,
                               (int)(buffer.Length - this.delimiterBytes.Length)));
                }
            }

            // return incomplete message
            return(StreamEncoding.GetString(buffer.GetBuffer()));
        }
Ejemplo n.º 2
0
        private int ReceiveLength()
        {
            var lengthBytes = new byte[4];

            this.stream.Read(lengthBytes, 0, lengthBytes.Length);
            var length = StreamEncoding.GetInt32(lengthBytes);

            return(length);
        }
Ejemplo n.º 3
0
        private string ReceiveMessage(int length)
        {
            var messageBytes       = new byte[length];
            var currentBufferIndex = 0;
            var bytesRead          = -1;

            while (bytesRead != 0 && currentBufferIndex < messageBytes.Length)
            {
                bytesRead = this.stream.Read(
                    messageBytes,
                    currentBufferIndex,
                    messageBytes.Length - currentBufferIndex);

                currentBufferIndex += bytesRead;
            }

            return(StreamEncoding.GetString(messageBytes));
        }
Ejemplo n.º 4
0
 public DelimitedStream(TcpClient client, string delimiter) : base(client)
 {
     this.delimiter      = delimiter;
     this.delimiterBytes = StreamEncoding.GetBytes(this.delimiter);
 }
Ejemplo n.º 5
0
        public void Send(string message)
        {
            var data = StreamEncoding.GetBytes(message);

            this.CustomSend(data);
        }
Ejemplo n.º 6
0
        private void SendLength(byte[] message)
        {
            var lengthBytes = StreamEncoding.GetBytes(message.Length);

            this.stream.Write(lengthBytes, 0, lengthBytes.Length);
        }