private void ThreadTask() { try { while (!m_Terminate) { Socket sckt = null; try { sckt = m_IncomingSocket.Accept(); } catch (SocketException e) { Trace(e); sckt = null; } if (sckt != null) { if (MaxConn == 0 || m_ConnList.Count < MaxConn) { var scktConn = new SocketConn(sckt, false); scktConn.OnEventTrace += ScktConn_OnEventTrace; scktConn.OnDisconnect += new SocketConn.DlgDisconnect(SocketConn_Disconnect); scktConn.OnSendError += new SocketConn.DlgSendError(SocketConn_SendError); if (!m_ConnList.TryAdd(scktConn, scktConn)) { Trace(EventType.Exception, "Couldn't add connection to socket server connection list. Refusing connection. ConnID: {0}", scktConn); sckt.Close(); } OnConnect?.Invoke(scktConn); if (m_StartScktConnReadThread) { scktConn.OnReceiveData += new SocketConn.DlgReceiveData(SocketConn_ReceiveData); scktConn.OnReceiveError += new SocketConn.DlgReceiveError(SocketConn_ReceiveError); scktConn.StartReadThread(); } if (m_AcceptDelay) { m_AcceptDelayWaitHandle.WaitOne(ACCEPT_DELAY, false); } } else { sckt.Close(); Trace( EventType.Warning, "Socket connection refused. Reached connection limit of {0} connections. {1}/{2}", MaxConn, sckt.LocalEndPoint, sckt.RemoteEndPoint); } } } } catch (Exception exc) { Trace(exc); } }
private void SocketConn_Disconnect(SocketConn scktConn) { if (!m_Terminate) { TryToReconnect(); OnDisconnect?.Invoke(scktConn); } }
private void SocketServer_OnDisconnect(SocketConn scktConn) { lock (m_CIPConnList) if (m_CIPConnList.ContainsKey(scktConn)) { m_CIPConnList.Remove(scktConn); } OnConnStatusChanged?.Invoke(ConnType.Receive, false, scktConn.ConnID); }
public CIPConn(SocketConn scktConn) { LOG_TAG += string.Format(" ({0})", scktConn.RemoteEndPoint); m_ActivityTimeRef = DateTime.Now; m_ScktConn = scktConn; m_ScktConn.OnDisconnect += ScktConn_OnDisconnect; m_ScktConn.OnReceiveData += ScktConn_OnReceiveData; m_ScktConn.OnReceiveError += ScktConn_OnReceiveError; m_ScktConn.OnSendError += ScktConn_OnSendError; }
private void ScktConn_OnReceiveData(SocketConn scktConn, byte[] data) { try { ReceiveBytes(data); } catch (Exception e) { Trace(e); } }
private void SocketServer_OnConnect(SocketConn scktConn) { lock (m_CIPConnList) { CIPConn cipConn = new CIPConn(scktConn); cipConn.OnEventTrace += CIPConn_OnEventTrace; cipConn.OnReceiveData += OnConnRecReceivedDataMsg; cipConn.Open(); m_CIPConnList.Add(scktConn, cipConn); } OnConnStatusChanged?.Invoke(ConnType.Receive, true, scktConn.ConnID); }
private void ScktConn_OnDisconnect(SocketConn scktConn) { try { Trace(EventType.Info, string.Format("{0} - Connection is closed: {1}", LOG_TAG, scktConn.RemoteEndPoint)); Close(); } catch (Exception e) { Trace(e); } }
public void TryToReconnect() { try { m_SocketConn?.Close(); } catch (Exception exc) { Trace(exc); } finally { m_SocketConn = null; } }
public void CloseConnection(SocketConn scktConn) { scktConn.Close(); if (m_ConnList.TryRemove(scktConn, out SocketConn removed)) { Trace(EventType.Data, "Removed connection {0} from the socket server connection list", removed); } else { Trace(EventType.Exception, "Removing connection from the socket server connection list: {0}", scktConn); } }
private void SocketConn_Disconnect(SocketConn scktConn) { if (m_ConnList.TryRemove(scktConn, out SocketConn removed)) { Trace(EventType.Data, "Removed connection {0} from the socket server connection list by disconnection event", removed); } else { Trace(EventType.Exception, "Removing connection {0} from the socket server connection list by disconnection event", scktConn); } OnDisconnect?.Invoke(scktConn); }
private void ThreadTask() { try { while (!m_Terminate) { if (m_SocketConn == null || !m_SocketConn.Connected) { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(m_RemoteAddress, m_RemotePort); m_SocketConn = new SocketConn(socket, false); m_SocketConn.OnEventTrace += ScktConn_OnEventTrace; m_SocketConn.OnDisconnect += new SocketConn.DlgDisconnect(SocketConn_Disconnect); m_SocketConn.OnSendError += new SocketConn.DlgSendError(SocketConn_SendError); if (m_StartScktConnReadThread) { m_SocketConn.OnReceiveData += new SocketConn.DlgReceiveData(SocketConn_ReceiveData); m_SocketConn.OnReceiveError += new SocketConn.DlgReceiveError(SocketConn_ReceiveError); m_SocketConn.StartReadThread(); } OnConnect?.Invoke(m_SocketConn); } catch (Exception e) { Trace(EventType.Error, "Exception connecting the socket. Remote endpoint: {0}", RemoteEndPoint); OnConnectError?.Invoke(e); TryToReconnect(); } } m_ReconnectSignal.WaitOne(RETRY_CONNECT); } } catch (Exception exc) { Trace(exc); } }
private void SocketClient_OnDisconnect(SocketConn scktConn) { m_ClientConnStates = ClientConnStates.Disconnected; Trace(EventType.Info, string.Format("{0} - Connection is closed: {1}", LOG_TAG, scktConn.RemoteEndPoint)); OnConnStatusChanged?.Invoke(ConnType.Send, false, scktConn.ConnID); }
private void SocketClient_OnConnect(SocketConn scktConn) { m_ClientConnStates = ClientConnStates.SendListServices; Trace(EventType.Info, string.Format("{0} - Connection established: {1}", LOG_TAG, scktConn.RemoteEndPoint)); OnConnStatusChanged?.Invoke(ConnType.Send, true, scktConn.ConnID); }
private void SocketConn_SendError(SocketConn scktConn, Exception scktExp) { OnSendError?.Invoke(scktExp); }
private void SocketConn_ReceiveError(SocketConn scktConn, Exception scktExp) { OnReceiveError?.Invoke(scktExp); }
private void SocketConn_ReceiveData(SocketConn scktConn, byte[] data) { OnReceiveData?.Invoke(data); }
public bool SendData(SocketConn scktConn, byte[] data) { return(scktConn.SendData(data)); }
private void ScktConn_OnSendError(SocketConn scktConn, Exception scktExp) { Trace(EventType.Error, string.Format("{0} - Send socket error {1}", LOG_TAG, scktExp.Message)); }