/// <summary> /// Queries the database, returns an datatable. /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable GetMultiQuery(string sql) { DataTable dt = new DataTable(); try { SQLiteConnection SConnection = new SQLiteConnection(); SConnection.ConnectionString = "Data Source=users.siu;" + "UseUTF16Encoding=True;" + "Legacy Format=False;"; SConnection.Open(); SQLiteCommand Command = new SQLiteCommand(SConnection); Command.CommandText = sql; SQLiteDataReader Reader = Command.ExecuteReader(); dt.Load(Reader); Reader.Close(); Reader.Dispose(); Command.Dispose(); SConnection.Close(); SConnection.Dispose(); } catch (Exception e) { SiusLog.Log(SiusLog.WARNING, "GetQuery", e.Message); } return(dt); }
/// <summary> /// RequestStop() will set the connected status to false, /// abort the thread, and close the socket. /// </summary> public void RequestStop() { if (thrListener != null && thrListener.IsAlive) // thread is active { // set event "Stop" Sius.Connected = false; thrListener.Abort(); Listening.Stop(); TcpClient[] tcpClients = new TcpClient[htRcon.Count]; htRcon.Values.CopyTo(tcpClients, 0); for (int i = 0; i < tcpClients.Length; i++) { try { if (tcpClients[i] != null) { tcpClients[i].Close(); } } catch (Exception e) { SiusLog.Log(SiusLog.DEBUG, "disconnect", e.Message); } } } }
public static void Connect() { try { SQLiteConnection SConnection = new SQLiteConnection(); SConnection.ConnectionString = "Data Source=users.siu;" + "UseUTF16Encoding=True;" + "Legacy Format=False;"; SConnection.Open(); CreatePlayersTable(SConnection); CreateBannersTable(SConnection); CreateMessagesTable(SConnection); CreateSquadsTable(SConnection); SiusLog.Log(SiusLog.DEBUG, "SQLite", "Attempted to create all necessary database tables."); SConnection.Close(); SConnection.Dispose(); } catch (Exception e) { SiusLog.Log(SiusLog.ERROR, "SQLite", e.Message); } }
/// <summary> /// Begin listening for incoming TCP connections on the specified port number /// </summary> public void StartListening() { int port = SiusConf.GetSetting.Integer("port"); // TCP port numbers are 16 bits in length, so they can only be within the range of 0 - 65535 if (port <= 0 || port >= 65535) { SiusLog.Log(SiusLog.ERROR, "server", "An error occured when trying to define a port number. Please make sure the port is within the range of 1 - 65534"); Sius.Connected = false; return; } string address = SiusConf.GetSetting.String("ip"); IPAddress localAddr; // check if the IP Address is valid if (SiusUtil.IsValidIP(address) == false) { SiusLog.Log(SiusLog.ERROR, "server", "Unable to retrieve IP Address. Please make sure that you are using a valid IP Address such as 127.0.0.1"); Sius.Connected = false; return; } else { localAddr = IPAddress.Parse(address); } // Log our port and ip address SiusLog.Log(SiusLog.INFORMATION, "server", "Server port: " + port + ", Server IP: " + address); // Create TCP Listener and start checking for incoming connections try { Listening = new TcpListener(localAddr, port); Listening.Start(); } catch (SocketException e) { SiusLog.Log(SiusLog.ERROR, "connect", e.Message); Sius.Connected = false; return; } // Server is connected! Sius.Connected = true; // Start a new thread thrListener = new Thread(KeepListening); thrListener.Start(); }
/// <summary> /// Creates the initial "banners" table. /// ID - NAME - BANNER /// </summary> private static void CreateBannersTable(SQLiteConnection SConnection) { try { SQLiteCommand Command = new SQLiteCommand(SConnection); Command.CommandText = "CREATE TABLE IF NOT EXISTS [banners] (" + "[name] TEXT COLLATE NOCASE UNIQUE NOT NULL," + "[banner] TEXT NOT NULL" + ")"; Command.ExecuteNonQuery(); } catch (Exception e) { SiusLog.Log(SiusLog.ERROR, "SQLite", e.Message); } }
private void KeepListening() { // While the server is running while (Sius.Connected == true) { try { // Accept a pending connection tcpClient = Listening.AcceptTcpClient(); // Create a new instance of Connection Connection newConnection = new Connection(tcpClient); } catch (SocketException e) { SiusLog.Log(SiusLog.WARNING, "connect", e.Message); } } }
/// <summary> /// Creates the initial "messages" table. /// ID - NAME - MESSAGE - FLAG /// </summary> private static void CreateMessagesTable(SQLiteConnection SConnection) { try { SQLiteCommand Command = new SQLiteCommand(SConnection); Command.CommandText = "CREATE TABLE IF NOT EXISTS [messages] (" + "[name] TEXT COLLATE NOCASE NOT NULL," + "[sender] TEXT COLLATE NOCASE NOT NULL," + "[message] TEXT NOT NULL," + "[time] TEXT NULL," + "[flag] INTEGER DEFAULT 0" + ")"; Command.ExecuteNonQuery(); } catch (Exception e) { SiusLog.Log(SiusLog.ERROR, "SQLite", e.Message); } }
/// <summary> /// Will send a sql query without expecting a result returned. /// </summary> /// <param name="sql"></param> public static void SendQuery(string sql) { try { SQLiteConnection SConnection = new SQLiteConnection(); SConnection.ConnectionString = "Data Source=users.siu;UseUTF16Encoding=True;Legacy Format=False;"; SConnection.Open(); SQLiteCommand Command = new SQLiteCommand(SConnection); Command.CommandText = sql; Command.ExecuteNonQuery(); Command.Dispose(); SConnection.Close(); SConnection.Dispose(); } catch (Exception e) { SiusLog.Log(SiusLog.DEBUG, "SendQuery", e.Message); } }
/// <summary> /// Creates the initial "players" table. /// ID - NAME - PASSWORD - USAGE - CREATED - LASTLOGIN /// </summary> private static void CreatePlayersTable(SQLiteConnection SConnection) { try { SQLiteCommand Command = new SQLiteCommand(SConnection); Command.CommandText = "CREATE TABLE IF NOT EXISTS [players] (" + "[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + "[name] TEXT COLLATE NOCASE UNIQUE NOT NULL," + "[password] TEXT NULL," + "[salt] TEXT NULL," + "[squad] TEXT COLLATE NOCASE NULL," + "[usage] INTEGER DEFAULT 0," + "[created] TEXT NULL," + "[lastseen] TEXT DEFAULT CURRENT_TIMESTAMP NULL" + ")"; Command.ExecuteNonQuery(); } catch (Exception e) { SiusLog.Log(SiusLog.ERROR, "SQLite", e.Message); } }
static void Main(string[] args) { try { // Add to the console Console.Title = "Sius"; Console.WriteLine("Sius"); Console.Write("Press any key to exit ..." + Environment.NewLine); Time = DateTime.Now; // Check if we are logging if (!string.IsNullOrEmpty(SiusConf.GetSetting.String("logging"))) { SiusLog.Logging = true; } // Check if we want to clear the log before starting if (SiusConf.GetSetting.Boolean("clearlog")) { SiusLog.ClearLogFile(); } // Create the SQLite database and tables SQLite.Connect(); // Log that Sius has successfully started SiusLog.Log(SiusLog.INFORMATION, "server", "Sius successfully started."); // Initialize the Transmission Control Protocol (TCP) connection Listen tcp = new Listen(); tcp.StartListening(); // Start the Ping timer System.Timers.Timer SendPing = new System.Timers.Timer(); SendPing.Elapsed += new ElapsedEventHandler(Protocol.Ping.OnTimedEvent); if (SiusConf.GetSetting.Integer("ping") >= 10) { SendPing.Interval = SiusConf.GetSetting.Integer("ping") * 1000; } else { SendPing.Interval = 150000; // 150 seconds } SendPing.Enabled = true; SendPing.Start(); // Start the Scorereset timer System.Timers.Timer Scorereset = new System.Timers.Timer(); Scorereset.Elapsed += new ElapsedEventHandler(Protocol.Scorereset.OnTimedEvent); if (SiusConf.GetSetting.Integer("scorereset") >= 1) { Scorereset.Interval = SiusConf.GetSetting.Integer("scorereset") * 3600000; } else { Scorereset.Interval = 1209600000; // 2 weeks } Scorereset.Enabled = true; Scorereset.Start(); // Wait until we're told to exit the program while (Running == true) { // press any key to exit Console.ReadKey(true); Running = false; SendPing.Close(); Scorereset.Close(); } // Cleanup and end all TCP connections Sius.Connected = false; SiusLog.Log(SiusLog.INFORMATION, "server", "Exiting"); tcp.RequestStop(); // Append a line to the log to mark the end of a run. SiusLog.DirectLogToFile("-----------------------------------------------------"); } catch (Exception e) { // Log any unexpected errors SiusLog.Log(SiusLog.ERROR, "server", "Error: " + e.StackTrace); } }
// Occurs when a new client is accepted private void AcceptClient() { srReceiver = new StreamReader(tcpClient.GetStream()); swSender = new StreamWriter(tcpClient.GetStream()); // Read the account information from the client strReceive = srReceiver.ReadLine(); // We received a response from the zone if (!string.IsNullOrEmpty(strReceive)) { //SiusLog.Log(SiusLog.INFORMATION, "zone", strReceive); string respond = Protocol.Connect.Try(strReceive); if (!string.IsNullOrEmpty(respond)) { //SiusLog.Log(SiusLog.DEBUG, "zone", respond); //CONNECTOK swSender.WriteLine(respond); swSender.Flush(); } if (respond.StartsWith("CONNECTBAD")) { CloseConnection(); } } else { CloseConnection(); return; } // Add the zone to the hash table Zone.AddZone(tcpClient); // try // { // Keep waiting for a message from the user while ((Sius.Connected == true) && thrSender.IsAlive) { if (!string.IsNullOrEmpty(strReceive = srReceiver.ReadLine())) { //SiusLog.Log(SiusLog.INFORMATION, "zone", strReceive); string respond = Message.Receive(strReceive); if (!string.IsNullOrEmpty(respond)) { try { //SiusLog.Log(SiusLog.DEBUG, "zone", respond); swSender.WriteLine(respond); swSender.Flush(); } catch (Exception e) { SiusLog.Log(SiusLog.DEBUG, "connect", e.Message); } } } else { break; } } // } // catch (Exception e) // { // SiusLog.Log(SiusLog.MALICIOUS, "disconnect", e.Message); // } //Disconnect Zone.RemoveZone(tcpClient); SiusLog.Log(SiusLog.INFORMATION, "disconnect", Zone.ZoneName + " disconnected from network."); CloseConnection(); }