Пример #1
0
        internal void Stop()
        {
            if (Winsock.WSAWaitForMultipleEvents(1, new IntPtr[] { _stop }, 1 /*true*/, 0, 0 /*false*/) == Winsock.WSA_WAIT_EVENT_0)
            {
                Trace.WriteLine("[RCS] Second call to Stop().");
                return;
            }
            Winsock.WSASetEvent(_stop);
            Trace.WriteLine("[RCS] Stop event has been fired.");

            try
            {
                Trace.WriteLine("[RCS] Waiting for server thread...");
                if (!Application.MessageLoop)
                {
                    _mainThread.Join();
                }
                else
                {
                    while (_mainThread.IsAlive && !_mainThread.Join(100))
                    {
                        Application.DoEvents();
                    }
                }
                Trace.WriteLine("[RCS] Server thread has finished.");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("[RCS] Server thread waiting exception: " + ex.Message);
            }
            finally
            {
                _mainThread = null;
            }
            Winsock.WSACloseEvent(_stop);
        }
Пример #2
0
        private void Listener()
        {
            IntPtr socketEvent = Winsock.WSACreateEvent();

            IntPtr[] allEvents = { _stop, socketEvent };
            IntPtr   handle    = _listenSocket.Handle;

            Winsock.WSAEventSelect(handle, socketEvent, Winsock.FD_ACCEPT);
            while (true)
            {
                try
                {
                    uint res = Winsock.WSAWaitForMultipleEvents(2, allEvents, 0 /*false*/, Winsock.WSA_INFINITE, 1 /*true*/);
                    if (res == Winsock.WSA_WAIT_EVENT_0)
                    {
                        Trace.WriteLine("[RCS] Event 0 (STOP) signalled");
                        break;
                    }
                    else if (res == Winsock.WSA_WAIT_EVENT_0 + 1)
                    {
                        Winsock.WSAResetEvent(socketEvent);
                        Trace.WriteLine("[RCS] Event 1 (ACCEPT) signalled");

                        Socket cs = _listenSocket.Accept();
                        Winsock.WSAEventSelect(cs.Handle, socketEvent, 0);
                        cs.Blocking = true;

                        Trace.WriteLine("[RCS] Client accepted at socket " + cs.Handle.ToString());
                        ThreadPool.QueueUserWorkItem(_onAccept, cs);
                    }
                    else
                    {
                        Trace.WriteLine("[RCS] Event " + res.ToString() + " (?) signalled");
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("[RCS] Accept exception: " + ex.Message);
                    // If not sopped: sleep and try again
                    if (Winsock.WSAWaitForMultipleEvents(1, new IntPtr[] { _stop }, 1 /*true*/, 0, 0 /*false*/) != Winsock.WSA_WAIT_EVENT_0)
                    {
                        Trace.WriteLine("[RCS] It is not stop!");
                        Thread.Sleep(100);
                    }
                    else
                    {
                        Trace.WriteLine("[RCS] It is stop, exiting.");
                        // Stopped: exit
                        break;
                    }
                }
            }
            try
            {
                Trace.WriteLine("[RCS] Closing main socket " + handle.ToString() + " ...");
                _listenSocket.Close();
                Trace.WriteLine("[RCS] Main socket " + handle.ToString() + " closed.");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("[RCS] Main socket closing exception: " + ex.Message);
            }
            finally
            {
                _listenSocket = null;
            }
            Winsock.WSACloseEvent(socketEvent);
        }