/// <summary> /// Helper method for Gem.Network.Tests.MessageEncodingTests.SuccessfulDecodeTest() /// Set's up a server and sends a message for decoding /// This is done in a seperate project because a socket exception is thrown /// because client / server use the same port /// </summary> public static void MessageDecodingTestHelper() { var server = new NetworkServer(); //server.Connect(new ServerConfig { Name = "local", Port = 14241 }); NetIncomingMessage inc; while (true) { if ((inc = server.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.ConnectionApproval: Console.WriteLine("Incoming LOGIN"); inc.SenderConnection.Approve(); break; case NetIncomingMessageType.Data: var outgoingmessage = server.CreateMessage(); var obj = new ClassToSerialize(); obj.StringProperty = "SomeString"; MessageSerializer.Encode(obj, ref outgoingmessage); server.SendToAll(outgoingmessage); Console.WriteLine("Sended a package with some string"); break; } } } }
private void ProcessMessages() { NetIncomingMessage msg; while ((msg = NetworkServer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.ConnectionApproval: Login.HandleConnection(msg); break; case NetIncomingMessageType.DiscoveryRequest: var discovery = NetworkServer.CreateMessage(); Connections.WriteServerInfo(discovery); NetworkServer.SendDiscoveryResponse(discovery, msg.SenderEndPoint); break; case NetIncomingMessageType.StatusChanged: Connections.UpdateConnectionStatus(msg.SenderConnection); break; case NetIncomingMessageType.Data: if (msg.SequenceChannel == Channels.GameplayData) { MessageProcessor.ProcessMessages(msg); } else if (msg.SequenceChannel == Channels.Login) { Login.ProcessMessage(msg); } break; //Error handling case NetIncomingMessageType.ErrorMessage: Logger.Error(msg.ReadString()); break; case NetIncomingMessageType.WarningMessage: Logger.Warning(msg.ReadString()); break; case NetIncomingMessageType.DebugMessage: if (GlobalSettings.Debug) { Logger.Debug(msg.ReadString()); } break; case NetIncomingMessageType.VerboseDebugMessage: if (GlobalSettings.Debug) { Logger.Trace(msg.ReadString()); } break; } NetworkServer.Recycle(msg); } }
public static void DynamicMessageDecodingTestHelper() { #region Dynamic Type var propertyList = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "Name", PropertyType = typeof(string) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); dynamic myObject = Activator.CreateInstance(myNewType); myObject.Name = "DynamicType"; #endregion NetworkServer server; server = new NetworkServer(); //server.Connect(new ServerConfig { Name = "local", Port = 14241 }); NetIncomingMessage inc; while (true) { if ((inc = server.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.ConnectionApproval: Console.WriteLine("Incoming LOGIN"); inc.SenderConnection.Approve(); break; case NetIncomingMessageType.Data: var outgoingmessage = server.CreateMessage(); MessageSerializer.Encode(myObject, ref outgoingmessage); server.SendToAll(outgoingmessage); Console.WriteLine("Sended a package with some string"); break; } } } }
public void Update(GameTime gameTime) { GameInstance.Tick(gameTime); TickGameStartCountdown(gameTime); Timers.Update(gameTime); ProcessMessages(); //Send all the wideband messages (if someone is listening) if (Connections.ActiveConnections.Count > 0) { if (MessageProcessor.MessageQueue.Count > 0) { var msg = NetworkServer.CreateMessage(); MessageProcessor.WriteMessages(msg); NetworkServer.SendMessage(msg, Connections.ActiveConnections, Lidgren.Network.NetDeliveryMethod.ReliableOrdered, Channels.GameplayData); } //As well as narrowband ones foreach (var plr in Players) { if (MessageProcessor.HasPrivateMessages(plr) && plr.Connection != null && plr.Connection.Status == Lidgren.Network.NetConnectionStatus.Connected) { var msg = NetworkServer.CreateMessage(); MessageProcessor.WritePrivateMessages(plr, msg); plr.Connection.SendMessage(msg, Lidgren.Network.NetDeliveryMethod.ReliableOrdered, Channels.GameplayData); } } } //Just clear the queue since no one is listening MessageProcessor.ClearQueue(); MessageProcessor.ClearPrivateQueues(); FlushMessages(); }