/// <summary>
 /// Encodes the UInt32.
 /// http://tools.ietf.org/html/rfc4506#section-4.2
 /// </summary>
 public static void EncodeUInt32(uint v, IByteWriter w)
 {
     w.Write((byte)((v >> 0x18) & 0xff));
     w.Write((byte)((v >> 0x10) & 0xff));
     w.Write((byte)((v >> 8) & 0xff));
     w.Write((byte)(v & 0xff));
 }
		/// <summary>
		/// Encodes the UInt32.
		/// http://tools.ietf.org/html/rfc4506#section-4.2
		/// </summary>
		public static void EncodeUInt32(uint v, IByteWriter w)
		{
			w.Write((byte)((v >> 0x18) & 0xff));
			w.Write((byte)((v >> 0x10) & 0xff));
			w.Write((byte)((v >> 8) & 0xff));
			w.Write((byte)(v & 0xff));
		}
		/// <summary>
		/// Encodes the Int64.
		/// http://tools.ietf.org/html/rfc4506#section-4.5
		/// </summary>
		public static void EncodeInt64(long v, IByteWriter w)
		{
			w.Write((byte)((v >> 56) & 0xff));
			w.Write((byte)((v >> 48) & 0xff));
			w.Write((byte)((v >> 40) & 0xff));
			w.Write((byte)((v >> 32) & 0xff));
			w.Write((byte)((v >> 24) & 0xff));
			w.Write((byte)((v >> 16) & 0xff));
			w.Write((byte)((v >>  8) & 0xff));
			w.Write((byte)(v & 0xff));
		}
 private static void NoCheckWriteFixOpaque(IByteWriter w, uint len, byte[] v)
 {
     try
     {
         w.Write(v);
         uint tail = len % 4u;
         if (tail != 0)
         {
             w.Write(_tails[4u - tail]);
         }
     }
     catch (SystemException ex)
     {
         throw new FormatException("can't write byte array", ex);
     }
 }
 /// <summary>
 /// Encodes the Int64.
 /// http://tools.ietf.org/html/rfc4506#section-4.5
 /// </summary>
 public static void EncodeInt64(long v, IByteWriter w)
 {
     w.Write((byte)((v >> 56) & 0xff));
     w.Write((byte)((v >> 48) & 0xff));
     w.Write((byte)((v >> 40) & 0xff));
     w.Write((byte)((v >> 32) & 0xff));
     w.Write((byte)((v >> 24) & 0xff));
     w.Write((byte)((v >> 16) & 0xff));
     w.Write((byte)((v >> 8) & 0xff));
     w.Write((byte)(v & 0xff));
 }
Пример #6
0
        public bool Write(object obj, Type type)
        {
            if (!Base.Write(obj, type))
            {
                if (CustomWriters.TryGetValue(type, out var writer))
                {
                    writer(obj, Stream);
                    return(true);
                }

                return(false);
            }

            return(true);
        }
Пример #7
0
    public static void WriteCollection <T>(this IByteWriter writer, ICollection <T> collection)
        where T : struct
    {
        if (collection == null)
        {
            writer.WriteInt(); // 0 items
            return;
        }

        writer.WriteInt(collection.Count);
        foreach (T type in collection)
        {
            writer.Write <T>(type);
        }
    }
Пример #8
0
        /// <summary>
        /// Returns the specified 64-bit unsigned value as varint encoded array of bytes.
        /// </summary>
        /// <param name="value">64-bit unsigned value</param>
        /// <returns>Varint array of bytes.</returns>
        public static void WriteVarintBytes(IByteWriter writer, ulong value)
        {
            do
            {
                var byteVal = value & 0x7f;
                value >>= 7;

                if (value != 0)
                {
                    byteVal |= 0x80;
                }

                writer.Write((byte)byteVal);
            } while (value != 0);
        }
Пример #9
0
        /// <summary>
        /// 发送消息包。
        /// </summary>
        /// <param name="buffer">发送的包实例。</param>
        /// <returns>返回当前执行的结果。</returns>
        protected async Task <bool> SendAsync(IByteWriter buffer)
        {
            if (!Socket.Connected)
            {
                return(false);
            }
            await using var ms = new MemoryStream();
            await using var bw = new ByteWriter(ms);
            buffer.Write(bw);
            var bytes = ms.ToArray();

            Actived = DateTimeOffset.Now;
            await _semaphore.WaitAsync(TimeSpan.FromMinutes(1));

            var count = await Socket.SendAsync(bytes, SocketFlags.None);

            _semaphore.Release();
            return(count > 0);
        }
Пример #10
0
 public bool WriteTo(IByteWriter writer)
 {
     return(writer.Write(_buffer, 0, _position, 0));
 }
 private static void NoCheckWriteFixOpaque(IByteWriter w, uint len, byte[] v)
 {
     try
     {
         w.Write(v);
         uint tail = len % 4u;
         if (tail != 0)
             w.Write(_tails[4u - tail]);
     }
     catch (SystemException ex)
     {
         throw new FormatException("can't write byte array", ex);
     }
 }
Пример #12
0
 public bool WriteTo(IByteWriter writer)
 {
     return writer.Write(_buffer, 0, _position, 0);
 }
Пример #13
0
 public bool Publish(string topic, byte[] data, int offset, int length, int msToWaitForWriteLock)
 {
     byte[] allData = CreateMessage(topic, data, offset, length, _encoder);
     return(_writer.Write(allData, 0, allData.Length, msToWaitForWriteLock));
 }