public static bool StartServer(int port, string name) { if (IsServer) { Main.Log("Server already started, shut it down before you open a new one."); return(false); } if (IsClient && (IsConnected || IsConnecting)) { Main.Log("Cannot host server while client is active."); return(false); } var config = new NetPeerConfiguration(APP_ID); config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest); config.EnableMessageType(NetIncomingMessageType.ConnectionApproval); config.Port = port; Server = new CServer(name, config); Server.Start(); Main.Log("Started hosting server on port " + port + " called " + name); Main.SetStatus("Hosting Server (" + name.Trim() + ", " + port + ")"); Audio.InitAll(); return(true); }
public static void ProcessData(DataType type, NetIncomingMessage msg, CServer s) { switch (type) { case DataType.AUDIO: bool muted = s.IsMuted(msg.SenderConnection); // Ignore if muted, just don't send. if (muted) { break; } // Bounce to all other clients. int c = msg.ReadInt32(); msg.ReadBytes(c, out s.buffer); // Find speaker ID. int id = Audio.GetOrCreateID(msg.SenderConnection); var outMsg = s.CreateMessage(); outMsg.Write((byte)DataType.AUDIO); outMsg.Write(id); outMsg.Write(c); outMsg.Write(s.buffer); s.SendToAll(outMsg, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0); // Play on local device. Audio.QueuePlay(id, s.buffer, c); break; } }
public static void StopServer() { if (!IsServer) { return; } Server.Shutdown("The host has shutdown the server."); Server = null; Main.Log("The locally hosted server has been shut down."); Audio.ClearAll(); Main.UponServerStop(); }