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); } }
public void AddPlayer(NetConnection connection, ConnectionApprovalMessage approval) { var player = new Player { Connection = connection, Name = approval.Name }; player.Tank = new Tank(); _players[connection.RemoteUniqueIdentifier] = player; Broadcast(new PlayerConnectedMessage { PlayerID = connection.RemoteUniqueIdentifier }, NetDeliveryMethod.ReliableOrdered); }
// Use this for initialization void Start() { var approval = new ConnectionApprovalMessage { Name = "James" }; var config = new NetPeerConfiguration("Tanks2"); _client = new NetClient(config); _messageHandler = new GameMessageHandler(_client); _client.Start(); _client.Connect("localhost", 4200, _messageHandler.Encode(approval)); }
private void ConfigureConnectionApproval(ConnectionApprovalMessage msg, NetIncomingMessage im) { if (msg.Password == password) { im.SenderConnection.Approve(); server.Out.Info($"Approved {msg.Sender} with Endpoint: {im.SenderEndPoint}"); im.SenderConnection.Tag = msg.Sender; server.SendToAllExcept(server.ConvertToOutgoingMessage( new ServerNotification { Message = $"Say welcome to {msg.Sender}" }), im.SenderConnection); } else { im.SenderConnection.Deny(); server.Out.Info($"Denied {msg.Sender} : {im.SenderEndPoint}"); } }
// Update is called once per frame void Update() { NetIncomingMessage msg; while ((msg = _client.ReadMessage()) != null) { ProcessMessage(msg); _client.Recycle(msg); } var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); var controls = new PlayerInput(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), mousePos.x, mousePos.y); var approval = new ConnectionApprovalMessage { Name = "Test" }; _client.SendMessage(_messageHandler.Encode(new PlayerControlUpdateMessage { Controls = controls }), NetDeliveryMethod.UnreliableSequenced, (int)GameMessageType.PlayerControls); }
/// <summary> /// Processes the server incoming messages by using the GemServer's active configuration /// </summary> public void ProcessNetworkMessages() { NetIncomingMessage im; server.Wait(); while ((im = this.server.ReadMessage()) != null) { switch (im.MessageType) { case NetIncomingMessageType.ConnectionApproval: var approvalMsg = new ConnectionApprovalMessage(im); GemNetworkDebugger.Append.Info("Incoming Connection"); GemServer.ServerConfiguration[GemNetwork.ActiveProfile].OnIncomingConnection(server, im.SenderConnection, approvalMsg); break; case NetIncomingMessageType.StatusChanged: switch ((NetConnectionStatus)im.ReadByte()) { case NetConnectionStatus.Connected: //im.SenderConnection.Approve(); GemNetworkDebugger.Append.Info("{0} Connected", im.SenderConnection); break; case NetConnectionStatus.Disconnected: GemNetworkDebugger.Append.Info(im.SenderConnection + " status changed. " + (NetConnectionStatus)im.SenderConnection.Status); GemServer.ServerConfiguration[GemNetwork.ActiveProfile].OnClientDisconnect(server, im.SenderConnection, im.ReadString()); break; case NetConnectionStatus.RespondedConnect: GemNetworkDebugger.Append.Info(im.SenderConnection + " status changed. " + (NetConnectionStatus)im.SenderConnection.Status); break; } break; case NetIncomingMessageType.Data: byte id = im.ReadByte(); if (id == GemNetwork.NotificationByte) { GemServer.ServerConfiguration[GemNetwork.ActiveProfile].HandleNotifications(server, im.SenderConnection, new Notification(im)); } else if (GemServer.MessageFlow[GemNetwork.ActiveProfile, MessageType.Data].HasKey(id)) { GemServer.MessageFlow[GemNetwork.ActiveProfile, MessageType.Data, id] .HandleIncomingMessage(im); } else { var msg = server.CreateMessage(); msg.Write(im); server.SendAndExclude(msg, im.SenderConnection); } break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: GemNetworkDebugger.Append.Warn(im.ReadString()); break; case NetIncomingMessageType.ErrorMessage: GemNetworkDebugger.Append.Error(im.ReadString()); break; case NetIncomingMessageType.DiscoveryRequest: //notify the client break; } this.server.Recycle(im); } }
/// <summary> /// Connect to the server /// </summary> public void Connect(ConnectionConfig connectionDetails, PackageConfig packageConfig, ConnectionApprovalMessage approvalMessage) { this.connectionDetails = connectionDetails; this.PackageConfig = packageConfig; var config = new NetPeerConfiguration(connectionDetails.ServerName); config.EnableMessageType(NetIncomingMessageType.Data); config.EnableMessageType(NetIncomingMessageType.WarningMessage); config.EnableMessageType(NetIncomingMessageType.VerboseDebugMessage); config.EnableMessageType(NetIncomingMessageType.ErrorMessage); config.EnableMessageType(NetIncomingMessageType.Error); config.EnableMessageType(NetIncomingMessageType.DebugMessage); config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse); client = new NetClient(config); client.Start(); //encrypt passwords ? //INetEncryption algo = new NetTripleDESEncryption(encryptionKey); //handshake.Encrypt(algo); //Create the approval message var handshake = CreateMessage(); approvalMessage.Encode(handshake); client.Connect(connectionDetails.ServerIP, handshake); }
private bool IsApproved(ConnectionApprovalMessage approval) { return(approval.Name.Trim() != ""); }
public void ConnectClient(ConnectionApprovalMessage message) { client.Connect("localhost", 1234, message); }