// Handle incoming messages // Data packets are enqueued for later processing public void MessageReceived(object obj) { NetPeer peer = obj as NetPeer; NetIncomingMessage msg = peer.ReadMessage(); switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: OnIncomingStatusChange(peer, msg); break; case NetIncomingMessageType.DiscoveryResponse: OnIncomingDiscoveryResponse(peer, msg); break; case NetIncomingMessageType.Data: OnIncomingData(peer, msg); break; case NetIncomingMessageType.DebugMessage: OnIncomingDebugMessage(peer, msg); break; default: Debug.Log($"Unhandled packet received: {msg.MessageType}, Data: {Encoding.UTF8.GetString(msg.ReadBytes(msg.LengthBytes))}"); break; } }
static void AppLoop(object sender, EventArgs e) { while (NativeMethods.AppStillIdle) { // read any incoming messages NetIncomingMessage im; while ((im = Peer.ReadMessage()) != null) { switch (im.MessageType) { case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: MainForm.richTextBox1.AppendText(im.ReadString() + Environment.NewLine); break; case NetIncomingMessageType.UnconnectedData: MainForm.richTextBox1.AppendText("Received from " + im.SenderEndPoint + ": " + im.ReadString() + Environment.NewLine); Peer.Recycle(im); break; } } System.Threading.Thread.Sleep(1); } }
public void MessagePump() { NetIncomingMessage nim; while ((nim = netpeer.ReadMessage()) != null) { switch (nim.MessageType) { case NetIncomingMessageType.StatusChanged: OnStatusChanged(nim); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.Error: OnDebugMessage(nim); break; default: OnUnknownMessage(nim); break; } netpeer.Recycle(nim); } }
protected void Update() { NetIncomingMessage msg; while ((msg = instance.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.Data: var index = msg.ReadUInt32(); methods[index].Invoke(msg); break; default: Console.WriteLine("Unhandled type: " + msg.MessageType); break; } instance.Recycle(msg); } }
public bool ReceiveOne() { NetIncomingMessage message = Peer.ReadMessage(); if (message != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: onDataMessage(message); break; case NetIncomingMessageType.StatusChanged: onStatusChanged(message); break; default: onInternalMessage(message); break; } Peer.Recycle(message); return(true); } return(false); }
protected bool ProcessMessages(out int messageCount) { messageCount = 0; if (peer == null) { return(false); } if (peer.Status == NetPeerStatus.NotRunning) { return(false); } NetIncomingMessage msg; while ((msg = peer.ReadMessage()) != null) { NetIncomingMessageType type = msg.MessageType; // This is not the fastest way to do this... if (baseHandlers.ContainsKey(type)) { messageCount++; baseHandlers[type].Invoke(msg); } peer.Recycle(msg); } return(true); }
public void ProcessMessage(object peerObj) { NetPeer peer = peerObj as NetPeer; NetIncomingMessage msg = peer.ReadMessage(); switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: OnIncomingStatusChange(peer, msg); break; case NetIncomingMessageType.DiscoveryRequest: OnIncomingDiscoveryRequest(peer, msg); break; case NetIncomingMessageType.ConnectionApproval: OnIncomingConnectionApproval(peer, msg); break; case NetIncomingMessageType.Data: OnIncomingData(peer, msg); break; case NetIncomingMessageType.WarningMessage: OnIncomingWarningMessage(peer, msg); break; case NetIncomingMessageType.DebugMessage: OnIncomingDebugMessage(peer, msg); break; } server.Recycle(msg); }
/// <summary> /// Reads every message in the queue and returns a list of data messages. /// Other message types just write a Console note. /// This should be called every update by the Game Screen /// The Game Screen should implement the actual handling of messages. /// </summary> /// <returns></returns> public List <NetIncomingMessage> CheckForMessages() { mIncomingMessages.Clear(); NetIncomingMessage incomingMessage; string output = ""; while ((incomingMessage = mPeer.ReadMessage()) != null) { switch (incomingMessage.MessageType) { case NetIncomingMessageType.DiscoveryRequest: mPeer.SendDiscoveryResponse(null, incomingMessage.SenderEndPoint); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: if (mRole == AgentRole.Server) { output += incomingMessage.ReadString() + "\n"; } break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)incomingMessage.ReadByte(); if (mRole == AgentRole.Server) { output += "Status Message: " + incomingMessage.ReadString() + " \n"; } if (status == NetConnectionStatus.Connected) { //PLAYER CONNECTED //Message to send the player their ID NetOutgoingMessage idMessage = mPeer.CreateMessage(); idMessage.Write((byte)MessageType.PlayerID); idMessage.Write(nextPlayerID++); mPeer.SendMessage(idMessage, incomingMessage.SenderConnection, NetDeliveryMethod.ReliableOrdered); IsPlayerConnected = true; } break; case NetIncomingMessageType.Data: mIncomingMessages.Add(incomingMessage); break; default: // unknown message type break; } } if (mRole == AgentRole.Server) { StreamWriter textOut = new StreamWriter(new FileStream("log.txt", FileMode.Append, FileAccess.Write)); textOut.Write(output); textOut.Close(); } return(mIncomingMessages); }
private void OnMessageReceived(object state) { NetPeer sender = (NetPeer)state; NetIncomingMessage newMes = sender.ReadMessage(); Parse(newMes); }
public void Update() { NetIncomingMessage msg; while ((msg = Net.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Log(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: OnStatusChanged(msg); break; case NetIncomingMessageType.Data: OnData(msg); break; default: Log($"Unknown Message Type: {msg.MessageType}"); break; } Net.Recycle(msg); } }
internal Task ProcessNetworkMessages(NetPeer peer) { return(Task.Run(async() => { NetIncomingMessage im; peer.MessageReceivedEvent.WaitOne(); while ((im = peer.ReadMessage()) != null) { if (im.SenderConnection != null) { if (connectionHandler.HasKey(im.SenderConnection)) { await connectionHandler[im.SenderConnection].Invoke(im.MessageType, im); } } foreach (var handler in globalHandler.GetAll) { await handler(im.MessageType, im); } if (messageHandlers.HasKey(im.MessageType)) { await messageHandlers[im.MessageType].Invoke(im); } peer.Recycle(im); } })); }
public Object Listener() // Listens for incoming messages on your peer port. { NetIncomingMessage message = peer.ReadMessage(); // Reads any incoming messages assuming there are any. if (message != null) // Makes sure there actually is a message. { switch (message.MessageType) // Checks for the message type that we grabbed above. { case NetIncomingMessageType.DiscoveryRequest: // Someone is looking for you because they requested this port number. NetOutgoingMessage res = peer.CreateMessage(); // Creates a response / packet to send back. res.Write("Here!"); // Replies with "Here!" in theory you could customize this for different networks to choose what to do peer.SendDiscoveryResponse(res, message.SenderEndPoint); break; case NetIncomingMessageType.DiscoveryResponse: // Your request was received and you got a response back. peer.Connect(message.SenderEndPoint); // Since you got a message back you connect to the end point. break; case NetIncomingMessageType.Data: // You received Data or an update from a peer return(DeserializeData(message.ReadBytes(message.LengthBytes))); } } return(null); }
void HandleWebConnections() { NetIncomingMessage message; while ((message = peer.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: string data = message.ReadString(); break; case NetIncomingMessageType.StatusChanged: switch (message.SenderConnection.Status) { } break; case NetIncomingMessageType.DebugMessage: Console.WriteLine(message.ReadString()); break; default: Console.WriteLine("unhandled message with type: " + message.MessageType); break; } } }
void Update() { NetIncomingMessage message; while ((message = peer.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: Debug.Log(message.MessageType); NetDataType type = (NetDataType)message.ReadByte(); OnDataType(type, message); break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)message.ReadByte(); Debug.Log("status " + status); break; case NetIncomingMessageType.DebugMessage: break; default: print("unhandled message with type: " + message.MessageType); break; } } }
public static async void Start() { var config = new NetPeerConfiguration("masterserver") { AutoFlushSendQueue = false, //Set it to false so lidgren doesn't wait until msg.size = MTU for sending Port = Port, SuppressUnreliableUnorderedAcks = true, PingInterval = 500, ConnectionTimeout = ServerMsTimeout }; config.EnableMessageType(NetIncomingMessageType.UnconnectedData); var peer = new NetPeer(config); peer.Start(); CheckMasterServerListed(); ConsoleLogger.Log(LogLevels.Normal, $"Master server {LmpVersioning.CurrentVersion} started! Поехали!"); RemoveExpiredServers(); while (RunServer) { NetIncomingMessage msg; while ((msg = peer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.VerboseDebugMessage: ConsoleLogger.Log(LogLevels.Debug, msg.ReadString()); break; case NetIncomingMessageType.WarningMessage: ConsoleLogger.Log(LogLevels.Warning, msg.ReadString()); break; case NetIncomingMessageType.ErrorMessage: ConsoleLogger.Log(LogLevels.Error, msg.ReadString()); break; case NetIncomingMessageType.UnconnectedData: if (FloodControl.AllowRequest(msg.SenderEndPoint.Address)) { var message = GetMessage(msg); if (message != null && !message.VersionMismatch) { HandleMessage(message, msg, peer); message.Recycle(); } peer.Recycle(msg); } break; } } await Task.Delay(ServerMsTick); } peer.Shutdown("Goodbye and thanks for all the fish!"); }
static void Main(string[] args) { NetPeerConfiguration config = new NetPeerConfiguration("unittests"); config.EnableUPnP = true; NetPeer peer = new NetPeer(config); peer.Start(); // needed for initialization Console.WriteLine("Unique identifier is " + NetUtility.ToHexString(peer.UniqueIdentifier)); ReadWriteTests.Run(peer); NetQueueTests.Run(); MiscTests.Run(peer); BitVectorTests.Run(); EncryptionTests.Run(peer); var om = peer.CreateMessage(); peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, 14242)); try { peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, 14242)); } catch (NetException nex) { if (nex.Message != "This message has already been sent! Use NetPeer.SendMessage() to send to multiple recipients efficiently") { throw nex; } } peer.Shutdown("bye"); // read all message NetIncomingMessage inc; while ((inc = peer.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine("Peer message: " + inc.ReadString()); break; case NetIncomingMessageType.Error: throw new Exception("Received error message!"); } } Console.WriteLine("Done"); Console.ReadKey(); }
static void UdpMain(GameList gameList) { var config = new NetPeerConfiguration("SpagAachen.Ballz"); config.Port = 43117; config.EnableMessageType(NetIncomingMessageType.UnconnectedData); var peer = new NetPeer(config); peer.Start(); while (true) { Thread.Sleep(10); NetIncomingMessage msg; while ((msg = peer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.UnconnectedData: gameList.RequestAddGame(msg); break; } peer.Recycle(msg); } } }
void ReadInData() { NetworkMessage client_input = new NetworkMessage(); NetIncomingMessage im; while ((im = server.ReadMessage()) != null) { switch (im.MessageType) { case NetIncomingMessageType.Data: byte[] msg = im.ReadBytes(im.LengthBytes); client_input = NetworkMessage.Deserialize(msg); OutputQueue.AddToQueue(client_input.GetData()); Debug.WriteLine(msg); break; case NetIncomingMessageType.DiscoveryRequest: // Create a response NetOutgoingMessage om = server.CreateMessage(); om.Write("Connecting to DOG server"); server.SendDiscoveryResponse(om, im.SenderEndPoint); break; case NetIncomingMessageType.UnconnectedData: Debug.WriteLine("Received from " + im.SenderEndPoint + ": " + im.ReadString() + Environment.NewLine); break; } server.Recycle(im); } }
/// <summary> /// Called when [update frame] occurs. /// </summary> public void OnUpdateFrame() { NetIncomingMessage msg; if (_netPeer != null) { while ((msg = _netPeer.ReadMessage()) != null) { IncomingMsg.Add(ReadMessage(msg)); _netPeer.Recycle(msg); } } if (_netClient != null) { while ((msg = _netClient.ReadMessage()) != null) { IncomingMsg.Add(ReadMessage(msg)); _netClient.Recycle(msg); } } if (_netServer != null) { while ((msg = _netServer.ReadMessage()) != null) { IncomingMsg.Add(ReadMessage(msg)); _netServer.Recycle(msg); } } }
public void HandleWebConnections() { NetIncomingMessage message; while ((message = peer.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: HandleData(message); break; case NetIncomingMessageType.StatusChanged: switch (message.SenderConnection.Status) { case NetConnectionStatus.Connected: Debug.WriteLine("Connected!"); break; case NetConnectionStatus.Disconnected: Debug.WriteLine("Disconnected..."); break; } break; case NetIncomingMessageType.ConnectionApproval: message.SenderConnection.Approve(); break; case NetIncomingMessageType.DiscoveryResponse: peer.Connect(message.SenderEndPoint); connecting = false; break; case NetIncomingMessageType.DiscoveryRequest: var msg = peer.CreateMessage(); msg.Write("hi"); peer.SendDiscoveryResponse(msg, message.SenderEndPoint); peer.Connect(message.SenderEndPoint); isServer = true; break; case NetIncomingMessageType.DebugMessage: Debug.WriteLine(message.ReadString()); break; case NetIncomingMessageType.WarningMessage: Debug.WriteLine(message.ReadString()); break; default: Debug.WriteLine("unhandled message with type: " + message.MessageType); break; } peer.Recycle(message); } }
void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } NetIncomingMessage message; while ((message = peer.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)message.ReadByte(); Debug.Log("status " + status); switch (status) { case NetConnectionStatus.Connected: Vector3 spawnPosition = Vector3.zero; Quaternion spawnRotation = Quaternion.identity; MyNetwork.SpawnPlayer(spawnPosition, spawnRotation); NetOutgoingMessage newMessage = peer.CreateMessage(); newMessage.Write((byte)NetDataType.SPAWN_PLAYER); newMessage.Write(spawnPosition); newMessage.Write(spawnRotation); NetSendResult result = peer.SendMessage(newMessage, message.SenderConnection, NetDeliveryMethod.ReliableUnordered); Debug.Log("sent: " + result); break; } break; case NetIncomingMessageType.WarningMessage: Debug.LogWarning(message.ReadString()); break; case NetIncomingMessageType.DebugMessage: Debug.Log(message.ReadString()); break; case NetIncomingMessageType.ConnectionApproval: { // TODO: Store connections message.SenderConnection.Approve(); break; } default: print("unhandled message with type: " + message.MessageType); break; } } }
private static void Main(string[] args) { map.generate(); Console.WriteLine(("Enter Server name:")); var name = Console.ReadLine(); var config = new NetPeerConfiguration("PythonRouge") { Port = 32078 }; config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest); Server = new NetServer(config); Server.Start(); Console.WriteLine("Ready"); NetIncomingMessage msg; while (true) { while ((msg = Server.ReadMessage()) != null) { Console.WriteLine("Got Message"); switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); string reason = msg.ReadString(); Console.WriteLine(status + " " + reason); break; case NetIncomingMessageType.ConnectionApproval: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.Data: msgHandler(msg); break; case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.DiscoveryRequest: // Create a response and write some example data to it NetOutgoingMessage response = Server.CreateMessage(); Console.WriteLine("Client request"); response.Write(name); // Send the response to the sender of the request Server.SendDiscoveryResponse(response, msg.SenderEndPoint); break; default: Console.WriteLine("Unhandled type: " + msg.MessageType); break; } Server.Recycle(msg); } } }
protected void ReadMessages() { if (peer != null) { NetIncomingMessage msg; while ((msg = peer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.Data: byte messageId = msg.ReadByte(); Action <NetIncomingMessage> handler = null; if (messageHandlers.TryGetValue(messageId, out handler)) { handler(msg); } else { Debug.LogWarning("No handler for message id " + messageId); } break; case NetIncomingMessageType.StatusChanged: switch (msg.SenderConnection.Status) { case NetConnectionStatus.Connected: if (Connected != null) { Connected(msg.SenderConnection); } break; case NetConnectionStatus.Disconnected: if (Disconnected != null) { Disconnected(msg.SenderConnection); } break; } Debug.Log("Status on " + msg.SenderConnection + " changed to " + msg.SenderConnection.Status); break; case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.VerboseDebugMessage: Debug.Log("Lidgren: " + msg.ReadString()); break; } peer.Recycle(msg); } } }
private void MessageReady(object obj) { var msg = _peer.ReadMessage(); if (msg != null) { _handleMessage(msg); } }
protected virtual void Update() { while ((_im = _peer.ReadMessage()) != default) { this.OnIncomingMessageRecieved.Invoke(this, _im); } _outgoing.Flush(); }
protected void ReadMessages() { if (peer == null) { return; } NetIncomingMessage netIncomingMessage; while ((netIncomingMessage = peer.ReadMessage()) != null) { switch (netIncomingMessage.MessageType) { case NetIncomingMessageType.Data: { byte b = netIncomingMessage.ReadByte(); Action <NetIncomingMessage> value = null; if (messageHandlers.TryGetValue(b, out value)) { value(netIncomingMessage); } else { Debug.LogWarning("No handler for message id " + b); } break; } case NetIncomingMessageType.StatusChanged: switch (netIncomingMessage.SenderConnection.Status) { case NetConnectionStatus.Connected: if (this.Connected != null) { this.Connected(netIncomingMessage); } break; case NetConnectionStatus.Disconnected: if (this.Disconnected != null) { this.Disconnected(netIncomingMessage); } break; } Debug.Log(string.Concat("Status on ", netIncomingMessage.SenderConnection, " changed to ", netIncomingMessage.SenderConnection.Status)); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Debug.Log("Lidgren: " + netIncomingMessage.ReadString()); break; } peer.Recycle(netIncomingMessage); } }
public virtual void Update() { NetIncomingMessage Msg; while ((Msg = Peer.ReadMessage()) != null) { HandleMessage(Msg); } }
public void Update() { NetIncomingMessage message; while ((message = _peer.ReadMessage()) != null) { ReadMessage(message); _peer.Recycle(message); } }
/// <summary> /// Reads incoming messages. To be called continuously. /// </summary> public virtual void Update(int msElapsed) { NetIncomingMessage msg; while ((msg = peer.ReadMessage()) != null) { HandleIncomingMessage(msg); peer.Recycle(msg); } }
static void Main(string[] args) { var config = new NetPeerConfiguration("MultiPlayer"); config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest); config.EnableMessageType(NetIncomingMessageType.ConnectionApproval); config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse); config.AcceptIncomingConnections = true; config.Port = 5002; var Me = new NetPeer(config); Me.Start(); Me.DiscoverLocalPeers(5001); while (true) { NetIncomingMessage message; while ((message = Me.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: Console.WriteLine(message.ReadString()); break; case NetIncomingMessageType.DiscoveryRequest: var response = Me.CreateMessage("Uuum hi..?"); Me.SendDiscoveryResponse(response, message.SenderEndPoint); Console.WriteLine($"Someone at {message.SenderEndPoint.Address} is attempting to discover us!"); break; case NetIncomingMessageType.DiscoveryResponse: Me.Connect(message.SenderEndPoint); Console.WriteLine($"Peer discovered at {message.SenderEndPoint.Address}. Attempting to connect."); //TODO connect? break; case NetIncomingMessageType.ConnectionApproval: message.SenderConnection.Approve(); Console.WriteLine($"Uuum. Someone connected. I don't really know what to do now..."); break; case NetIncomingMessageType.StatusChanged: Console.WriteLine(message.ReadString()); break; default: Console.WriteLine(message.ReadString()); break; } } } }