static void Main(string[] args) { while (true) { // Server.ReadMessage() Returns new messages, that have not yet been read. // If "inc" is null -> ReadMessage returned null -> Its null, so dont do this :) if ((inc = Server.ReadMessage()) != null) { // Theres few different types of messages. To simplify this process, i left only 2 of em here switch (inc.MessageType) { case NetIncomingMessageType.Error: OnError(); break; case NetIncomingMessageType.ConnectionApproval: OnConnectionApproval(); break; case NetIncomingMessageType.Data: OnData(); break; default: break; } } } }
private void ReadMessages(NetServer server) { NetIncomingMessage msg; while ((msg = server.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.Data: HandleIncomingMessage(MessageHandler.Decode(msg), msg); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: var status = (NetConnectionStatus)msg.ReadByte(); switch (status) { case NetConnectionStatus.Connected: Console.WriteLine("Connection Established " + msg.SenderConnection.RemoteUniqueIdentifier); break; case NetConnectionStatus.Disconnected: Console.WriteLine("User Disconnected" + msg.SenderConnection.RemoteUniqueIdentifier); _room.RemovePlayer(msg.SenderConnection); break; default: Console.WriteLine("Connection Status: " + status.ToString()); break; } break; case NetIncomingMessageType.ConnectionApproval: ConnectionApprovalMessage approval = MessageHandler.Decode(msg) as ConnectionApprovalMessage; Console.WriteLine(approval.Name); if (IsApproved(approval)) { msg.SenderConnection.Approve(); _room.AddPlayer(msg.SenderConnection, approval); } else { msg.SenderConnection.Deny(); } break; default: Console.WriteLine("Unhandled type: " + msg.MessageType); break; } server.Recycle(msg); } }
static void Main(string[] args) { NetPeerConfiguration config = new NetPeerConfiguration("garbagethrower"); config.MaximumConnections = 1; config.Port = 14242; config.PingInterval = 2.0f; config.ConnectionTimeout = 2.0f; var server = new NetServer(config); server.Start(); while (true) { NetIncomingMessage msg; while ((msg = server.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: var status = (NetConnectionStatus)msg.ReadByte(); var reason = msg.ReadString(); Console.WriteLine("New status: " + status + " (" + reason + ")"); break; case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.DebugMessage: var str = msg.ReadString(); if (str.StartsWith("Malformed packet; stated") || str.StartsWith("Received unhandled library message") || str.StartsWith("Unexpected NetMessageType")) { break; // we'll get a bunch of these and we're fine with that } Console.WriteLine(msg.MessageType + ": " + str); break; case NetIncomingMessageType.Data: Console.WriteLine("Received " + msg.LengthBits + " bits of data"); break; case NetIncomingMessageType.UnconnectedData: Console.WriteLine("Received " + msg.LengthBits + " bits of unconnected data"); break; default: Console.WriteLine("Received " + msg.MessageType); break; } } } }
internal void update() { NetIncomingMessage incomingMessage; while ((incomingMessage = server.ReadMessage()) != null) { switch (incomingMessage.MessageType) { case NetIncomingMessageType.StatusChanged: statusChanged(incomingMessage); break; case NetIncomingMessageType.Data: data(incomingMessage); break; case NetIncomingMessageType.Receipt: break; case NetIncomingMessageType.DiscoveryRequest: discoveryRequest(incomingMessage); break; case NetIncomingMessageType.DiscoveryResponse: break; case NetIncomingMessageType.DebugMessage: break; case NetIncomingMessageType.WarningMessage: break; case NetIncomingMessageType.ErrorMessage: break; } server.Recycle(incomingMessage); } }
public void ReadMessages() { NetIncomingMessage message; var stop = false; while (!stop) { message = server.ReadMessage(); Console.Write(""); if (message != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: { Console.WriteLine(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " said: "); var data = message.ReadString(); Console.WriteLine(data); List <NetConnection> all = server.Connections; all.Remove(message.SenderConnection); if (all.Count > 0) { NetOutgoingMessage om = server.CreateMessage(); om.Write(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " said: " + data); server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0); } if (data == "exit") { stop = true; } break; } case NetIncomingMessageType.DebugMessage: Console.WriteLine(message.ReadString()); break; case NetIncomingMessageType.StatusChanged: Console.WriteLine(message.SenderConnection.Status); if (message.SenderConnection.Status == NetConnectionStatus.Connected) { clients.Add(message.SenderConnection.Peer); Console.WriteLine("{0} has connected.", message.SenderConnection.Peer.Configuration.LocalAddress); } if (message.SenderConnection.Status == NetConnectionStatus.Disconnected) { clients.Remove(message.SenderConnection.Peer); Console.WriteLine("{0} has disconnected.", message.SenderConnection.Peer.Configuration.LocalAddress); } break; default: Console.WriteLine("Unhandled message type: {message.MessageType}"); break; } server.Recycle(message); } } Console.WriteLine("Shutdown package \"exit\" received. Press any key to finish shutdown"); Console.ReadKey(); }
static void Main(string[] args) { var config = new NetPeerConfiguration("application name") { Port = 12345 }; var server = new NetServer(config); server.Start(); // server NetIncomingMessage msg; GameRoom newGameRoom = new GameRoom(null, "TEST", 8); NetworkSessionContainer.NetworkSessions.GameRooms.Add(newGameRoom); while (true) { //Ustawić odświerzanie co 16.666ms foreach (GameRoom gameRoom in NetworkSessionContainer.NetworkSessions.GameRooms) { gameRoom.Update(); } if ((msg = server.ReadMessage()) == null) { continue; } switch (msg.MessageType) { case NetIncomingMessageType.ConnectionApproval: { Console.WriteLine("Connected"); break; } case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.StatusChanged: { NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); break; } case NetIncomingMessageType.Data: { short opcode = msg.ReadInt16(); if (opcode == 2000) //Logowanie użytkownika { unsafe { if (!NetworkSessionContainer.NetworkSessions.UserSessions.Exists(x => x.Connection == msg.SenderConnection)) { UserSession session = new UserSession(); TypedReference tr = __makeref(session); IntPtr ptr = **(IntPtr **)(&tr); Console.WriteLine(ptr); session.Connection = msg.SenderConnection; Random randID = new Random(); session.ID = randID.Next(1000000); //martwić sie tym będe później //session.ID = ptr.ToInt32(); session.Name = msg.ReadString(); NetOutgoingMessage outMessage = session.Connection.Peer.CreateMessage(); outMessage.Write((short)2000); outMessage.Write(session.ID); session.Connection.SendMessage(outMessage, NetDeliveryMethod.UnreliableSequenced, outMessage.LengthBytes); session.UserGameState = new MenuState(); NetworkSessionContainer.NetworkSessions.UserSessions.Add(session); // Musze dorobić jakąś obsługe menu } else { //Zaimplementować że jest już taki gość } //TO NIE SPAWN A LOGIN /*foreach (UserSession otherPlayers in UsersSessions.Sessions) * { * NetOutgoingMessage informAboutPlayer = session.Connection.Peer.CreateMessage(); * informAboutPlayer.Write((short)2620); * informAboutPlayer.Write(otherPlayers.ID, 32); * informAboutPlayer.Write("konserwa"); * session.Connection.SendMessage(informAboutPlayer, NetDeliveryMethod.UnreliableSequenced, informAboutPlayer.LengthBytes); * Console.WriteLine($"Wysyłam pakiet od {session.ID} wysyłam dane o {otherPlayers.ID}"); * //Console.WriteLine(BitConverter.ToString(informAboutPlayer.Data)); * * * NetOutgoingMessage SendToCurrentPlayerAboutPlayers = otherPlayers.Connection.Peer.CreateMessage(); * SendToCurrentPlayerAboutPlayers.Write((short)2620); * SendToCurrentPlayerAboutPlayers.Write(session.ID, 32); * SendToCurrentPlayerAboutPlayers.Write("konserwa"); * otherPlayers.Connection.SendMessage(SendToCurrentPlayerAboutPlayers, NetDeliveryMethod.UnreliableSequenced, SendToCurrentPlayerAboutPlayers.LengthBytes); * Console.WriteLine($"Wysyłam pakiet od {otherPlayers.ID} wysyłam dane o {session.ID}"); * // Console.WriteLine(BitConverter.ToString(SendToCurrentPlayerAboutPlayers.Data)); * }*/ } } else { // NetworkSessionContainer.NetworkSessions.UserSessions.Find(x => x.Connection == msg.SenderConnection).UserGameState.Recive(msg); foreach (UserSession user in NetworkSessionContainer.NetworkSessions.UserSessions) { if (user.Connection == msg.SenderConnection) { user.UserGameState.Recive(msg); } } } break; } case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; default: { Console.WriteLine("Unhandled type: " + msg.MessageType); break; } } //server.Recycle(msg); } }
public void ReadMessages() { NetIncomingMessage message; var stop = false; while (!stop) { while ((message = server.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: { Console.WriteLine("I got smth!"); var data = message.ReadString(); Console.WriteLine(data); if (data == "exit") { stop = true; } break; } case NetIncomingMessageType.DebugMessage: Console.WriteLine(message.ReadString()); break; case NetIncomingMessageType.StatusChanged: Console.WriteLine(message.SenderConnection.Status); if (message.SenderConnection.Status == NetConnectionStatus.Connected) { clients.Add(message.SenderConnection.Peer); Console.WriteLine("{0} has connected.", message.SenderConnection.Peer.Configuration.LocalAddress); } if (message.SenderConnection.Status == NetConnectionStatus.Disconnected) { clients.Remove(message.SenderConnection.Peer); Console.WriteLine("{0} has disconnected.", message.SenderConnection.Peer.Configuration.LocalAddress); } break; case NetIncomingMessageType.UnconnectedData: Console.WriteLine("UnconnectedData Modify"); break; case NetIncomingMessageType.DiscoveryResponse: Console.WriteLine("DiscoveryResponse Modify"); break; case NetIncomingMessageType.DiscoveryRequest: Console.WriteLine("DiscoveryRequest Modify"); break; default: Console.WriteLine("Unhandled message type: {message.MessageType}"); break; } server.Recycle(message); } } Console.WriteLine("Shutdown package \"exit\" received. Press any key to finish shutdown"); Console.ReadKey(); }
public static void Uodate(Game1 game, GameTime gameTime) { gameLobbyTimer -= (float)gameTime.ElapsedGameTime.Milliseconds; while ((incmsg = Server.ReadMessage()) != null) { switch (incmsg.MessageType) { case NetIncomingMessageType.Data: string header = incmsg.ReadString(); switch (header) { case "connect": { string username = incmsg.ReadString(); game.Window.Title = "" + Player.players.Count + header; outmsg = Server.CreateMessage(); outmsg.Write("connect"); outmsg.Write(gameLobbyTimer); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); } break; case "CharacterSelection": { string username = incmsg.ReadString(); string selectedChar = incmsg.ReadString(); int x = incmsg.ReadInt32(); int y = incmsg.ReadInt32(); Player.players.Add(new Player(username, new Vector2(x, y), selectedChar)); game.Window.Title = "" + Player.players.Count + header; for (int i = 0; i < Player.players.Count; i++) { outmsg = Server.CreateMessage(); outmsg.Write("CharacterSelection"); outmsg.Write(Player.players[i].name); outmsg.Write(Player.players[i].selectedCharacter); outmsg.Write((int)Player.players[i].pos.X); outmsg.Write((int)Player.players[i].pos.Y); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); } } break; case "move": { game.Window.Title = "" + Player.players.Count + header; try { string username = incmsg.ReadString(); float x = incmsg.ReadFloat(); float y = incmsg.ReadFloat(); string direction = incmsg.ReadString(); for (int i = 0; i < Player.players.Count; i++) { if (Player.players[i].name.Equals(username)) { Player.players[i].pos = new Vector2(x, y); Player.players[i].direction = direction; } } } catch { continue; } } break; case "chat": string chatItem = incmsg.ReadString(); outmsg = Server.CreateMessage(); outmsg.Write("chat"); outmsg.Write(chatItem); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); break; case "newshot": string bulletname = incmsg.ReadString(); float velocityX = incmsg.ReadFloat(); float velocityY = incmsg.ReadFloat(); float damage = incmsg.ReadFloat(); foreach (Player p in Player.players) { if (p.name == bulletname) { outmsg = Server.CreateMessage(); outmsg.Write("newshot"); outmsg.Write(p.name); outmsg.Write(p.pos.X + 16); outmsg.Write(p.pos.Y + 24); outmsg.Write(velocityX); outmsg.Write(velocityY); outmsg.Write(damage); outmsg.Write(p.selectedCharacter); Server.SendMessage(outmsg, Server.Connections, NetDeliveryMethod.ReliableOrdered, 0); } } break; case "bulletCollision": string name = incmsg.ReadString(); float Damage = incmsg.ReadFloat(); for (int i = 0; i < Player.players.Count; i++) { if (Player.players[i].name == name) { Console.WriteLine(Damage); Player.players[i].health -= Damage; Console.WriteLine("bullet message receieved"); Console.WriteLine(Player.players[i].health); } } break; } break; } } }