Пример #1
0
 IFrame IProtocolProcessor.GetOutboundFrame(NetContext context)
 {
     lock (this)
     {
         return(GetFrame(context, ref singleFrameOrList));
     }
 }
Пример #2
0
 protected void EnqueueFrame(NetContext context, IFrame frame)
 {
     lock(this)
     {
         AddFrame(context, ref singleFrameOrList, frame);
     }
 }
Пример #3
0
 protected void EnqueueFrame(NetContext context, IFrame frame)
 {
     lock (this)
     {
         AddFrame(context, ref singleFrameOrList, frame);
     }
 }
Пример #4
0
 internal BufferStream(NetContext context, int maxLength)
 {
     if(context == null) throw new ArgumentNullException("context");
     this.context = context;
     this.maxLength = maxLength;
     buffers = context.GetCircularBuffer();
 }
Пример #5
0
 public void PromptToSend(NetContext context)
 {
     if (Interlocked.CompareExchange(ref writerCount, 1, 0) == 0)
     {
         // then **we** are the writer
         context.Handler.StartSending(this);
     }
 }
Пример #6
0
        public int ReadOutgoing(NetContext context, byte[] buffer, int index, int count)
        {
            int bufferedLength = outgoingBuffer == null ? 0 : (int)outgoingBuffer.Length;

            if (bufferedLength == 0)
            {
                // nothing in the outbound buffer? then check to see if we have any new messages needing processing at the protocol layer, then the connection/handler layer
                IFrame    frame;
                const int SANE_PACKET_SIZE = 512;
                while (bufferedLength < SANE_PACKET_SIZE &&
                       ((frame = protocol.GetOutboundFrame(context)) != null
                        ||
                        (context.Handler.RequestOutgoing(this) && (frame = protocol.GetOutboundFrame(context)) != null)) &&
                       GetFlag(ConnectionFlags.IsAlive))
                // ^^^^ we try and get a frame from the protocol layer; if that is empty, we nudge the connection/handler layer to write, and then we
                // check again for a fram from the protocol layer (the connection/handler can only queue frames); we then repeat this if necessary/appropriate
                // to fill a packet
                {
                    if (outgoingBuffer == null)
                    {
                        outgoingBuffer = new BufferStream(context, context.Handler.MaxOutgoingQuota);
                    }
                    outgoingBuffer.Position   = outgoingBuffer.Length;
                    outgoingBuffer.IsReadonly = false;
                    frame.Write(context, this, outgoingBuffer);
                    outgoingBuffer.IsReadonly = true;
                    bufferedLength            = (int)outgoingBuffer.Length;
                    if (frame.Flush)
                    {
                        break;              // send "as is"
                    }
                }
            }

            if (bufferedLength == 0)
            {
                Interlocked.Exchange(ref writerCount, 0); // nix and nada
                return(0);
            }

            int bytesRead = 0;

            if (outgoingBuffer != null)
            {
                outgoingBuffer.Position = 0;
                bytesRead = outgoingBuffer.Read(buffer, index, count);

                outgoingBuffer.Discard(bytesRead);
                if (outgoingBuffer.Length == 0)
                {
                    outgoingBuffer.Dispose();
                    outgoingBuffer = null;
                }
            }


            return(bytesRead);
        }
Пример #7
0
        public void GracefulShutdown(NetContext context)
        {
            var tmp = this.protocol;

            if (tmp != null)
            {
                tmp.GracefulShutdown(context, this);
            }
        }
Пример #8
0
 public void AppendIncoming(NetContext context, byte[] buffer, int index, int count)
 {
     if(count != 0)
     {
         if (incomingBuffer == null) incomingBuffer = new BufferStream(context, context.Handler.MaxIncomingQuota);
         incomingBuffer.Position = incomingBuffer.Length;
         incomingBuffer.Write(buffer, index, count);
     }
 }
Пример #9
0
 internal BufferStream(NetContext context, int maxLength)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     this.context   = context;
     this.maxLength = maxLength;
     buffers        = context.GetCircularBuffer();
 }
Пример #10
0
 public TcpHandler(int concurrentOperations = 0)
 {
     context = new NetContext(AsyncHandler, this);
     if (concurrentOperations <= 0) concurrentOperations = 2 * Environment.ProcessorCount;
     this.concurrentOperations = new Semaphore(concurrentOperations, concurrentOperations);
     MutexTimeout = 10000;
     ConnectTimeout = 5000;
     MaxIncomingQuota = DefaultMaxIncomingQuota;
     MaxOutgoingQuota = DefaultMaxOutgoingQuota;
 }
