protected override BitwiseStream InternalValueToBitStream() { var val = (ulong)InternalValue; var ret = new BitStream(); ulong max_number = (ulong)((1 << prefixBits) - 1); if (val < max_number) { ret.WriteByte((byte)val); } else { ret.WriteByte((byte)max_number); val -= max_number; while (val >= 128) { ret.WriteByte((byte)((val % 128) + 128)); val /= 128; } ret.WriteByte((byte)val); } ret.SeekBits(8 - prefixBits, SeekOrigin.Begin); var ret2 = new BitStream(); ret.CopyTo(ret2); ret2.Seek(0, SeekOrigin.Begin); return(ret2); }
public void CopyTo() { var bs1 = new BitStream(); bs1.Write(Encoding.ASCII.GetBytes("Hello"), 0, 5); bs1.WriteBits(0x2, 4); bs1.SeekBits(0, SeekOrigin.Begin); Assert.AreEqual(5, bs1.Length); Assert.AreEqual(44, bs1.LengthBits); Assert.AreEqual(0, bs1.Position); Assert.AreEqual(0, bs1.PositionBits); var dst = new MemoryStream(); bs1.CopyTo(dst); Assert.AreEqual(5, dst.Length); Assert.AreEqual(5, dst.Position); dst.Seek(0, SeekOrigin.Begin); var rdr = new StreamReader(dst); var final = rdr.ReadToEnd(); Assert.AreEqual("Hello", final); }
/// <summary> /// Attempts to connects to the server. /// </summary> /// <param name="host">The server address.</param> /// <param name="port">The server port.</param> /// <param name="approvalMessage">The initial message to send to the server when making the connection. This can /// be utilized by the server to provide additional information for determining whether or not to accept the connection. /// Or, can be null to send nothing.</param> /// <returns> /// True if the connection attempt was successfully started. Does not mean that the connection was established, but /// just that it can be attempted. Will return false if a connection is already established or being established. /// </returns> public bool Connect(string host, int port, BitStream approvalMessage = null) { if (_local.ConnectionsCount > 0) { return(false); } NetConnection conn; if (approvalMessage != null && approvalMessage.LengthBits > 0) { // Connect with approval message var netOutMsg = SocketHelper.GetNetOutgoingMessage(_local, approvalMessage.LengthBytes); approvalMessage.CopyTo(netOutMsg); conn = _local.Connect(host, port, netOutMsg); } else { // Connect without approval message conn = _local.Connect(host, port); } // Check if connection successful if (conn == null) { Debug.Fail("conn is null. Why?"); return(false); } // Store the remote connection as an IPSocket _remote = IPSocket.Create(conn); return(true); }
protected override BitStream internalDecode(BitStream data) { BitStream ret = new BitStream(); using (var bzip2 = new BZip2OutputStream(ret, true)) { data.CopyTo(bzip2); } ret.Seek(0, SeekOrigin.Begin); return(ret); }
protected override BitStream internalDecode(BitStream data) { BitStream ret = new BitStream(); using (var strm = new GZipStream(ret, CompressionMode.Compress, true)) { data.CopyTo(strm); } ret.Seek(0, SeekOrigin.Begin); return(ret); }
/// <summary> /// Sends data to the other end of the connection. /// </summary> /// <param name="data">Data to send.</param> /// <param name="deliveryMethod">The method to use to deliver the message. This determines how reliability, ordering, /// and sequencing will be handled.</param> /// <param name="sequenceChannel">The sequence channel to use to deliver the message. Only valid when /// <paramref name="deliveryMethod"/> is not equal to <see cref="NetDeliveryMethod.Unreliable"/> or /// <see cref="NetDeliveryMethod.ReliableUnordered"/>. Must also be a value greater than or equal to 0 and /// less than <see cref="NetConstants.NetChannelsPerDeliveryMethod"/>.</param> /// <returns> /// True if the <paramref name="data"/> was successfully enqueued for sending; otherwise false. /// </returns> /// <exception cref="NetException"><paramref name="deliveryMethod"/> equals <see cref="NetDeliveryMethod.Unreliable"/> /// or <see cref="NetDeliveryMethod.ReliableUnordered"/> and <paramref name="sequenceChannel"/> is non-zero.</exception> /// <exception cref="NetException"><paramref name="sequenceChannel"/> is less than 0 or greater than or equal to /// <see cref="NetConstants.NetChannelsPerDeliveryMethod"/>.</exception> /// <exception cref="NetException"><paramref name="deliveryMethod"/> equals <see cref="NetDeliveryMethod.Unknown"/>.</exception> public bool Send(BitStream data, NetDeliveryMethod deliveryMethod, int sequenceChannel = 0) { if (data == null || data.Length == 0) { return(false); } var msg = SocketHelper.GetNetOutgoingMessage(_conn.Peer, data.LengthBytes); data.CopyTo(msg); var ret = _conn.SendMessage(msg, deliveryMethod, sequenceChannel); return(ret == NetSendResult.Sent || ret == NetSendResult.Queued); }
public void CopyToNativeArrayWithOffset() { using (var bitStream = new BitStream(100, Allocator.Persistent)) using (var nativeArray = new NativeArray <byte>(100, Allocator.Persistent)) { bitStream.Write((byte)'a'); bitStream.Write((byte)'b'); bitStream.Write((byte)'c'); bitStream.Write((byte)'d'); bitStream.Write((byte)'e'); bitStream.Write((byte)'f'); bitStream.CopyTo(2, 3, nativeArray); Assert.AreEqual(nativeArray[0], (byte)'c'); Assert.AreEqual(nativeArray[1], (byte)'d'); Assert.AreEqual(nativeArray[2], (byte)'e'); Assert.AreNotEqual(nativeArray[3], (byte)'f'); } }
public void CopyToByteArrayWithOffset() { byte[] byteArray = new byte[100]; using (var bitStream = new BitStream(100, Allocator.Persistent)) { bitStream.Write((byte)'a'); bitStream.Write((byte)'b'); bitStream.Write((byte)'c'); bitStream.Write((byte)'d'); bitStream.Write((byte)'e'); bitStream.Write((byte)'f'); bitStream.CopyTo(2, 3, ref byteArray); Assert.AreEqual(byteArray[0], (byte)'c'); Assert.AreEqual(byteArray[1], (byte)'d'); Assert.AreEqual(byteArray[2], (byte)'e'); Assert.AreNotEqual(byteArray[3], (byte)'f'); } }