/// <summary> /// Starts the socket listener /// </summary> public void Start() { try { if (WriteLogs) { Logs.WriteLog("black", "Starting Server"); } SocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType); SocketServer.Bind(new IPEndPoint(IPAddress, Port)); SocketServer.Listen(10); InvokeUI.UpdateConnectionsCount(ConnectionList.Count); SocketServer.BeginAccept(new AsyncCallback(OnConnect), SocketServer); if (WriteLogs) { Logs.WriteLog("green", "Socket Listener Succesfully Running On Port [{0}]", Port); } IsAlive = true; } catch (Exception Ex) { if (WriteDebugLogs) { Logs.WriteLog("red", "Failed To Set Up Connection Listener On Port [{0}]", Port); Logs.WriteLog("red", Ex.Message); } } }
/// <summary> /// Kill specific connection /// </summary> /// <param name="Connection"></param> public void CloseConnection(Connection Connection) { try { if (ConnectionList.Any(wr => wr.Value == Connection)) { Connection.Close(); ConnectionList.Remove(Connection.cIndex); if (WriteLogs) { Logs.WriteLog("black", "Closed Connection with index [{0}] ", Connection.cIndex); } } InvokeUI.UpdateConnectionsCount(ConnectionList.Count); } catch (Exception Ex) { if (WriteDebugLogs) { Logs.WriteLog("red", Ex.Message); } } }
private void CreateConnection(Socket socket) { try { Connection conn = new Connection(socket, this); int index = Helpers.GetFirstIndexFromList(ConnectionList); conn.cIndex = index; ConnectionList.Add(index, conn); conn.StartReceiveData(); InvokeUI.UpdateConnectionsCount(ConnectionList.Count); if (WriteLogs) { Logs.WriteLog("green", "Created connection for IP [{0}]", conn.IpAddress); } if (SendHello) { ProtocolCore.SendHelloMessage(index); } } catch (Exception Ex) { if (WriteDebugLogs) { Logs.WriteLog("red", Ex.Message); } } }
/// <summary> /// Kill connection with specific index /// </summary> /// <param name="cIndex"></param> public void CloseConnection(int cIndex) { try { ConnectionList[cIndex].Close(); ConnectionList.Remove(cIndex); if (WriteLogs) { Logs.WriteLog("black", "Closed Connection with index [{0}]", cIndex); } InvokeUI.UpdateConnectionsCount(ConnectionList.Count); } catch (Exception Ex) { if (WriteDebugLogs) { Logs.WriteLog("red", Ex.Message); } } }
/// <summary> /// Kills all alive connections /// </summary> public void KillAllConnections() { try { foreach (var conn in ConnectionList) { if (conn.Value.ConnectionSocket.Connected) { conn.Value.Close(); } } ConnectionList.Clear(); InvokeUI.UpdateConnectionsCount(ConnectionList.Count); } catch (Exception Ex) { if (WriteDebugLogs) { Logs.WriteLog("red", Ex.Message); } } }