//IServerConnectoin<ClientDefault, ClientPingDefault> public void DispatchOrdersToClients(IServerConnectoin <ClientDefault, ClientPingDefault> conn, int frame, byte[] data) { var from = conn != null ? conn.PlayerIndex : 0; foreach (var c in Conns.Except(conn).ToList()) { DispatchOrdersToClient(c, from, frame, data); } }
/// <summary> /// 根据模板设置 conn /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="serverName"></param> /// <param name="userId"></param> /// <param name="userPwd"></param> public void SetConn(string host, string port, string serverName, string userId, string userPwd) { conn = connModel.Replace("@Host", host).Replace("@Port", port).Replace("@ServerName", serverName).Replace("@UserId", userId).Replace("@UserPwd", userPwd); string key = $"{userId}@{serverName}/{port}"; if (!Conns.ContainsKey(key)) { Conns.Add(key, conn); } }
/// <summary> /// Allow to initialise many Connection string of many Database and return them. /// </summary> /// <param name="connections">List Object of Connection class</param> /// <param name="connectionTypes">List Object of ConnectionType Enumeration</param> /// <returns>List Object IDbConnection</returns> public List <IDbConnection> InitialiseMultipleConnections(List <Connection> connections, List <ConnectionType> connectionTypes) { int i = 0; foreach (ConnectionType typeBD in connectionTypes) { switch (typeBD) { case ConnectionType.SQLServer: { Conn = new SqlConnection(String.Format("Data Source={0};Initial catalog={1};User ID={2};Password={3}", connections.ElementAt(i).Serveur, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password)); Conns.Add(Conn); break; } case ConnectionType.MySQL: { Conn = new MySqlConnection(String.Format("Server={0};Database={1};Uid={2};Pwd={3}", connections.ElementAt(i).Serveur, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password)); Conns.Add(Conn); break; } case ConnectionType.PostGreSQL: { Conn = new NpgsqlConnection(String.Format("Server={0};Database={1};UID={2};Password={3};Port={4};", connections.ElementAt(i).Serveur, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password, connections.ElementAt(i).Port)); Conns.Add(Conn); break; } //case ConnectionType.Oracle: //{ // //Conn = new OracleConnection(String.Format("Server={0}:{1}/{2};Uid={3};Password={4};", // // connection.Serveur, connection.Port, connection.Database, connection.User, connection.Password)); // Conn = new OracleConnection(String.Format("SERVER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = {1}))(CONNECT_DATA = (SERVICE_NAME = {2})));", // connections.ElementAt(i).Serveur, connections.ElementAt(i).Port, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password)); // Conns.Add(Conn); // break; //} case ConnectionType.Acces: { Conn = new OleDbConnection(String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}{1}", connections.ElementAt(i).Path, connections.ElementAt(i).Database)); Conns.Add(Conn); break; } } i++; } return(Conns); }
public void StartGame() { listener.Stop(); Console.WriteLine("[{0}] Game started", DateTime.Now.ToString(ServerSettings.TimestampFormat)); // Drop any unvalidated clients foreach (var c in PreConns.ToArray()) { DropClient(c); } // Drop any players who are not ready foreach (var c in Conns.Where(c => GetClient(c).IsInvalid).ToArray()) { SendOrderTo(c, "ServerError", "You have been kicked from the server!"); DropClient(c); } // HACK: Turn down the latency if there is only one real player if (LobbyInfo.NonBotClients.Count() == 1) { LobbyInfo.GlobalSettings.OrderLatency = 1; } SyncLobbyInfo(); State = ServerState.GameStarted; foreach (var c in Conns) { foreach (var d in Conns) { DispatchOrdersToClient(c, d.PlayerIndex, 0x7FFFFFFF, new byte[] { 0xBF }); } } DispatchOrders(null, 0, new ServerOrderDefault("StartGame", "").Serialize()); foreach (var t in serverTraits.WithInterface <IStartGame <ClientDefault, ClientPingDefault> >()) { t.GameStarted(this); } }
private void DailyRun() { Conns con = new Conns(); ConnsAS400 con400 = new ConnsAS400(); fs = new FileStream(ConfigurationSettings.AppSettings["logFilePath"].ToString(), FileMode.OpenOrCreate, FileAccess.Write);; StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine(Environment.NewLine + "Branch Update service Started: start date & time :" + DateTime.Now.ToString()); m_streamWriter.Flush(); while (IsStarted) { if (DateTime.Now.Hour.Equals(int.Parse(ConfigurationSettings.AppSettings["StartHour"].ToString()))) { try { con.OpenStoredProcedure(); con.cmd.CommandText = "INTERNET.GET_EACH_COUNT_OF_EPF"; con.cmd.ExecuteNonQuery(); m_streamWriter.WriteLine(Environment.NewLine + "Successfully run the procedure" + DateTime.Now.ToString()); m_streamWriter.Flush(); con.OpenConection(); con400.OpenConection(); m_streamWriter.WriteLine(Environment.NewLine + "Database connection open" + DateTime.Now.ToString()); m_streamWriter.Flush(); string getuser = "******"; OracleDataReader reader1 = con.DataReader(getuser); while (reader1.Read()) { string id = reader1["USER_ID"].ToString(); string branch = "SELECT BRNACH FROM INTRANET.INTUSR WHERE USERID = '" + id + "' "; OdbcDataReader reader2 = con400.DataReader(branch); while (reader2.Read()) { string branch1 = reader2["BRNACH"].ToString(); string update = "UPDATE SLIC_MTRACK.TRACK_PERFORM SET BRANCH = '" + branch1 + "' WHERE USER_ID = '" + id + "'"; con.ExecuteQueries(update); } } con.Commit(); con400.Commit(); m_streamWriter.WriteLine(Environment.NewLine + "SuccessFully Updated the branch.." + DateTime.Now.ToString()); m_streamWriter.Flush(); Thread.Sleep(1000 * 60 * 60 * 10); } catch (Exception ex) { m_streamWriter.WriteLine("Error: " + ex.Message); m_streamWriter.WriteLine(""); m_streamWriter.Flush(); IsStarted = false; } } } m_streamWriter.WriteLine(Environment.NewLine + "Branch Update service Finished: finish date & time :" + DateTime.Now.ToString()); m_streamWriter.Flush(); }
public ServerDefault(IPEndPoint endpoint, ServerSettings serverSettings, ModData modData, bool dedicated) { listener = new TcpListener(endpoint); listener.Start(); var localEndpoint = (IPEndPoint)listener.LocalEndpoint; Ip = localEndpoint.Address; Port = localEndpoint.Port; Dedicated = dedicated; ServerSettings = serverSettings; ServerSettings.Name = Engine.Settings.SanitizedServerName(ServerSettings.Name); ModData = modData; randomSeed = (int)DateTime.Now.ToBinary(); // UPnP is only supported for servers created by the game client. //if (!dedicated && ServerSettings.AllowPortForward) // UPnP.ForwardPort(ServerSettings.ListenPort, ServerSettings.ExternalPort).Wait(); foreach (var trait in modData.Manifest.ServerTraits) { serverTraits.Add(modData.ObjectCreator.CreateObject <ServerTrait>(trait)); } LobbyInfo = new Session <ClientDefault, ClientPingDefault> { GlobalSettings = { RandomSeed = randomSeed, Map = serverSettings.Map, ServerName = serverSettings.Name, EnableSingleplayer = serverSettings.EnableSingleplayer || !dedicated, GameUid = Guid.NewGuid().ToString() } }; new Thread(_ => { foreach (var t in serverTraits.WithInterface <INotifyServerStart <ClientDefault, ClientPingDefault> >()) { t.ServerStarted(this); } //Log.Write("server", "Initial mod: {0}", ModData.Manifest.Id); //Log.Write("server", "Initial map: {0}", LobbyInfo.GlobalSettings.Map); var timeout = serverTraits.WithInterface <ITick <ClientDefault, ClientPingDefault> >().Min(t => t.TickTimeout); for (;;) { var checkRead = new List <Socket>(); if (State == ServerState.WaitingPlayers) { checkRead.Add(listener.Server); } checkRead.AddRange(Conns.Select(c => c.Socket)); checkRead.AddRange(PreConns.Select(c => c.Socket)); if (checkRead.Count > 0) { Socket.Select(checkRead, null, null, timeout); } if (State == ServerState.ShuttingDown) { EndGame(); break; } foreach (var s in checkRead) { if (s == listener.Server) { AcceptConnection(); continue; } var preConn = PreConns.SingleOrDefault(c => c.Socket == s); if (preConn != null) { preConn.ReadData(this); continue; } var conn = Conns.SingleOrDefault(c => c.Socket == s); if (conn != null) { conn.ReadData(this); } } foreach (var t in serverTraits.WithInterface <ITick <ClientDefault, ClientPingDefault> >()) { t.Tick(this); } if (State == ServerState.ShuttingDown) { EndGame(); //if (!dedicated && ServerSettings.AllowPortForward) // UPnP.RemovePortForward().Wait(); break; } } foreach (var t in serverTraits.WithInterface <INotifyServerShutdown <ClientDefault, ClientPingDefault> >()) { t.ServerShutdown(this); } PreConns.Clear(); Conns.Clear(); try { listener.Stop(); } catch { } }) { IsBackground = true }.Start(); }
public void DropClient(IServerConnectoin <ClientDefault, ClientPingDefault> toDrop) { if (!PreConns.Remove(toDrop)) { Conns.Remove(toDrop); var dropClient = LobbyInfo.Clients.FirstOrDefault(c1 => c1.Index == toDrop.PlayerIndex); if (dropClient == null) { return; } var suffix = ""; if (State == ServerState.GameStarted) { suffix = dropClient.IsObserver ? " (Spectator)" : ""; } SendMessage("{0}{1} has disconnected.".F(dropClient.Name, suffix)); // Send disconnected order, even if still in the lobby DispatchOrdersToClients(toDrop, 0, new ServerOrderDefault("Disconnected", "").Serialize()); LobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex); LobbyInfo.ClientPings.RemoveAll(p => p.Index == toDrop.PlayerIndex); // Client was the server admin // TODO: Reassign admin for game in progress via an order if (Dedicated && dropClient.IsAdmin && State == ServerState.WaitingPlayers) { // Remove any bots controlled by the admin LobbyInfo.Clients.RemoveAll(c => c.Bot != null && c.BotControllerClientIndex == toDrop.PlayerIndex); var nextAdmin = LobbyInfo.Clients.Where(c1 => c1.Bot == null) .MinByOrDefault(c => c.Index); if (nextAdmin != null) { nextAdmin.IsAdmin = true; SendMessage("{0} is now the admin.".F(nextAdmin.Name)); } } DispatchOrders(toDrop, toDrop.MostRecentFrame, new byte[] { 0xbf }); // All clients have left: clean up if (!Conns.Any()) { foreach (var t in serverTraits.WithInterface <INotifyServerEmpty <ClientDefault, ClientPingDefault> >()) { t.ServerEmpty(this); } } if (Conns.Any() || Dedicated) { SyncLobbyClients(); } if (!Dedicated && dropClient.IsAdmin) { Shutdown(); } } try { toDrop.Socket.Disconnect(false); } catch { } }