示例#1
0
        static private void Read(object datalink_obj)
        {
            TCPDataLink datalink = (TCPDataLink)datalink_obj;

            List <byte> buffer = new List <byte> ();

            byte[] bytebuffer = new byte[1];

            while (!datalink._disposed && datalink.Connected())
            {
                if (datalink._stream.DataAvailable)
                {
                    if (datalink._stream.CanRead)
                    {
                        buffer.Clear();

                        while (datalink._stream.DataAvailable)
                        {
                            // TODO: maybe check byte receive count
                            datalink._stream.Read(bytebuffer, 0, bytebuffer.Length);
                            buffer.Add(bytebuffer [0]);
                        }

                        datalink._receiver.IncomingData(buffer.ToArray(), datalink);
                    }
                }
                else
                {
                    // Don't waste cpu time when no data is available
                    Thread.Sleep(1);
                }
            }
        }
示例#2
0
        private static void Listen(object obj)
        {
            TCPDataLinkListener <T> listener = obj as TCPDataLinkListener <T>;

            while (listener._listening)
            {
                while (listener._listener.Pending())
                {
                    TcpClient   newClient   = listener._listener.AcceptTcpClient();
                    TCPDataLink newDataLink = new TCPDataLink(newClient);
                    T           pp          = new T();
                    newDataLink.SetReceiver(pp);

                    listener._subscriber.IncomingNewDataLink(newDataLink, pp);
                }

                Thread.Sleep(NO_CONNECTION_PENDING_TIMEOUT);
            }
        }