Пример #11
0
 protected override void Dispose(bool disposing)
 {
     if(disposing)
     {
         if (context != null) context.Recycle(buffers);
         context = null;
         buffers = null;
     }
     base.Dispose(disposing);
 }
Пример #12
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");
        }
 protected override void Send(NetContext context, Connection connection, object message)
 {
     byte[] blob = (byte[])message, length = new byte[4];
     int len = blob.Length; // big-endian
     length[0] = (byte)(len >> 24);
     length[1] = (byte)(len >> 16);
     length[2] = (byte)(len >> 8);
     length[3] = (byte)(len);
     EnqueueFrame(context, new BinaryFrame(length, false));
     EnqueueFrame(context, new BinaryFrame(blob, true));
 }
Пример #14
0
 public void AppendIncoming(NetContext context, byte[] buffer, int index, int count)
 {
     if (count != 0)
     {
         if (incomingBuffer == null)
         {
             incomingBuffer = new BufferStream(context, context.Handler.MaxIncomingQuota);
         }
         incomingBuffer.Position = incomingBuffer.Length;
         incomingBuffer.Write(buffer, index, count);
     }
 }
Пример #15
0
 public TcpHandler(int concurrentOperations = 0)
 {
     Context = new NetContext(AsyncHandler, this);
     if (concurrentOperations <= 0)
     {
         concurrentOperations = 2 * Environment.ProcessorCount;
     }
     this.concurrentOperations = new Semaphore(concurrentOperations, concurrentOperations);
     MutexTimeout     = 10000;
     ConnectTimeout   = 5000;
     MaxIncomingQuota = DefaultMaxIncomingQuota;
     MaxOutgoingQuota = DefaultMaxOutgoingQuota;
 }
Пример #16
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (context != null)
         {
             context.Recycle(buffers);
         }
         context = null;
         buffers = null;
     }
     base.Dispose(disposing);
 }
            protected override int ProcessIncoming(NetContext context, Connection connection, System.IO.Stream incomingBuffer)
            {
                if (incomingBuffer.Length < 4) return 0;
                byte[] length = new byte[4];
                NetContext.Fill(incomingBuffer, length, 4);

                int len = (length[0] << 24) | (length[1] << 16) | (length[2] << 8) | (length[3]);
                if (incomingBuffer.Length < len + 4) return 0;

                byte[] blob = new byte[len];
                NetContext.Fill(incomingBuffer, blob, len);
                context.Handler.OnReceived(connection, blob);
                return len + 4;
            }
Пример #18
0
 protected override bool TryBasicResponse(NetContext context, System.Collections.Specialized.StringDictionary requestHeaders, string requestLine, System.Collections.Specialized.StringDictionary responseHeaders, out HttpStatusCode code, out string body)
 {
     var match = Regex.Match(requestLine, @"GET (.*) HTTP/1.[01]");
     Uri uri;
     if (match.Success && Uri.TryCreate(match.Groups[1].Value.Trim(), UriKind.RelativeOrAbsolute, out uri))
     {
         switch (uri.OriginalString)
         {
             case "/ping":
                 code = System.Net.HttpStatusCode.OK;
                 body = "Ping response from custom factory";
                 return true;
         }
     }
     return base.TryBasicResponse(context, requestHeaders, requestLine, responseHeaders, out code, out body);
 }
Пример #19
0
 protected virtual void Write(NetContext context, Connection connection, Stream stream)
 {
     byte[] buffer = null;
     try
     {
         buffer = context.GetBuffer();
         buffer[0] = (byte) (value >> 24);
         buffer[1] = (byte) (value >> 16);
         buffer[2] = (byte) (value >> 8);
         buffer[3] = (byte) (value);
         stream.Write(buffer, 0, 4);
     } finally
     {
         context.Recycle(buffer);   
     }
 }
