void DatabaseThreadFunc() { while (true) { ValidateConnection(); if (_sql != null && _sql.State == ConnectionState.Open && _queries.Count > 0) { _queriesLock.WaitOne(); DBQuery query = _queries[0]; _queries.RemoveAt(0); _queriesLock.ReleaseMutex(); try { ExecuteQuery(query); } catch (Exception ex) { LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true); // When the query fails, its likely due to database disconnection. Reschedule this query to be done again. AddQuery(query); } } Thread.Sleep(100); } }
public ServerBase(int listenPort, string dbConnectString) { _rand = new Random(); _playServers = new List<PlayServerInfo>(); _expectedConnections = new Dictionary<string, int>(); IPAddress[] IPS = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress ip in IPS) { if (ip.AddressFamily == AddressFamily.InterNetwork) _playServers.Add(new PlayServerInfo("Main Server", ip.ToString(), (ushort)listenPort)); } // Start log thread LogThread log = new LogThread(); // Start database thread if( dbConnectString != null ) _db = new DatabaseThread(dbConnectString); // Start AI Thread _aiThread = new AIThread(); _listenPort = listenPort; // Start input thread _inputThread = new InputThread(); }
void ValidateConnection() { if (_sql == null) { try { _sql = new MySqlConnection(); _sql.ConnectionString = _dbConnectString; _sql.Open(); Thread.Sleep(10); while (_sql.State == ConnectionState.Connecting) { Thread.Sleep(100); } } catch (Exception ex) { LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true); _sql.Close(); _sql = null; } } else { if (_sql.State == ConnectionState.Closed || _sql.State == ConnectionState.Broken) { _sql.Close(); _sql = null; } } }
void ExecuteQuery(DBQuery q) { LogThread.Log(string.Format("ExecuteQuery - {0}", q.SqlString), LogThread.LogMessageType.Debug); MySqlCommand cmd = new MySqlCommand(q.SqlString, _sql); if (q.Read) { MySqlDataReader reader = cmd.ExecuteReader(); List <object[]> rows = new List <object[]>(); while (reader.Read()) { object[] row = new object[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { row[i] = reader[i]; } rows.Add(row); } reader.Close(); q.Rows = rows; } else { cmd.ExecuteNonQuery(); } OnQueryComplete(q, null); }
void ListenThreadFunc() { Socket listenSocket = null; try { listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint(IPAddress.Any, _port); listenSocket.Bind(ep); LogThread.Log("ListenThread: Listening on port " + _port, LogThread.LogMessageType.Normal, true); while (true) { listenSocket.Listen(10); Socket conn = listenSocket.Accept(); if (conn != null && OnConnectionAccepted != null) { LogThread.Log("ListenThread: Connection Accepted", LogThread.LogMessageType.Debug); OnConnectionAccepted(this, new SocketArg(conn)); } } } catch (Exception ex) { LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true); listenSocket.Close(); _theThread = null; } }
public void Process() { _processing = true; while (_processing) { try { if (_tasks.Count > 0) { // Grab the first task _tasksLock.WaitOne(); Task t = _tasks[0]; _tasks.RemoveAt(0); _tasksLock.ReleaseMutex(); // Execute the task ProcessTask(t); } } catch (Exception ex) { LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, false); } Thread.Sleep(10); } }
public ServerBase(int listenPort, string dbConnectString) { _rand = new Random(); _playServers = new List <PlayServerInfo>(); _expectedConnections = new Dictionary <string, int>(); IPAddress[] IPS = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress ip in IPS) { if (ip.AddressFamily == AddressFamily.InterNetwork) { _playServers.Add(new PlayServerInfo("Main Server", ip.ToString(), (ushort)listenPort)); } } // Start log thread LogThread log = new LogThread(); // Start database thread if (dbConnectString != null) { _db = new DatabaseThread(dbConnectString); } // Start AI Thread _aiThread = new AIThread(); _listenPort = listenPort; // Start input thread _inputThread = new InputThread(); }
void InputThreadFunc() { while (true) { try { _connectionsLock.WaitOne(); Connection[] connections = _connections.ToArray(); _connectionsLock.ReleaseMutex(); List <Connection> removeList = new List <Connection>(); foreach (Connection c in connections) { c.Update(); if (c.Status == Connection.ConnStatus.Closed) { Program.Server.RemoveCharacter(c.Character); removeList.Add(c); } } _connectionsLock.WaitOne(); foreach (Connection c in removeList) { _connections.Remove(c); } _connectionsLock.ReleaseMutex(); } catch (Exception ex) { LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true); } Thread.Sleep(10); } }
void ProcessTask(Task t) { // Call the task handler, this will throw an exception if the handler isnt registered. LogThread.Log(string.Format("ProcessTask({0}) -> {1}", t.Type, _taskHandlers[t.Type].Method.Name), LogThread.LogMessageType.Debug); _taskHandlers[t.Type](t); }