Esempio n. 1
0
 public NetContext(EventHandler<SocketAsyncEventArgs> asyncHandler, TcpHandler handler)
 {
     if (handler == null) throw new ArgumentNullException("handler");
     if (asyncHandler == null) throw new ArgumentNullException("asyncHandler");
     this.asyncHandler = asyncHandler;
     this.handler = handler;
 }
Esempio n. 2
0
        public void DeflateRoundTrip()
        {
            var handler = new TcpHandler();
            var ctx = new NetContext(delegate { }, handler);

            var extn = PerFrameDeflate.Default.CreateExtension("deflate-frame");
            var conn = new WebSocketConnection(new IPEndPoint(IPAddress.Loopback, 20000));

            string[] messages = {
                "This extension uses one reserved bit to indicate whether DEFLATE is applied to the frame or not.  We call this \"COMP\" bit.",
                "Hello",
                "This extension operates only on data frames, and only on the \"Application data\" therein (it does not affect the \"Extension data\" portion of the \"Payload data\").",
                "world", "Hello",
                "To send a frame with DEFLATE applied, an endpoint MUST use the following algorithm.",
                "world",
                "Apply DEFLATE [RFC1951] to all the octets in the \"Application data\" part of the frame.  Multiple blocks MAY be used.  Any type of block MAY be used.  Both block with \"BFINAL\" set to 0 and 1 MAY be used.",
                "If the resulting data does not end with an empty block with no compression (\"BTYPE\" set to 0), append an empty block with no compression to the tail.",
                "Remove 4 octets (that are 0x00 0x00 0xff 0xff) from the tail.",
                "Hello",
                "Build a frame by putting the resulting octets in the \"Application data\" part instead of the original octets.  The payload length field of the frame MUST be the sum of the size of the \"Extension data\" part and these resulting octets.  \"COMP\" bit MUST be set to 1."
            };

            var frames = Array.ConvertAll(messages, CreateFrame);

            int initialSize = frames.Sum(x => x.PayloadLength);
            Assert.IsTrue(frames.All(x => !x.Reserved1), "no COMP initially");
            var munged = frames.SelectMany(f => extn.ApplyOutgoing(ctx, conn, f)).ToArray();
            Assert.AreEqual(frames.Length, munged.Length, "compress: 1 in, 1 out");
            
            Assert.IsTrue(frames.Any(x => x.Reserved1), "some COMP after compress");
            Assert.IsTrue(frames.Any(x => !x.Reserved1), "some non-COMP after compress");

            for (int i = 0; i < munged.Length; i++)
            {
                var ms = new MemoryStream();
                munged[i].Payload.Position = 0;
                munged[i].Payload.CopyTo(ms);
                ms.Position = 0;
                munged[i].Payload = new MemoryStream(ms.ToArray()); // read-only, deliberately
            }
            int mungedSize = frames.Sum(x => x.PayloadLength);

            var unmunged = munged.SelectMany(f => extn.ApplyIncoming(ctx, conn, f)).ToArray();

            Assert.AreEqual(unmunged.Length, unmunged.Length, "inflate: 1 in, 1 out");
            Assert.IsTrue(unmunged.All(x => !x.Reserved1), "no COMP after inflate");

            int unmungedSize = unmunged.Sum(x => x.PayloadLength);

            Console.WriteLine("Uncompressed: {0} bytes; compressed: {1} bytes; inflated {2} bytes", initialSize, mungedSize, unmungedSize);

            string[] finalMessages = Array.ConvertAll(unmunged, ReadFrameMessage);

            Assert.IsTrue(finalMessages.SequenceEqual(messages), "Equal messages");
        }
Esempio n. 3
0
 public NetContext(EventHandler <SocketAsyncEventArgs> asyncHandler, TcpHandler handler)
 {
     if (handler == null)
     {
         throw new ArgumentNullException("handler");
     }
     if (asyncHandler == null)
     {
         throw new ArgumentNullException("asyncHandler");
     }
     this.asyncHandler = asyncHandler;
     this.handler      = handler;
 }
Esempio n. 4
0
 public ClientNode(TcpHandler handler, IPEndPoint endpoint)
 {
     Seen();
     if (endpoint == null)
     {
         throw new ArgumentNullException("endpoint");
     }
     if (handler == null)
     {
         throw new ArgumentNullException("handler");
     }
     this.handler  = handler;
     this.endpoint = endpoint;
 }
Esempio n. 5
0
        public void Shutdown(NetContext context)
        {
            bool wasAlive = GetFlag(ConnectionFlags.IsAlive);

            SetFlag(ConnectionFlags.IsAlive, false);
            if (wasAlive)
            {
                TcpHandler handler = context == null ? null : context.Handler;
                if (handler != null)
                {
                    handler.OnClosing(this);
                }
            }

            var socket = Socket;

            try { socket.Shutdown(SocketShutdown.Send); }
            catch { /* swallow */ }
            try { socket.Close(); }
            catch { /* swallow */ }
            try { ((IDisposable)socket).Dispose(); }
            catch { /* swallow */ }
        }
Esempio n. 6
0
        static string DecompressFrame(IExtension extn, params byte[] data)
        {
            var handler = new TcpHandler();
            var ctx = new NetContext(delegate { }, handler);
            
            if (extn == null) extn = PerFrameDeflate.Default.CreateExtension("deflate-frame");
            var frame = new WebSocketsFrame {
                OpCode = WebSocketsFrame.OpCodes.Text,
                Payload = new MemoryStream(data),
                PayloadLength = data.Length,
                Reserved1 = true
            };
            var connection = new WebSocketConnection(new IPEndPoint(IPAddress.Loopback, 20000));
            var decoded = extn.ApplyIncoming(ctx, connection, frame).Single();

            return ReadFrameMessage(decoded);
        }
Esempio n. 7
0
        public void CompressFrame()
        {
            var handler = new TcpHandler();
            var ctx = new NetContext(delegate { }, handler);

            IExtensionFactory factory = new PerFrameDeflate(0, false);
            var extn = factory.CreateExtension("deflate-frame");
            var data = Encoding.UTF8.GetBytes("Hello");
            var frame = new WebSocketsFrame
            {
                OpCode = WebSocketsFrame.OpCodes.Text,
                Payload = new MemoryStream(data),
                PayloadLength = data.Length,
                Reserved1 = false
            };
            var connection = new WebSocketConnection(new IPEndPoint(IPAddress.Loopback, 20000));
            var encoded = extn.ApplyOutgoing(ctx, connection, frame).Single();
            var ms = new MemoryStream();
            encoded.Payload.CopyTo(ms);
            string hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);
            Assert.AreEqual("F2-48-CD-C9-C9-07-00", hex);

            // unrelated decoder
            extn = PerFrameDeflate.Default.CreateExtension("deflate-frame");
            var decoded = extn.ApplyIncoming(ctx, connection, frame).Single();

            ms = new MemoryStream();
            decoded.Payload.Position = 0;
            decoded.Payload.CopyTo(ms);
            string s = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
            Assert.AreEqual("Hello", s);
        }
Esempio n. 8
0
 public ClientNode(TcpHandler handler, IPEndPoint endpoint)
 {
     Seen();
     if (endpoint == null) throw new ArgumentNullException("endpoint");
     if (handler == null) throw new ArgumentNullException("handler");
     this.handler = handler;
     this.endpoint = endpoint;
 }