Пример #20
0
 protected virtual void Write(NetContext context, Connection connection, Stream stream)
 {
     byte[] buffer = null;
     try
     {
         buffer    = context.GetBuffer();
         buffer[0] = (byte)(value >> 24);
         buffer[1] = (byte)(value >> 16);
         buffer[2] = (byte)(value >> 8);
         buffer[3] = (byte)(value);
         stream.Write(buffer, 0, 4);
     } finally
     {
         context.Recycle(buffer);
     }
 }
Пример #21
0
 protected virtual void Write(NetContext context, Connection connection, Stream stream)
 {
     byte[] buffer = null;
     try
     {
         buffer = context.GetBuffer();
         if(encoding.GetMaxByteCount(value.Length) <= buffer.Length || encoding.GetByteCount(value) <= buffer.Length)
         {
             int len = encoding.GetBytes(value, 0, value.Length, buffer, 0);
             stream.Write(buffer, 0, len);
         } else
         { // need to do things the hard way...
             throw new NotImplementedException();
         }
     }
     finally
     {
         context.Recycle(buffer);
     }
 }
Пример #22
0
 protected virtual void Write(NetContext context, Connection connection, Stream stream)
 {
     byte[] buffer = null;
     try
     {
         buffer = context.GetBuffer();
         if (encoding.GetMaxByteCount(value.Length) <= buffer.Length || encoding.GetByteCount(value) <= buffer.Length)
         {
             int len = encoding.GetBytes(value, 0, value.Length, buffer, 0);
             stream.Write(buffer, 0, len);
         }
         else
         { // need to do things the hard way...
             throw new NotImplementedException();
         }
     }
     finally
     {
         context.Recycle(buffer);
     }
 }
Пример #23
0
 internal static void AddFrame(NetContext context, ref object holder, IFrame frame)
 {
     if (holder == null)
     {
         holder = frame;
     }
     else
     {
         var list = holder as CircularBuffer <IFrame>;
         if (list == null)
         {
             list = context.GetFrameBuffer();
             list.Push((IFrame)holder);
             list.Push(frame);
             holder = list;
         }
         else
         {
             list.Push(frame);
         }
     }
 }
Пример #24
0
        internal static IFrame GetFrame(NetContext context, ref object holder)
        {
            if (holder == null)
            {
                return(null);
            }
            var frame = holder as IFrame;

            if (frame != null)
            {
                holder = null;
                return(frame);
            }

            var list = (CircularBuffer <IFrame>)holder;

            switch (list.Count)
            {
            case 0:
                holder = null;
                context.Recycle(list);
                return(null);

            case 1:
                frame  = list.Pop();
                holder = null;
                context.Recycle(list);
                return(frame);

            case 2:
                frame  = list.Pop();
                holder = list.Pop();
                context.Recycle(list);
                return(frame);

            default:
                return(list.Pop());
            }
        }
Пример #25
0
 internal static void AddFrame(NetContext context, ref object holder, IFrame frame)
 {
     if (holder == null)
     {
         holder = frame;
     }
     else
     {
         var list = holder as CircularBuffer<IFrame>;
         if (list == null)
         {
             list = context.GetFrameBuffer();
             list.Push((IFrame)holder);
             list.Push(frame);
             holder = list;
         }
         else
         {
             list.Push(frame);
         }
     }
 }
Пример #26
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 */ }
        }
Пример #27
0
 void IMessageProcessor.OnShutdown(NetContext context, Connection conn)
 {
 }
Пример #28
0
 void IMessageProcessor.Received(NetContext context, Connection connection, object message)
 { // right back at you!
     connection.Send(context, message);
 }
Пример #29
0
 void IMessageProcessor.Authenticate(NetContext context, Connection connection, StringDictionary claims)
 {
 }
Пример #30
0
 void IMessageProcessor.OpenConnection(NetContext context, Connection connection)
 {
 }
Пример #31
0
 void IMessageProcessor.EndProcessor(NetContext context)
 {
 }
 protected override int ProcessIncoming(NetContext context, Connection connection, System.IO.Stream incomingBuffer)
 {
     throw new NotSupportedException(); // not expecting more incoming
 }
Пример #33
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);
        }
