예제 #1
0
        private static void StopOne(TcpListener tcpListener)
        {
            if (tcpListener == null)
            {
                return;
            }

            WorkerThread wt = cdctThread[tcpListener];

            if (tcpListener != null)
            {
                tcpListener.Stop();
                tcpListener = null;
            }

            wt.SetToStop();
            wt.SetEvent();
        }
예제 #2
0
        internal void Receive()
        {
            ResetFinishing();

            socket.ReceiveTimeout = 1000; // milliseconds

            lastReceiveTime = DateTime.Now;

            if (processingThread == null)
            {
                processingThread = new WorkerThread(e =>
                {
                    if (onProcessingThreadException != null)
                    {
                        onProcessingThreadException(this, new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "Receive", null, e));
                    }
                });
                processingThread.Start(() =>
                {
                    List <byte> lstBts = BtsQueueToList();
                    if (lstBts != null && lstBts.Count > 0)
                    {
                        SetLastReceiveTime();
                        onReceived(this, new TcpChannelReceivedEventArgs(lstBts.ToArray()));
                    }
                });
            }

            readingThread = new WorkerThread(e =>
            {
                if (onReadingThreadException != null)
                {
                    onReadingThreadException(this, new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "Receive", null, e));
                }
            });
            readingThread.Start(
                // ThreadStart Delegate
                () =>
            {
                if (IsFinishing || !IsWithinReceiveTimeout || !IsSocketConnected)
                {
                    throw new StopThreadException();
                }

                if (socket.Poll(POLL_TIMEOUT, SelectMode.SelectRead))
                {
                    if (socket.Available > 0)
                    {
                        // The socket is supposed to be connected and data are available.
                        byte[] buf = new byte[socket.Available];

                        int received = 0;
                        if ((received = socket.Receive(buf, 0, buf.Length, SocketFlags.None)) > 0 &&
                            onReceived != null && processingThread != null)
                        {
                            // Processing in dedicated thread
                            if (processingThread.IsThreadActive)
                            {
                                cqueBytes.Enqueue(buf);
                            }

                            processingThread.SetEvent();
                        }
                    }
                    else
                    if (onNoDataRead != null)
                    {
                        onNoDataRead(this, new TcpChannelNotifyEventArgs(EnumNotificationLevel.Debug, "Reading Thread, Start"));
                    }
                }
            },

                // AfterLoop Delegate
                () =>
            {
                WorkerThread.Stop(ref processingThread);

                if (IsSocketConnected)
                {
                    CloseSocket();
                }

                socket        = null;
                UnparsedBytes = null;

                bool isConnected = false;
                if (!IsFinishing && dlgtReconnect != null)
                {
                    isConnected = dlgtReconnect();      // reconnection after certain delay
                }
                if (!isConnected)
                {
                    Close();
                }
            }, 0);
        }
예제 #3
0
        public static void StartAcceptSubscribersOnPort(int acceptPort,
                                                        TcpChannelEventHandler <TcpChannelReceivedEventArgs> onReceived,
                                                        TcpChannelEventHandler <EventArgs> onInitConnectionToServer         = null,
                                                        TcpChannelEventHandler <TcpChannelNotifyEventArgs> onServerNotifies = null,
                                                        int socketReceiveTimeoutInSec = DEFAULT_RECEIVE_TIMEOUT_IN_SEC,
                                                        int socketSendTimeoutInSec    = DEFAULT_SEND_TIMEOUT_IN_SEC,
                                                        int socketReceiveBufferSize   = DEFAULT_RECEIVE_BUFFER_SIZE,
                                                        int socketSendBufferSize      = DEFAULT_SEND_BUFFER_SIZE)
        {
            if (onReceived == null)
            {
                throw new Exception("onReceived is null.");
            }

            EasyTcpServer.onServerNotifies += onServerNotifies;
            string processName = Process.GetCurrentProcess().ProcessName;

            AutoResetEvent evAccept    = new AutoResetEvent(false);
            TcpListener    tcpListener = null;

            try
            {
                tcpListener = StartListenOnPort(acceptPort);
            }
            catch (Exception e)
            {
                if (EasyTcpServer.onServerNotifies != null)
                {
                    EasyTcpServer.onServerNotifies(null
                                                   , new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "StartAcceptSubscribersOnPort", null, e));
                }
            }

            if (tcpListener != null)
            {
                WorkerThread wtAccept = new WorkerThread(e =>
                {
                    if (EasyTcpServer.onServerNotifies != null)
                    {
                        EasyTcpServer.onServerNotifies(null
                                                       , new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "StartAcceptSubscribersOnPort, Accept Worker Thread", null, e));
                    }
                });

                cdctThread[tcpListener] = wtAccept;

                wtAccept.Start(() =>
                {
                    AcceptBegin(tcpListener, evAccept,
                                tcpServer =>
                    {
                        if (tcpServer.IsSocketConnected)
                        {
                            tcpServer.onReceived += onReceived;
                            tcpServer.Receive();

                            if (onInitConnectionToServer != null)
                            {
                                onInitConnectionToServer(tcpServer, null);
                            }
                        }
                    },
                                ex =>
                    {
                        if (EasyTcpServer.onServerNotifies != null)
                        {
                            EasyTcpServer.onServerNotifies(null
                                                           , new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "StartAcceptSubscribersOnPort.AcceptBegin, AcceptEnd", null, ex));
                        }
                    },
                                socketReceiveTimeoutInSec, socketSendTimeoutInSec, socketReceiveBufferSize, socketSendBufferSize);

                    evAccept.WaitOne();

                    wtAccept.SetEvent();
                });

                wtAccept.SetEvent();
            }
        }