private void aceptClient(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { Socket client = e.AcceptSocket; if (client != null) { INetSession session = sessionFactory.CreateSession(this.Application, this.Protocol, this, client); this.Application.SessionCreated(session); session.SessionClosed += new EventHandler(session_SessionClosed); session.Start(); } else { maxConntectedSemaphore.Release(); } } else { maxConntectedSemaphore.Release(); } }
protected override bool InnerStart() { try { serverSocket = new Socket(config.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(EndPoint); serverSocket.Listen(config.Backlog); serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); } catch (Exception e) { return(false); } maxConntectedSemaphore = new Semaphore(config.MaxConnectionNumber, config.MaxConnectionNumber); Status = Dynamic.Net.Base.NetServerStatus.Started; StartupCompleted(); IsRunning = true; while (Status == Dynamic.Net.Base.NetServerStatus.Started) { Socket client = null; try { maxConntectedSemaphore.WaitOne(); if (Status != Dynamic.Net.Base.NetServerStatus.Started) { break; } client = serverSocket.Accept(); } catch (ObjectDisposedException) { break; } catch (NullReferenceException) { break; } catch (Exception e) { SocketException se = e as SocketException; if (se != null) { if (se.ErrorCode == 10004 || se.ErrorCode == 10038) { break; } } break; } if (client != null) { INetSession session = sessionFactory.CreateSession(this.Application, this.Protocol, this, client); this.Application.SessionCreated(session); session.SessionClosed += new EventHandler(session_SessionClosed); session.Start(); } else { maxConntectedSemaphore.Release(); } } IsRunning = false; return(true); }