Пример #34
0
        public int ReadOutgoing(NetContext context, byte[] buffer, int index, int count)
        {
            int bufferedLength = outgoingBuffer == null ? 0 : (int) outgoingBuffer.Length;

            if (bufferedLength == 0)
            {
                // nothing in the outbound buffer? then check to see if we have any new messages needing processing at the protocol layer, then the connection/handler layer
                IFrame frame;
                const int SANE_PACKET_SIZE = 512;
                while (bufferedLength < SANE_PACKET_SIZE &&
                    ((frame = protocol.GetOutboundFrame(context)) != null
                        ||
                        (context.Handler.RequestOutgoing(this) && (frame = protocol.GetOutboundFrame(context)) != null))
                       && GetFlag(ConnectionFlags.IsAlive))
                    // ^^^^ we try and get a frame from the protocol layer; if that is empty, we nudge the connection/handler layer to write, and then we
                    // check again for a fram from the protocol layer (the connection/handler can only queue frames); we then repeat this if necessary/appropriate
                    // to fill a packet
                {
                    if (outgoingBuffer == null) outgoingBuffer = new BufferStream(context, context.Handler.MaxOutgoingQuota);
                    outgoingBuffer.Position = outgoingBuffer.Length;
                    outgoingBuffer.IsReadonly = false;
                    frame.Write(context, this, outgoingBuffer);
                    outgoingBuffer.IsReadonly = true;
                    bufferedLength = (int) outgoingBuffer.Length;
                    if (frame.Flush) break; // send "as is"
                }
            }

            if (bufferedLength == 0)
            {
                Interlocked.Exchange(ref writerCount, 0); // nix and nada
                return 0;
            }

            int bytesRead = 0;
            if (outgoingBuffer != null)
            {
                outgoingBuffer.Position = 0;
                bytesRead = outgoingBuffer.Read(buffer, index, count);

                outgoingBuffer.Discard(bytesRead);
                if(outgoingBuffer.Length == 0)
                {
                    outgoingBuffer.Dispose();
                    outgoingBuffer = null;
                }
            }

            
            return bytesRead;
        }
Пример #35
0
        internal static IFrame GetFrame(NetContext context, ref object holder)
        {
            if (holder == null) return null;
            var frame = holder as IFrame;
            if (frame != null)
            {
                holder = null;
                return frame;
            }

            var list = (CircularBuffer<IFrame>)holder;
            switch(list.Count)
            {
                case 0:
                    holder = null;
                    context.Recycle(list);
                    return null;
                case 1:
                    frame = list.Pop();
                    holder = null;
                    context.Recycle(list);
                    return frame;
                case 2:
                    frame = list.Pop();
                    holder = list.Pop();
                    context.Recycle(list);
                    return frame;
                default:
                    return list.Pop();
            }
        }
Пример #36
0
 protected virtual void InitializeClientHandshake(NetContext context, Connection connection)
 {
     /* nothing to do */
 }
Пример #37
0
 public void Send(NetContext context, object message)
 {
     protocol.Send(context, this, message);
     PromptToSend(context);
 }
Пример #38
0
 internal void InitializeClientHandshake(NetContext context)
 {
     protocol.InitializeClientHandshake(context, this);
 }
Пример #39
0
        public bool ProcessBufferedData(NetContext context, out int messageCount)
        {
            messageCount = 0;
            
            if (incomingBuffer != null && incomingBuffer.Length > 0)
            {
                int toDiscard;
                do
                {
                    if (!CanRead) break; // stop processing data
#if VERBOSE
                    long inboundLength = incomingBuffer.Length;
                    Debug.WriteLine(string.Format("[{0}]\tprocessing with {1} bytes available", context.Handler, inboundLength));
#endif
                    incomingBuffer.Position = 0;
                    toDiscard = protocol.ProcessIncoming(context, this, incomingBuffer);
                    if (toDiscard > 0)
                    {
                        messageCount++;
                        // could be null if our processing shut it down!
                        if(incomingBuffer != null)
                        {
                            if(toDiscard == incomingBuffer.Length)
                            {
                                incomingBuffer.Dispose();
                                incomingBuffer = null;
                            } else
                            {
                                incomingBuffer.Discard(toDiscard);
                            }
                            
                        } 
#if VERBOSE
                        Debug.WriteLine(string.Format("[{0}]\tprocessed {1} bytes; {2} remaining", context.Handler, toDiscard, inboundLength - toDiscard));
#endif
                    } else if(toDiscard < 0)
                    {
                        if(incomingBuffer != null)
                        {
                            incomingBuffer.Dispose();
                            incomingBuffer = null;
                        }
                        
                        // stop reading! (for example, protocol error, graceful shutdown)
                        return false;
                    }
                    else
                    {
#if VERBOSE
                        Debug.WriteLine(string.Format("[{0}]\tincomplete", context.Handler));
#endif
                    }
                } while (toDiscard > 0 && incomingBuffer != null && incomingBuffer.Length > 0);
            }
            if(incomingBuffer != null && incomingBuffer.Length == 0)
            {
                incomingBuffer.Dispose();
                incomingBuffer = null;
            }
            return true;
        }
