示例#1
0
        partial void InternalStart()
        {
            var ip = IPAddress.Any;

            if (!string.IsNullOrEmpty(BoundHost))
            {
                ip = BoundHost.GetIPAddress();
            }

            var ep = new IPEndPoint(ip, (int)BoundPort);

            _listener = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                Blocking = true
            };
            _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
            _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
            _listener.Bind(ep);
            _listener.Listen(5);

            Session.ErrorOccured += Session_ErrorOccured;
            Session.Disconnected += Session_Disconnected;

            _listenerCompleted = new ManualResetEvent(false);

            ExecuteThread(() =>
            {
                try
                {
                    while (true)
                    {
                        // accept new inbound connection
                        var asyncResult = _listener.BeginAccept(AcceptCallback, _listener);
                        // wait for the connection to be established
                        asyncResult.AsyncWaitHandle.WaitOne();
                    }
                }
                catch (ObjectDisposedException)
                {
                    // BeginAccept will throw an ObjectDisposedException when the
                    // socket is closed
                }
                catch (Exception ex)
                {
                    RaiseExceptionEvent(ex);
                }
                finally
                {
                    if (Session != null)
                    {
                        Session.ErrorOccured -= Session_ErrorOccured;
                        Session.Disconnected -= Session_Disconnected;
                    }

                    // mark listener stopped
                    _listenerCompleted.Set();
                }
            });
        }
示例#2
0
        partial void InternalStart()
        {
            var addr = BoundHost.GetIPAddress();
            var ep   = new IPEndPoint(addr, (int)BoundPort);

            _listener = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                Blocking = true
            };
            _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
            _listener.Bind(ep);
            _listener.Listen(1);

            // update bound port (in case original was passed as zero)
            BoundPort = (uint)((IPEndPoint)_listener.LocalEndPoint).Port;

            Session.ErrorOccured += Session_ErrorOccured;
            Session.Disconnected += Session_Disconnected;

            _listenerTaskCompleted = new ManualResetEvent(false);

            ExecuteThread(() =>
            {
                try
                {
                    while (true)
                    {
                        // accept new inbound connection
                        var asyncResult = _listener.BeginAccept(AcceptCallback, _listener);
                        // wait for the connection to be established
                        asyncResult.AsyncWaitHandle.WaitOne();
                    }
                }
                catch (ObjectDisposedException)
                {
                    // BeginAccept will throw an ObjectDisposedException when the
                    // socket is closed
                }
                catch (Exception ex)
                {
                    RaiseExceptionEvent(ex);
                }
                finally
                {
                    // mark listener stopped
                    _listenerTaskCompleted.Set();
                }
            });
        }