public void PeerListener(NetPeer peer) { while (true) { NetIncomingMessage msg; while ((msg=peer.ReadMessage())!=null) { NetConnection connection = msg.SenderConnection; switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: switch (msg.SenderConnection.Status) { case NetConnectionStatus.Connected: logger.Info("{0}: connected",connection.RemoteEndPoint.Address.ToString()); ExecuteCommand(new CmdInitOk(connection)); break; case NetConnectionStatus.Disconnected: logger.Info("{0}: disconnected", connection.RemoteEndPoint.Address.ToString()); break; case NetConnectionStatus.Disconnecting: logger.Info("{0}: disconnecting", connection.RemoteEndPoint.Address.ToString()); break; } break; case NetIncomingMessageType.Data: msg.Decrypt(new NetRC2Encryption(peer, connection.CryptoKey)); string[] OpData = msg.ReadString().Split('&'); var OpCode = (LoginOperation)Enum.Parse(typeof(LoginOperation), OpData[0]); switch (OpCode) { case LoginOperation.AuthLog: string[] authData = OpData[1].Split('#'); string login = authData[0]; string pass = authData[1]; ExecuteCommand(new CmdLoginOkFail(connection, login,pass)); break; } break; } peer.Recycle(msg); } } }
void receiveData(GameTime gameTime) { timeSinceMessageReceived += (float)gameTime.ElapsedGameTime.TotalSeconds; if (timeSinceMessageReceived >= timeOutDelay) { waitingForMessage = true; } NetIncomingMessage msg; while ((msg = netPeer.ReadMessage()) != null) { timeSinceMessageReceived = 0f; waitingForMessage = false; CurrentPing = MathHelper.Max(connection.AverageRoundtripTime, OtherClientPing); currentScheduleTime = GameClock + CurrentPing * 1.5f; //currentScheduleTime = GameClock + .2f; timeOutDelay = MathHelper.Max(.53f, CurrentPing * 1.2f); if (msg.MessageType == NetIncomingMessageType.Data) { byte id = msg.ReadByte(); if (id == MessageID.SYNC) { processSync(msg); } else if (id == MessageID.UNIT_MOVE_COMMAND_BATCH) { processUnitMoveCommandBatch(msg); } else if (id == MessageID.STRUCTURE_COMMAND) { processStructureCommand(msg); } else if (id == MessageID.RALLY_POINT_COMMAND) { processRallyPointCommand(msg); } else if (id == MessageID.UNIT_STATUS_UPDATE) { processUnitStatusUpdate(msg); } else if (id == MessageID.UNIT_HP_UPDATE) { processUnitHpUpdate(msg); } else if (id == MessageID.STRUCTURE_STATUS_UPDATE) { processStructureStatusUpdate(msg); } else if (id == MessageID.RESOURCE_STATUS_UPDATE) { processResourceStatusUpdate(msg); } else if (id == MessageID.UNIT_ATTACK_COMMAND_BATCH) { processUnitAttackCommandBatch(msg); } else if (id == MessageID.UNIT_ATTACK_MOVE_COMMAND_BATCH) { processUnitAttackMoveCommandBatch(msg); } else if (id == MessageID.UNIT_BUILD_COMMAND) { processUnitBuildCommand(msg); } else if (id == MessageID.UNIT_HARVEST_COMMAND_BATCH) { processUnitHarvestCommandBatch(msg); } else if (id == MessageID.UNIT_RETURN_CARGO_COMMAND_BATCH) { processUnitReturnCargoCommandBatch(msg); } else if (id == MessageID.UNIT_STOP_COMMAND_BATCH) { processUnitStopCommandBatch(msg); } else if (id == MessageID.UNIT_HOLD_POSITION_COMMAND_BATCH) { processUnitHoldPositionCommandBatch(msg); } else if (id == MessageID.UNIT_DEATH) { processUnitDeath(msg); } else if (id == MessageID.STRUCTURE_DEATH) { processStructureDeath(msg); } else if (id == MessageID.UNIT_CANCEL_CONFIRMATION) { processUnitCancelConfirmation(msg); } /*else if (id == MessageID.PATH_UPDATE) * { * processPathUpdate(msg); * }*/ } netPeer.Recycle(msg); } }
public bool PumpEvents() { _incomingMessages.Clear(); _peer.ReadMessages(_incomingMessages); foreach (NetIncomingMessage incomingMessage in _incomingMessages) { try { LidgrenTransportChannel transportChannel; switch (incomingMessage.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: _logger.Debug(incomingMessage.ReadString()); break; case NetIncomingMessageType.WarningMessage: _logger.Warning(incomingMessage.ReadString()); break; case NetIncomingMessageType.ErrorMessage: _logger.Error(incomingMessage.ReadString()); break; case NetIncomingMessageType.StatusChanged: var status = (NetConnectionStatus)incomingMessage.ReadByte(); switch (status) { case NetConnectionStatus.InitiatedConnect: case NetConnectionStatus.ReceivedInitiation: case NetConnectionStatus.RespondedAwaitingApproval: case NetConnectionStatus.RespondedConnect: case NetConnectionStatus.Disconnecting: _logger.StatusChanged(status.ToString(), incomingMessage.SenderConnection.RemoteUniqueIdentifier); break; case NetConnectionStatus.Connected: _logger.StatusChanged(status.ToString(), incomingMessage.SenderConnection.RemoteUniqueIdentifier); var channel = new LidgrenTransportChannel(incomingMessage.SenderConnection); _processIncommingConnection(channel); _channels.Add(incomingMessage.SenderConnection.RemoteUniqueIdentifier, channel); break; case NetConnectionStatus.Disconnected: _logger.StatusChanged(status.ToString(), incomingMessage.SenderConnection.RemoteUniqueIdentifier); if (_channels.TryGetValue(incomingMessage.SenderConnection.RemoteUniqueIdentifier, out transportChannel)) { transportChannel.OnDisconnect(); _channels.Remove(incomingMessage.SenderConnection.RemoteUniqueIdentifier); } break; default: throw new ArgumentOutOfRangeException(); } break; case NetIncomingMessageType.Data: int numBytes = incomingMessage.ReadInt32(); _logger.OnDataReceived(incomingMessage.SenderConnection.RemoteUniqueIdentifier, numBytes); if (_channels.TryGetValue(incomingMessage.SenderConnection.RemoteUniqueIdentifier, out transportChannel)) { incomingMessage.ReadBytes(_buffer, 0, numBytes); transportChannel.OnDataReceived(new ArraySegment <byte>(_buffer, 0, numBytes)); } break; default: _logger.Debug("Received " + incomingMessage.MessageType); break; } } finally { _peer.Recycle(incomingMessage); } } return(false); }
private static void Listen(NetPeer peer) { //TODO FIXME! if (peer == null) return; NetIncomingMessage msg; while ((msg = peer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.Data: //JapeLog.WriteLine("Message received: Data"); IncomingData(msg); break; case NetIncomingMessageType.ConnectionApproval: // Read the first byte of the packet // ( Enums can be casted to bytes, so it be used to make bytes human readable ) int type = msg.ReadInt32(); if (type == (int)DataType.Login) { JapeLog.WriteLine("Incoming LOGIN"); msg.SenderConnection.Approve(); string hailMessage = msg.ReadString(); JapeLog.WriteLine("Approved new connection: " + hailMessage); } break; case NetIncomingMessageType.StatusChanged: if (msg.SenderConnection.Status == NetConnectionStatus.Connected) { if (!IsHost) { SendMessageParams(NetDeliveryMethod.ReliableOrdered, (int)DataType.NewPlayer, DataStorage.GetLocalPlayerConfig().UserName, (IsHost) ? 0 : -1, RemoteUID ); JapeLog.WriteLine("SEND NEWPLAYER"); } else JapeLog.WriteLine("This server is Dedicated (Not sending a new player message)"); } if (msg.SenderConnection.Status == NetConnectionStatus.Disconnected) { JapeLog.WriteLine("Player Disconnected"); //if player is in connected => remove from play } if (msg.SenderConnection.Status == NetConnectionStatus.Disconnecting) { JapeLog.WriteLine("Player Disconnecting"); //if player is in connected => remove from play } break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: //JapeLog.WriteLine(msg.ReadString()); break; } peer.Recycle(msg); } }
public bool PumpEvents() { _incomingMessages.Clear(); _peer.ReadMessages(_incomingMessages); foreach (NetIncomingMessage incomingMessage in _incomingMessages) { switch (incomingMessage.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: Log.Debug(incomingMessage.ReadString()); break; case NetIncomingMessageType.WarningMessage: Log.Warn(incomingMessage.ReadString()); break; case NetIncomingMessageType.ErrorMessage: Log.Error(incomingMessage.ReadString()); break; case NetIncomingMessageType.StatusChanged: var status = (NetConnectionStatus)incomingMessage.ReadByte(); switch (status) { case NetConnectionStatus.InitiatedConnect: break; case NetConnectionStatus.ReceivedInitiation: break; case NetConnectionStatus.RespondedAwaitingApproval: break; case NetConnectionStatus.RespondedConnect: break; case NetConnectionStatus.Connected: var channel = new LidgrenNetChannel(incomingMessage.SenderConnection, _bufferManager); INetChannelListener listener = _peerFactory.OnNetConnect(channel); channel.Listener = listener; _channels.Add(incomingMessage.SenderConnection.RemoteUniqueIdentifier, channel); break; case NetConnectionStatus.Disconnecting: Log.Debug("Channel #{0} is disconnecting", incomingMessage.SenderConnection.RemoteUniqueIdentifier); break; case NetConnectionStatus.Disconnected: Log.Debug("Channel #{0} has been disconnected", incomingMessage.SenderConnection.RemoteUniqueIdentifier); LidgrenNetChannel netChannel; if (_channels.TryGetValue(incomingMessage.SenderConnection.RemoteUniqueIdentifier, out netChannel)) { netChannel.Listener.OnDisconnected(); _channels.Remove(incomingMessage.SenderConnection.RemoteUniqueIdentifier); netChannel.Dispose(); } break; default: throw new ArgumentOutOfRangeException(); } break; case NetIncomingMessageType.Data: int numBytes = incomingMessage.ReadInt32(); incomingMessage.ReadBytes(_buffer, 0, numBytes); _stream.Position = 0; _channels[incomingMessage.SenderConnection.RemoteUniqueIdentifier].Listener.OnNetData(_reader); break; default: Log.Debug("Received " + incomingMessage.MessageType); break; } _peer.Recycle(incomingMessage); } return(false); }