fork download
  1. /// <summary>
  2. /// IP-aдрес сервреа по-умолчанию
  3. /// Очевидный localhost
  4. /// </summary>
  5. private static IPAddress _defaultIPAddress = IPAddress.Parse("127.0.0.1");
  6.  
  7. /// <summary>
  8. /// Порт сервера по-умлочанию
  9. /// </summary>
  10. private static int _defaultPort = 9000;
  11.  
  12. /// <summary>
  13. /// Конечная точка сервреа по-умлочанию
  14. /// </summary>
  15. private static IPEndPoint _defaultEndPoint = new IPEndPoint(_defaultIPAddress, _defaultPort);
  16.  
  17. #endregion Default
  18.  
  19. #region Private vars
  20.  
  21. private TcpListener _listener = null;
  22. private bool _isStoped = false;
  23. private bool _isPurgingStoped = false;
  24. private Task _listeningTask = null;
  25. private Task _purgingTask = null;
  26. private ConcurrentDictionary<Guid, TcpSession<T>> _sessions = null;//сессии сервера
  27. private object locker = new object(); //объект синхронизации потоков
  28. private DateTime _startTime;
  29. private CancellationToken cancellationToken;
  30.  
  31. #endregion Private vars
  32.  
  33. #region Props
  34.  
  35. public int ConnectedSessionCount => this._sessions.Where(kv => !kv.Value.IsStoped).Count();
  36. public TimeSpan UpTime => DateTime.Now - _startTime;
  37. public bool IsRuning => !_isStoped;
  38.  
  39. #endregion Props
  40.  
  41. #region ctors
  42.  
  43. public TcpServer() : this(_defaultEndPoint)
  44. {
  45. }
  46.  
  47. public TcpServer(int port) : this(new IPEndPoint(_defaultIPAddress, port))
  48. {
  49. }
  50.  
  51. public TcpServer(IPAddress address) : this(new IPEndPoint(address, _defaultPort))
  52. {
  53. }
  54.  
  55. public TcpServer(IPAddress address, int port) : this(new IPEndPoint(address, port))
  56. {
  57. }
  58.  
  59. public TcpServer(IPEndPoint endPoint)
  60. {
  61. Init(endPoint);
  62. }
  63.  
  64. #endregion ctors
  65.  
  66. #region Public methods
  67.  
  68. public void Start(CancellationToken cancellationToken)
  69. {
  70. this.cancellationToken = cancellationToken;
  71. if(_listener != null)
  72. {
  73. _startTime = DateTime.Now;
  74. _sessions = new ConcurrentDictionary<Guid, TcpSession<T>>();
  75. _listener.Start();
  76.  
  77. _listeningTask = new Task(ServerListeningTask);
  78. _listeningTask.Start();
  79.  
  80. //Создаем nfcr очистки сессий
  81. _purgingTask = new Task(ServerPurgingTask);
  82. _purgingTask.Start();
  83. }
  84. }
  85.  
  86. public void Stop()
  87. {
  88. if(_listener != null || cancellationToken.IsCancellationRequested)
  89. {
  90. try
  91. {
  92. _isStoped = true;
  93. _listener.Stop();
  94. _listeningTask.Wait();
  95. _listeningTask = null;
  96. _purgingTask.Wait();
  97. _purgingTask = null;
  98. _listener = null;
  99. StopAllClientSessions();
  100. }
  101. catch
  102. {
  103. }
  104. }
  105. }
  106.  
  107. #endregion Public methods
  108.  
  109. #region Private methods
  110.  
  111. private void Init(IPEndPoint endPoint)
  112. {
  113. try
  114. {
  115. _listener = new TcpListener(endPoint);
  116. }
  117. catch(Exception ex)
  118. {
  119. _listener = null;
  120. }
  121. }
  122.  
  123. private void ServerListeningTask()
  124. {
  125. Socket clientSocket = null;
  126. TcpSession<T> clientSession = null;
  127. while(!_isStoped || !this.cancellationToken.IsCancellationRequested)
  128. {
  129. try
  130. {
  131. clientSocket = _listener.AcceptSocket();
  132. clientSession = new TcpSession<T>(clientSocket);
  133. lock(locker)
  134. {
  135. _sessions.TryAdd(clientSession.ID, clientSession);
  136. }
  137. clientSession.StartSession(this.cancellationToken);
  138. }
  139. catch(SocketException ex)
  140. {
  141. _isStoped = true;
  142. }
  143. }
  144. }
  145.  
  146. private void ServerPurgingTask()
  147. {
  148. while(!_isPurgingStoped || !cancellationToken.IsCancellationRequested)
  149. {
  150. var disconnected = _sessions.Where(s => s.Value.IsStoped == true);
  151. lock(locker)
  152. {
  153. foreach(var s in disconnected)
  154. {
  155. _sessions.TryRemove(s.Key, out var temp);
  156. }
  157. }
  158. disconnected = null;
  159. Thread.Sleep(1000);
  160. }
  161. }
  162.  
  163. private void StopAllClientSessions()
  164. {
  165. foreach(var kv in _sessions)
  166. kv.Value.StopSession();
  167. }
  168.  
  169. #endregion Private methods
  170.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(5,23): error CS1525: Unexpected symbol `IPAddress', expecting `class', `delegate', `enum', `interface', `partial', `ref', or `struct'
prog.cs(18,0): error CS1028: Unexpected processor directive (no #region for this #endregion)
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty