/// <summary> /// Stops server. /// </summary> public override void Stop() { if (!IsStarted) { return; } _listener.Stop(); if (_acceptThread != null && _acceptThread.IsAlive) { _acceptThread.Interrupt(); _acceptThread.Join(); } IsStarted = false; while (SocketClients.Count > 0) { SocketClientBase socketClient = SocketClients[0]; socketClient.Disconnect(); if (SocketClients.Contains(socketClient)) { SocketClients.Remove(socketClient); } } }
/// <summary> /// Accepts clients in the separate thread. /// </summary> private void AcceptConnections() { try { _listener.Start(); while (true) { Socket socket = _listener.AcceptSocket(); //Waiting var client = new SocketClient(socket); client.Context = this.Context; client.Connected += (s, e) => { OnConnected(e); }; client.Disconnected += SocketClientDisconnected; client.ReceivedData += (s, e) => { OnReceivedData(e); }; client.SentData += (s, e) => { OnSentData(e); }; client.Error += (s, e) => { OnError(e); }; client.SocketError += (s, e) => { OnSocketError(e); }; SocketClients.Add(client); client.BeginReceive(); } } catch (SocketException e) { if (e.ErrorCode != 10004) { OnSocketError(new SocketErrorEventArgs(LocalEndPoint, e)); } } catch (Exception e) { OnError(new ErrorEventArgs(LocalEndPoint, e)); } }
/// <summary> /// Ends the pending of asynchronous accept of new clients. /// </summary> /// <param name="ar"></param> private void AcceptCallBack(IAsyncResult ar) { try { var listener = (TcpListener)ar.AsyncState; if (ServerStatus != SocketServerStatus.Start || !listener.Server.IsBound) { return; } Socket socket = listener.EndAcceptSocket(ar); var client = new SocketClient(socket); client.Context = this.Context; client.Connected += (s, e) => { OnConnected(e); }; client.Disconnected += SocketClientDisconnected; client.ReceivedData += (s, e) => { OnReceivedData(e); }; client.SentData += (s, e) => { OnSentData(e); }; client.Error += (s, e) => { OnError(e); }; client.SocketError += (s, e) => { OnSocketError(e); }; SocketClients.Add(client); client.BeginReceive(); ContinueAcceptConnections(); } catch (SocketException e) { if (e.ErrorCode != 10004) { OnSocketError(new SocketErrorEventArgs(LocalEndPoint, e)); } } catch (Exception e) { OnError(new ErrorEventArgs(LocalEndPoint, e)); } }
internal void RemoveSocketClientSubcription(string theSocketClientGuid, string theSubcriptionGuid) { SocketClientComponent SocketClient = SocketClients.FirstOrDefault(c => c.Guid == theSocketClientGuid); if (SocketClient != null) { SocketClient.RemoveSubcription(theSubcriptionGuid); } }
private void SocketClientDisconnected(object sender, SocketEventArgs e) { var socketClient = sender as SocketClientBase; if (socketClient != null && SocketClients.Contains(socketClient)) { SocketClients.Remove(socketClient); } OnDisconnected(e); }
/// <summary> /// Stops server. /// </summary> public override void Stop() { if (ServerStatus == SocketServerStatus.Stop) { return; } ServerStatus = SocketServerStatus.Stop; _listener.Stop(); while (SocketClients.Count > 0) { var socketClient = SocketClients[0]; socketClient.Disconnect(); if (SocketClients.Contains(socketClient)) { SocketClients.Remove(socketClient); } } }
internal void Update(SocketClientProperties[] theSocketClientProperties) { foreach (var item in theSocketClientProperties) { SocketClientComponent SocketClient = SocketClients.FirstOrDefault(c => c.Guid == item.Guid); if (SocketClient != null) { SocketClient.UpdateProperties(item); } else { if (!string.IsNullOrEmpty(item.Guid)) { var Logger = MultiPlugServices.Logging.New(item.Guid, Diagnostics.EventLogDefinitions.DefinitionsId); SocketClient = new SocketClientComponent(item.Guid, Logger); Add(new SocketClientComponent[] { SocketClient }); SocketClient.UpdateProperties(item); } } } }
/// <summary> /// Ends the pending of asynchronous accept of new clients. /// </summary> /// <param name="ar"></param> private void AcceptCallBack(IAsyncResult ar) { try { var listener = (TcpListener)ar.AsyncState; if (ServerStatus != SocketServerStatus.Start || !listener.Server.IsBound) { return; } Socket socket = listener.EndAcceptSocket(ar); var client = new SocketClientSsl(socket); client.Context = this.Context; client.Connected += (s, e) => { OnConnected(e); }; client.Disconnected += SocketClientDisconnected; client.ReceivedData += (s, e) => { OnReceivedData(e); }; client.SentData += (s, e) => { OnSentData(e); }; client.Error += (s, e) => { OnError(e); }; client.SocketError += (s, e) => { OnSocketError(e); }; client.SocketSecurity += (s, e) => { OnSocketSecurity(e); }; SocketClients.Add(client); client.BeginAuthenticateAsServer(ClientCertificateRequired, CheckCertificateRevocation, ServerCertificate, CertificateValidationCallback, SslProtocol); ContinueAcceptConnections(); } catch (SocketException e) { if (e.ErrorCode != 10004) { OnSocketError(new SocketErrorEventArgs(LocalEndPoint, e)); } } catch (Exception e) { OnError(new ErrorEventArgs(LocalEndPoint, e)); } }