コード例 #1
0
        protected override void ProduceText(string text)
        {
            int p = 0;

            while (true)
            {
                int index = text.IndexOf(':', p);
                if (index == -1)
                {
                    break;
                }
                if (int.TryParse(text.Substring(p, index - p), out int length))
                {
                    string msg = text.Substring(index + 1, length);
                    TextSubject.OnNext(msg);
                }
                else
                {
                    break;
                }
                p = index + length + 1;
                if (p >= text.Length)
                {
                    break;
                }
            }
        }
コード例 #2
0
        private void ProduceBytes(byte[] bytes)
        {
            int i = 0;

            while (bytes.Length > i + 4)
            {
                byte type    = bytes[i];
                var  builder = new StringBuilder();
                i++;
                while (bytes[i] != byte.MaxValue)
                {
                    builder.Append(bytes[i]);
                    i++;
                }
                i++;
                int length = int.Parse(builder.ToString());
                if (type == 0)
                {
                    var buffer = new byte[length];
                    Buffer.BlockCopy(bytes, i, buffer, 0, buffer.Length);
                    TextSubject.OnNext(Encoding.UTF8.GetString(buffer));
                }
                else if (type == 1)
                {
                    var buffer = new byte[length - 1];
                    Buffer.BlockCopy(bytes, i + 1, buffer, 0, buffer.Length);
                    BytesSubject.OnNext(buffer);
                }
                i += length;
            }
        }
コード例 #3
0
 protected override void ProduceText(string text)
 {
     string[] items = text.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
     foreach (var item in items)
     {
         if (item[0] == 'b')
         {
             byte[] bytes = Convert.FromBase64String(item.Substring(1));
             BytesSubject.OnNext(bytes);
         }
         else
         {
             TextSubject.OnNext(item);
         }
     }
 }