/// <summary> /// The entry point for a server /// </summary> /// <param name="holder"></param> /// <param name="proposal"></param> public void CreateSelfTestServer(NetworkServerData holder, SettingsProposal proposal) { RunServer(holder); ReceiveConfiguration(holder); proposal.Push(holder.Settings, true); InstantiateWorld(holder); }
/// <summary> /// This method receives the configuration and the world's initial state from the client. /// </summary> /// <param name="data"></param> public void ReceiveConfiguration(NetworkServerData data) { var configProposal = data.ClientOnServerSide.Read<ConfigurationProposal>(); data.LoadingData = configProposal.LoadingData; var competitions = GetCompetitions(data.LoadingData); data.Settings = competitions.Logic.CreateDefaultSettings(); if (configProposal.SettingsProposal != null) configProposal.SettingsProposal.Push(data.Settings, true); var worldSettingsType = competitions.Logic.WorldStateType; data.WorldState = (IWorldState)data.ClientOnServerSide.Read(worldSettingsType); }
/// <summary> /// Runs the test as an application with GUI /// </summary> /// <param name="data"></param> /// <param name="asserter"></param> /// <returns></returns> public IWorld CreateSelfTestInCommandLineContext(CommandLineData data, IAsserter asserter) { var holder = new NetworkServerData(); holder.Port = DefaultPort; holder.LoadingData = new LoadingData { AssemblyName = data.Unnamed[0], Level = data.Unnamed[1] }; var test = GetTest(holder.LoadingData, data.Unnamed[3]); new Action<ICvarcTest, IAsserter, NetworkServerData>(SelfTestClientThread).BeginInvoke(test, asserter, holder, null, null); var proposal = SettingsProposal.FromCommandLineData(data); CreateSelfTestServer(holder, proposal); return holder.World; }
/// <summary> /// This method runs TCP/IP listener, waits for the connected client and set the closing actions for client and server after the competitions are over. /// </summary> /// <param name="data"></param> public void RunServer(NetworkServerData data) { var server = new System.Net.Sockets.TcpListener(data.Port); server.Start(); data.ServerLoaded = true; var client = server.AcceptTcpClient(); data.ClientOnServerSide = new CvarcClient(client); data.StopServer = () => { client.Close(); server.Stop(); }; }
//Запускать из Intro по типа таймеру public static void CheckNetworkClient() { if (loadedNetworkServerData != null) { Func<IWorld> worldInitializer = () => { loader.InstantiateWorld(loadedNetworkServerData); var world = loadedNetworkServerData.World; loadedNetworkServerData = null; return world; }; WorldPrepared(worldInitializer); } }
/// <summary> /// Creates a controller for the network client and instantiate the world with this controller. This action is the last before the start. /// </summary> /// <param name="data"></param> public void InstantiateWorld(NetworkServerData data) { var factory = new SoloNetworkControllerFactory(data.ClientOnServerSide); var configuration = new Configuration { LoadingData = data.LoadingData, Settings = data.Settings }; data.World = CreateWorld(configuration, factory, data.WorldState); data.World.Exit += () => { if (data != null) data.Close(); }; }
public static void RunServerInTheSameThread(Action<int> Control) { var nsData = new NetworkServerData(); nsData.Port = Loader.DefaultPort; new Action(() => { nsData.WaitForServer(); Control(nsData.Port); }).BeginInvoke(null, null); var loader = GetLoader(); loader.CreateSoloNetworkWithData(nsData); RunWorld(nsData.World); }
/// <summary> /// Creates world for a solo network mode from a command line arguments. /// </summary> /// <param name="data"></param> /// <returns></returns> public IWorld CreateSoloNetwork(CommandLineData data) { int port; if (data.Unnamed.Count > 1) { try { port = int.Parse(data.Unnamed[1]); } catch { throw new Exception("Port number '" + data.Unnamed[1] + "' is incorrect: integer expected"); } } else port = DefaultPort; var nsdata = new NetworkServerData(); nsdata.Port = port; CreateSoloNetworkWithData(nsdata); return nsdata.World; }
/// <summary> /// Completely initializes the world. The NetworkServerData should only contain the port. /// </summary> /// <param name="nsdata"></param> public void CreateSoloNetworkWithData(NetworkServerData nsdata) { RunServer(nsdata); ReceiveConfiguration(nsdata); InstantiateWorld(nsdata); }
//Подготавливает диспетчер к приему нового клиента. static void RenewWaitingNetworkServer() { WaitingNetworkServer = new NetworkServerData() { Port = 14000 }; WaitingNetworkServer.ServerLoaded = true; }
static void ClientConnected(CvarcClient client) { //в отдельном трэде делать! RunThread( new Action(() => { WaitingNetworkServer.ClientOnServerSide = client; loader.ReceiveConfiguration(WaitingNetworkServer); loadedNetworkServerData = WaitingNetworkServer; // сигнал того, что мир готов к созданию. RenewWaitingNetworkServer(); // а это мы делаем, чтобы следующее подключение удалось. // создавать его прямо здесь нельзя, потому что другой трэд }), "Connection");//.BeginInvoke(null, null); }
/// <summary> /// Runs the test as a Unit test. /// </summary> /// <param name="assemblyName"></param> /// <param name="level"></param> /// <param name="testName"></param> /// <param name="asserter"></param> public void RunSelfTestInVSContext(string assemblyName, string level, string testName, IAsserter asserter) { var holder = new NetworkServerData(); holder.Port = DefaultPort; holder.LoadingData = new LoadingData { AssemblyName = assemblyName, Level = level }; var test = GetTest(holder.LoadingData, testName); var thread = new Thread(() => { var proposal = new SettingsProposal { SpeedUp = true }; CreateSelfTestServer(holder, proposal); holder.World.RunActively(1); holder.Close(); }) { IsBackground = true }; thread.Start(); try { SelfTestClientThread(test, asserter, holder); } catch (Exception e) { throw e; } finally { holder.Close(); } }
/// <summary> /// The entry point for a client that performs a test /// </summary> /// <param name="test"></param> /// <param name="asserter"></param> /// <param name="holder"></param> void SelfTestClientThread(ICvarcTest test, IAsserter asserter, NetworkServerData holder) { holder.WaitForServer(); test.Run(holder, asserter); }