namespace Serveur { abstract class ServeurTcp : IServeur { private TcpListener _serveur; public bool Connect(ushort port) { try { _serveur = new TcpListener(IPAddress.Any, port); _serveur.Start(); } catch (SocketException e) { if (e.SocketErrorCode == SocketError.AddressAlreadyInUse) return false; throw; } return true; } protected abstract Action Pooling { get; } public void Join() { try { while (true) { TcpClient client = _serveur.AcceptTcpClient(); ThreadPool.QueueUserWorkItem((objet) => Pooling(objet), new Tuple(client, this)); } } catch (SocketException e) { // On ignore les erreurs d'interruption d'écoute. if (e.SocketErrorCode != SocketError.Interrupted) { throw; } } } public void StopListening() { _serveur.Stop(); } } }