openspades/Sources/Client/NetClient.h

216 lines
5.6 KiB
C
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-09-05 00:52:03 +09:00
based on code of pysnip (c) Mathias Kaerlev 2011-2012.
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#pragma once
2013-08-18 16:18:06 +09:00
#include <cstdint>
#include <memory>
#include <set>
#include <string>
2019-05-12 18:53:49 +02:00
#include <unordered_map>
#include <vector>
2013-08-18 16:18:06 +09:00
#include "PhysicsConstants.h"
#include "Player.h"
#include <Core/Debug.h>
#include <Core/Math.h>
#include <Core/ServerAddress.h>
2014-03-31 23:09:47 +09:00
#include <Core/Stopwatch.h>
#include <Core/VersionInfo.h>
#include <OpenSpades.h>
2013-08-18 16:18:06 +09:00
struct _ENetHost;
struct _ENetPeer;
typedef _ENetHost ENetHost;
typedef _ENetPeer ENetPeer;
2013-08-18 16:18:06 +09:00
namespace spades {
namespace client {
class Client;
class Player;
enum NetClientStatus {
NetClientStatusNotConnected = 0,
NetClientStatusConnecting,
NetClientStatusReceivingMap,
NetClientStatusConnected
};
2019-05-12 18:53:49 +02:00
enum NetExtensionType {
ExtensionType128Player = 192,
ExtensionTypeMessageTypes = 193,
ExtensionTypeKickReason = 194,
};
2013-08-18 16:18:06 +09:00
class World;
class NetPacketReader;
struct PlayerInput;
struct WeaponInput;
class Grenade;
2017-11-18 21:15:07 +00:00
struct GameProperties;
class GameMapLoader;
2013-08-18 16:18:06 +09:00
class NetClient {
Client *client;
NetClientStatus status;
ENetHost *host;
ENetPeer *peer;
std::string statusString;
class MapDownloadMonitor {
Stopwatch sw;
unsigned int numBytesDownloaded;
GameMapLoader &mapLoader;
bool receivedFirstByte;
public:
MapDownloadMonitor(GameMapLoader &);
void AccumulateBytes(unsigned int);
std::string GetDisplayedText();
};
/** Only valid in the `NetClientStatusReceivingMap` state */
std::unique_ptr<GameMapLoader> mapLoader;
/** Only valid in the `NetClientStatusReceivingMap` state */
std::unique_ptr<MapDownloadMonitor> mapLoadMonitor;
std::shared_ptr<GameProperties> properties;
int protocolVersion;
2019-05-20 00:22:24 +09:00
/** Extensions supported by both client and server (map of extension id → version) */
2019-05-12 18:53:49 +02:00
std::unordered_map<uint8_t, uint8_t> extensions;
2019-05-20 00:22:24 +09:00
/** Extensions implemented in this client (map of extension id → version) */
2019-05-20 00:18:56 +09:00
std::unordered_map<uint8_t, uint8_t> implementedExtensions{
{ExtensionType128Player, 1},
2019-05-12 18:53:49 +02:00
};
2014-03-31 23:09:47 +09:00
class BandwidthMonitor {
ENetHost *host;
Stopwatch sw;
double lastDown;
double lastUp;
2014-03-31 23:09:47 +09:00
public:
BandwidthMonitor(ENetHost *);
double GetDownlinkBps() { return lastDown * 8.; }
double GetUplinkBps() { return lastUp * 8.; }
void Update();
};
2014-03-31 23:09:47 +09:00
std::unique_ptr<BandwidthMonitor> bandwidthMonitor;
2013-08-18 16:18:06 +09:00
std::vector<Vector3> savedPlayerPos;
std::vector<Vector3> savedPlayerFront;
std::vector<int> savedPlayerTeam;
struct PosRecord {
float time;
bool valid;
Vector3 pos;
PosRecord() : valid(false) {}
};
std::vector<PosRecord> playerPosRecords;
std::vector<std::vector<char>> savedPackets;
2013-08-18 16:18:06 +09:00
int timeToTryMapLoad;
bool tryMapLoadOnPacketType;
unsigned int lastPlayerInput;
unsigned int lastWeaponInput;
2013-08-18 16:18:06 +09:00
// used for some scripts including Arena by Yourself
IntVector3 temporaryPlayerBlockColor;
2019-05-12 18:53:49 +02:00
bool HandleHandshakePackets(NetPacketReader &);
void HandleExtensionPacket(NetPacketReader &);
void HandleGamePacket(NetPacketReader &);
stmp::optional<World &> GetWorld();
Player &GetPlayer(int);
stmp::optional<Player &> GetPlayerOrNull(int);
Player &GetLocalPlayer();
stmp::optional<Player &> GetLocalPlayerOrNull();
std::string DisconnectReasonString(uint32_t);
2013-08-18 16:18:06 +09:00
void MapLoaded();
void SendVersion();
void SendVersionEnhanced(const std::set<std::uint8_t> &propertyIds);
2019-05-12 18:53:49 +02:00
void SendSupportedExtensions();
2013-08-18 16:18:06 +09:00
public:
NetClient(Client *);
~NetClient();
NetClientStatus GetStatus() { return status; }
std::string GetStatusString();
/**
* Gets how much portion of the map has completed loading.
* `GetStatus()` must be `NetClientStatusReceivingMap`.
*
* @return A value in range `[0, 1]`.
*/
float GetMapReceivingProgress();
/**
* Return a non-null reference to `GameProperties` for this connection.
* Must be the connected state.
*/
std::shared_ptr<GameProperties> &GetGameProperties() {
SPAssert(properties);
return properties;
}
void Connect(const ServerAddress &hostname);
2013-08-18 16:18:06 +09:00
void Disconnect();
2014-03-16 01:23:30 +09:00
int GetPing();
2013-08-18 16:18:06 +09:00
void DoEvents(int timeout = 0);
void SendJoin(int team, WeaponType, std::string name, int kills);
2013-08-18 16:18:06 +09:00
void SendPosition();
void SendOrientation(Vector3);
void SendPlayerInput(PlayerInput);
void SendWeaponInput(WeaponInput);
void SendBlockAction(IntVector3, BlockActionType);
void SendBlockLine(IntVector3 v1, IntVector3 v2);
void SendReload();
void SendTool();
void SendGrenade(const Grenade &);
2013-08-18 16:18:06 +09:00
void SendHeldBlockColor();
void SendHit(int targetPlayerId, HitType type);
2013-08-18 16:18:06 +09:00
void SendChat(std::string, bool global);
void SendWeaponChange(WeaponType);
void SendTeamChange(int team);
2013-11-07 00:40:03 +10:00
void SendHandShakeValid(int challenge);
2014-03-31 23:09:47 +09:00
double GetDownlinkBps() { return bandwidthMonitor->GetDownlinkBps(); }
double GetUplinkBps() { return bandwidthMonitor->GetUplinkBps(); }
2013-08-18 16:18:06 +09:00
};
} // namespace client
} // namespace spades