OpenGTA/net/udp_client.h

168 lines
3.4 KiB
C++
Executable File

#ifndef UDP_CLIENT_H_INCLUDED
#define UDP_CLIENT_H_INCLUDED
#include "Singleton.h"
#include "game_objects.h"
#include <boost/asio.hpp>
#include "entity_controller.h"
namespace net
{
#pragma pack(push, 1)
struct ReceivedDatagram
{
uint32_t number;
uint32_t localTime;
float posX;
float posY;
float posZ;
float angle;
uint8_t pressedButtons;
};
struct PlayerDatagramToSend
{
uint16_t type;
uint16_t state;
float posX;
float posY;
float posZ;
float angle;
float velX;
float velY;
float velZ;
};
struct ServerDatagramToSend
{
uint32_t number = 0;
uint32_t localTime;
uint16_t yourType;
uint16_t numberOfPlayers;
std::array<PlayerDatagramToSend, 4> players;
};
#pragma pack(pop)
}
namespace OpenGTA {
using boost::asio::ip::udp;
using boost::asio::ip::address;
void updateStaticPositionController(const OpenGTA::Map::ObjectPosition& op, PositionController& c);
class UdpClient
{
public:
PlayerPedController playerController;
std::array<PositionController, 65536> staticPositionControllers;
size_t staticPosControllerCount = 0;
PositionController& createStaticPositionController(Vector3D pos);
PositionController& createStaticPositionController(Vector3D pos, float rot);
net::ReceivedDatagram player;
net::ServerDatagramToSend latestServerDatagram;
boost::asio::io_context udpReceiverIoContext;
boost::asio::io_context udpSenderIoContext;
udp::socket receiverSocket{ udpReceiverIoContext };
udp::socket senderSocket{ udpSenderIoContext };
std::array<char, 256> recvBuffer;
udp::endpoint remoteEndpoint;
boost::asio::steady_timer sendTimer{ udpSenderIoContext, boost::asio::chrono::milliseconds(50) };
std::thread receiverThread;
std::thread timerThread;
volatile bool quit = false;
UdpClient();
void wait();
void handleReceive(const boost::system::error_code& error, std::size_t bytes_transferred);
void udpSendTimerHandler(const boost::system::error_code& error);
void start();
};
typedef Loki::SingletonHolder<UdpClient> LocalClient;
/*
class PlayerController : public Util::KeyHandler {
public:
PlayerController() {
reset();
}
void reset() {
playerId = TypeIdBlackBox::getPlayerId();
cash = 0;
wantedLevel = 0;
modifier = 0;
numLives = 0;
pc_ptr = NULL;
}
PedController & getCtrl() {
assert(pc_ptr);
return *pc_ptr;
}
void setCtrl(PedController & pc) {
pc_ptr = &pc;
}
void giveLives(uint16_t k) {
numLives += k;
}
void disableCtrl(bool soft);
void enableCtrl();
Pedestrian & getPed();
Car& getCar();
int32_t getNumLives() { return numLives; }
int32_t getWantedLevel() { return wantedLevel; }
uint32_t getCash() { return cash; }
bool up(const uint32_t & key);
bool down(const uint32_t & key);
uint32_t getId() { return playerId; }
void addCash(uint32_t v) { cash += v; }
void setWanted(int32_t v) { wantedLevel = v; }
void addWanted(uint32_t v) { wantedLevel += v; if (wantedLevel > 5) wantedLevel = 5; }
uint32_t playerCarId = 0;
private:
uint32_t playerId;
uint32_t cash;
int32_t wantedLevel;
uint32_t modifier;
int32_t numLives;
PedController * pc_ptr;
};
typedef Loki::SingletonHolder<PlayerController> LocalPlayer;*/
}
#endif