Пример #40
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 */ }
 }
Пример #41
0
 void IFrame.Write(NetContext context, Connection connection, Stream stream)
 {
     Write(context, connection, stream);
 }
Пример #42
0
 internal void InitializeClientHandshake(NetContext context)
 {
     protocol.InitializeClientHandshake(context, this);
 }
Пример #43
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);
        }
Пример #44
0
 public void Send(NetContext context, object message)
 {
     protocol.Send(context, this, message);
     PromptToSend(context);
 }
Пример #45
0
 void IMessageProcessor.StartProcessor(NetContext context, string configuration)
 {
 }
Пример #46
0
 public void GracefulShutdown(NetContext context)
 {
     var tmp = this.protocol;
     if (tmp != null) tmp.GracefulShutdown(context, this);
 }
Пример #47
0
 void IMessageProcessor.Heartbeat(NetContext context)
 {
 }
Пример #48
0
        void BroadcastProcessIterator(IEnumerator<Connection> iterator, Func<Connection, object> selector, NetContext ctx)
        {

            bool cont;
            do
            {
                Connection conn;
                lock (iterator)
                {
                    cont = iterator.MoveNext();
                    conn = cont ? iterator.Current : null;
                }
                try
                {
                    if (cont && conn != null && conn.IsAlive)
                    {
                        var message = selector(conn);
                        if (message != null)
                        {
                            conn.Send(ctx, message);
                            Interlocked.Increment(ref broadcastCounter);
                        }
                    }
                }
                catch
                { // if an individual connection errors... KILL IT! and then gulp down the exception
                    try { conn.Shutdown(ctx); }
                    catch { }
                }
            } while (cont);
        }
Пример #49
0
 void IMessageProcessor.CloseConnection(NetContext context, Connection connection)
 {
 }
Пример #50
0
 void IFrame.Write(NetContext context, Connection connection, Stream stream)
 {
     Write(context, connection, stream);
 }
Пример #51
0
 void IMessageProcessor.AfterAuthenticate(NetContext context, Connection connection)
 {
 }
 protected override void Send(NetContext context, Connection connection, object message)
 {
     EnqueueFrame(context, new StringFrame((string)message));
 }
Пример #53
0
 void IMessageProcessor.Flushed(NetContext context, Connection connection)
 {
 }
Пример #54
0
 protected virtual void Write(NetContext context, Connection connection, Stream stream)
 {
     stream.Write(value, 0, value.Length);
 }
Пример #55
0
        private void BroadcastProcessIterator(IEnumerator <Connection> iterator, Func <Connection, object> selector, NetContext ctx)
        {
            bool cont;

            do
            {
                Connection conn;
                lock (iterator)
                {
                    cont = iterator.MoveNext();
                    conn = cont ? iterator.Current : null;
                }
                try
                {
                    if (cont && conn != null && conn.IsAlive)
                    {
                        var message = selector(conn);
                        if (message != null)
                        {
                            conn.Send(ctx, message);
                            Interlocked.Increment(ref broadcastCounter);
                        }
                    }
                }
                catch
                { // if an individual connection errors... KILL IT! and then gulp down the exception
                    try
                    {
                        conn?.Shutdown(ctx);
                    }
                    catch
                    {
                        //ignored
                    }
                }
            } while (cont);
        }
Пример #56
0
 public void PromptToSend(NetContext context)
 {
     if(Interlocked.CompareExchange(ref writerCount, 1, 0) == 0)
     { 
         // then **we** are the writer
         context.Handler.StartSending(this);
     }
 }
Пример #57
0
 void IProtocolProcessor.InitializeClientHandshake(NetContext context, Connection connection)
 {
     InitializeClientHandshake(context, connection);
 }
 public void SendShutdown(NetContext context)
 {
     EnqueueFrame(context, ShutdownFrame.Default);
 }