private static void Main(string[] args) { ServiceHost svcHost = null; try { NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, true) { ReliableSession = { InactivityTimeout = new TimeSpan(0, 2, 0) }, SendTimeout = new TimeSpan(0, 2, 0), ReceiveTimeout = new TimeSpan(0, 2, 0), OpenTimeout = new TimeSpan(0, 1, 0), CloseTimeout = new TimeSpan(0, 1, 0), MaxReceivedMessageSize = 2147483647, ReaderQuotas = { MaxArrayLength = 2147483647, MaxBytesPerRead = 2147483647, MaxStringContentLength = 2147483647, MaxDepth = 2147483647, }, }; svcHost = new ServiceHost(typeof(BesiegedServer.BesiegedServer), new Uri("net.tcp://172.21.72.122:31337/BesiegedServer/")); svcHost.AddServiceEndpoint(typeof(Framework.ServiceContracts.IBesiegedServer), binding, "BesiegedMessage"); svcHost.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = false }); svcHost.Open(); // Configure a client callback for the server itself to force start the process m_ServerClient = new ServerClient(); EndpointAddress endpointAddress = new EndpointAddress("net.tcp://172.21.72.122:31337/BesiegedServer/BesiegedMessage"); DuplexChannelFactory<IBesiegedServer> duplexChannelFactory = new DuplexChannelFactory<IBesiegedServer>(m_ServerClient, new NetTcpBinding(SecurityMode.None, true), endpointAddress); m_BesiegedServer = duplexChannelFactory.CreateChannel(); Task.Factory.StartNew(() => { var startServer = new GenericServerMessage() { MessageEnum = ServerMessage.ServerMessageEnum.StartServer }; m_BesiegedServer.SendMessage(startServer.ToXml()); }); var subject = new Subject<BesiegedMessage>(); var messagePublisher = m_ServerClient.MessageQueue .GetConsumingEnumerable() .ToObservable(TaskPoolScheduler.Default) .Subscribe(subject); // All generic server messages are handled here var genericServerMessageSubscriber = subject .Where(message => message is GenericServerMessage) .Subscribe(message => { var genericMessage = message as GenericServerMessage; switch (genericMessage.MessageEnum) { case ServerMessage.ServerMessageEnum.StartServer: break; case ServerMessage.ServerMessageEnum.ServerStarted: ConsoleLogger.Push("Server has started."); break; default: ConsoleLogger.Push("Unhandled GenericServerMessage was received: " + genericMessage.MessageEnum.ToString()); break; } }); // All server messages are handled here var m_ServerMessageSubscriber = subject .Where(message => message is ServerMessage && !(message is GenericServerMessage)) .Subscribe(message => { // do stuff with server bound messages here }); } catch (Exception ex) { ConsoleLogger.Push(ex.Message); } finally { while (true) { var serverMessage = Console.ReadLine().Trim().ToString().ToLower(); if (serverMessage == "exit") break; else if (serverMessage == "?" || serverMessage == "help" || serverMessage == "\\help") { ConsoleLogger.Push("Besieged Server Commands\nexit: Stops the server."); } else if (serverMessage == "list") { //var clients = GameObject.GetClients(); //if (clients.Count <= 0) //{ // Console.Write("> "); // continue; //} //Console.WriteLine("Connected Clients"); //foreach (BesiegedLogic.Objects.Client client in clients) //{ // Console.WriteLine(client.Alias); //} //Convert this to use Jesse's Code } else { ConsoleLogger.Push("Command " + serverMessage + " is not recognized"); } } if (svcHost != null) { svcHost.Close(); } } }
public void SendMessage(string serializedMessage) { try { BesiegedMessage message = serializedMessage.FromXml<BesiegedMessage>(); if (message is GenericServerMessage && (message as GenericServerMessage).MessageEnum == ServerMessage.ServerMessageEnum.StartServer) { m_ServerCallback = OperationContext.Current.GetCallbackChannel<IClient>(); GenericServerMessage started = new GenericServerMessage() { MessageEnum = ServerMessage.ServerMessageEnum.ServerStarted }; m_ServerCallback.SendMessage(started.ToXml()); m_IsServerInitialized.Value = true; //StartProcessingMessages(); ProcessMessages(); } else if (message is ConnectMessage) { IClient callback = OperationContext.Current.GetCallbackChannel<IClient>(); string clientId = Guid.NewGuid().ToString(); ConnectedClient connectedClient = new ConnectedClient((message as ConnectMessage).Name, clientId, callback); AggregateMessage aggregate = new AggregateMessage(); GenericClientMessage success = new GenericClientMessage() { ClientId = clientId, MessageEnum = ClientMessage.ClientMessageEnum.ConnectSuccessful }; aggregate.MessageList.Add(success); if (m_Games.Count > 0) // notify the client of any pre-existing game instances that they might be able to join { foreach (KeyValuePair<string, BesiegedGameInstance> game in m_Games) { if (!game.Value.IsGameInstanceFull) { string capacity = string.Format("{0}/{1} players", game.Value.Players.Count, game.Value.MaxPlayers); GameInfoMessage gameInfo = new GameInfoMessage(game.Value.GameId, game.Value.Name, capacity, false, game.Value.Password != string.Empty ? true : false); aggregate.MessageList.Add(gameInfo); } } } callback.SendMessage(aggregate.ToXml()); ConsoleLogger.Push(string.Format("{0} has joined the server", connectedClient.Name)); m_ConnectedClients.GetOrAdd(clientId, connectedClient); // Add an entry to the global client hook } else { m_MessageQueue.Add(message); } } catch (Exception ex) { ErrorLogger.Push(ex); } }