public static void Send(string information, NetConnection client) { NetBuffer prepareMessage = server.CreateBuffer(); prepareMessage.Write(information); server.SendMessage(prepareMessage, client, NetChannel.ReliableInOrder1); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); s_mainForm = new Form1(); NetConfiguration config = new NetConfiguration("largepacket"); config.Port = 14242; config.MaxConnections = 16; s_server = new NetServer(config); s_server.SimulatedLoss = 0.03f; // 3 % s_server.SimulatedMinimumLatency = 0.1f; // 100 ms s_server.SimulatedLatencyVariance = 0.05f; // 100-150 ms actually //m_server.SetMessageTypeEnabled(NetMessageType.VerboseDebugMessage, true); s_server.Start(); s_readBuffer = s_server.CreateBuffer(); Application.Idle += new EventHandler(OnAppIdle); Application.Run(s_mainForm); s_server.Shutdown("Application exiting"); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); s_mainForm = new Form1(); s_nextPixelToSend = new Dictionary <NetConnection, uint>(); NetConfiguration config = new NetConfiguration("imageservice"); config.Port = 14242; config.MaxConnections = 64; config.ThrottleBytesPerSecond = 25000; s_server = new NetServer(config); s_server.SimulatedMinimumLatency = 0.05f; s_server.SimulatedLoss = m_loss; s_fpsStart = NetTime.Now; s_readBuffer = s_server.CreateBuffer(); Application.Idle += new EventHandler(OnAppIdle); Application.Run(s_mainForm); s_server.Shutdown("Application exit"); }
public void start(string name, int connections, int port) { //Create configuration. config = new NetConfiguration(name); config.MaxConnections = connections; config.Port = port; //Create the server! server = new NetServer(config); server.SetMessageTypeEnabled(NetMessageType.ConnectionApproval, true); server.Start(); //Create a buffer. readBuffer = server.CreateBuffer(); Debug.PrintEvent("Server is running! press escape to exit server"); consoleIdle(true); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); s_mainForm = new Form1(); NetConfiguration config = new NetConfiguration("OoBSample"); config.MaxConnections = 0; // we accept only OoB data config.Port = 14242; s_server = new NetServer(config); s_server.SetMessageTypeEnabled(NetMessageType.OutOfBandData, true); s_server.Start(); s_readBuffer = s_server.CreateBuffer(); Application.Idle += new EventHandler(OnAppIdle); Application.Run(s_mainForm); s_server.Shutdown("Bye"); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); s_mainForm = new Form1(); NetConfiguration config = new NetConfiguration("stress"); config.Port = 14242; config.MaxConnections = 32; s_server = new NetServer(config); s_server.SimulatedMinimumLatency = 0.1f; s_server.SimulatedLatencyVariance = 0.05f; // s_server.SimulatedLoss = 0.1f; // s_server.SimulatedDuplicates = 0.05f; s_readBuffer = s_server.CreateBuffer(); Application.Idle += new EventHandler(OnAppIdle); Application.Run(s_mainForm); s_server.Shutdown("Application exiting"); }
static void Main(string[] args) { // create a configuration for the server NetConfiguration config = new NetConfiguration("chatApp"); config.MaxConnections = 128; config.Port = 14242; // create server and start listening for connections NetServer server = new NetServer(config); server.SetMessageTypeEnabled(NetMessageType.ConnectionApproval, true); server.Start(); // create a buffer to read data into NetBuffer buffer = server.CreateBuffer(); // keep running until the user presses a key Console.WriteLine("Press ESC to quit server"); bool keepRunning = true; while (keepRunning) { NetMessageType type; NetConnection sender; // check if any messages has been received while (server.ReadMessage(buffer, out type, out sender)) { switch (type) { case NetMessageType.DebugMessage: Console.WriteLine(buffer.ReadString()); break; case NetMessageType.ConnectionApproval: Console.WriteLine("Approval; hail is " + buffer.ReadString()); sender.Approve(); break; case NetMessageType.StatusChanged: string statusMessage = buffer.ReadString(); NetConnectionStatus newStatus = (NetConnectionStatus)buffer.ReadByte(); Console.WriteLine("New status for " + sender + ": " + newStatus + " (" + statusMessage + ")"); break; case NetMessageType.Data: // A client sent this data! string msg = buffer.ReadString(); // send to everyone, including sender NetBuffer sendBuffer = server.CreateBuffer(); sendBuffer.Write(sender.RemoteEndpoint.ToString() + " wrote: " + msg); // send using ReliableInOrder server.SendToAll(sendBuffer, NetChannel.ReliableInOrder1); break; } } // User pressed ESC? while (Console.KeyAvailable) { ConsoleKeyInfo info = Console.ReadKey(); if (info.Key == ConsoleKey.Escape) { keepRunning = false; } } Thread.Sleep(1); } server.Shutdown("Application exiting"); }
static void OnAppIdle(object sender, EventArgs e) { NetChannel useChannel = NetChannel.Unreliable; switch (s_mainForm.comboBox1.SelectedIndex) { case 0: useChannel = NetChannel.Unreliable; break; case 1: useChannel = NetChannel.ReliableUnordered; break; case 2: useChannel = NetChannel.ReliableInOrder1; break; } while (NativeMethods.AppStillIdle) { NetConnection source; NetMessageType type; while (s_server.ReadMessage(s_readBuffer, out type, out source)) { HandleMessage(type, source, s_readBuffer); } // send segment to all connections double now = NetTime.Now; if (now > s_nextSend) { foreach (NetConnection conn in s_server.Connections) { if (conn.Status == NetConnectionStatus.Connected) { NetBuffer outBuf = null; if (conn.Tag == null) { continue; } switch ((ImageClientStatus)conn.Tag) { case ImageClientStatus.JustConnected: if (outBuf == null) { outBuf = s_server.CreateBuffer(); } // send pixel size outBuf.WriteVariableUInt32(s_imageWidth); outBuf.WriteVariableUInt32(s_imageHeight); // send size using reliable and receipt, so we know when it's received and we can start pushing pixels conn.SendMessage(outBuf, NetChannel.ReliableUnordered, s_server.CreateBuffer()); conn.Tag = ImageClientStatus.WaitingForSizeReceipt; break; case ImageClientStatus.WaitingForSizeReceipt: // keep waiting break; case ImageClientStatus.Running: if (outBuf == null) { outBuf = s_server.CreateBuffer(); } uint nextPixel = s_nextPixelToSend[conn]; uint chunkSize = 128; uint pixelsToSend = (uint)(s_imageData.Length / 3) - nextPixel; if (pixelsToSend > 0) { if (pixelsToSend > chunkSize) { pixelsToSend = chunkSize; } outBuf.Reset(); outBuf.Write(nextPixel); outBuf.Write(s_imageData, (int)(nextPixel * 3), (int)(pixelsToSend * 3)); conn.SendMessage(outBuf, useChannel); s_nextPixelToSend[conn] = nextPixel + pixelsToSend; } else { // disconnect? if (conn.UnsentMessagesCount < 1) { conn.Disconnect("Fin", 5.0f); } } break; } } } s_nextSend = now + (1.0 / 150.0); // 150 segments per second max } s_ticks++; double fpsnow = NetTime.Now; double span = fpsnow - s_fpsStart; if (span >= 1.0) { double fps = (double)s_ticks / span; s_ticks = 0; s_fpsStart = fpsnow; s_mainForm.Text = "Server " + (int)fps + " fps; " + s_server.Connections.Count + " connections"; s_mainForm.label2.Text = s_server.GetStatisticsString(s_server.Connections.Count > 0 ? s_server.Connections[0] : null); } System.Threading.Thread.Sleep(1); } }