| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <map> |
| 24 |
#include <mutex> |
| 25 |
#include <sstream> |
| 26 |
#include <thread> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include <sys/socket.h> |
| 30 |
|
| 31 |
#include "ControlInterface.h" |
| 32 |
#include "Logger/Logger.h" |
| 33 |
#include "Manager.h" |
| 34 |
#include "Network/UdpServer.h" |
| 35 |
|
| 36 |
namespace Server { namespace CS2 |
| 37 |
{ |
| 38 |
class CS2Client; |
| 39 |
|
| 40 |
class CS2Server : public ControlInterface, private Network::TcpServer |
| 41 |
{ |
| 42 |
public: |
| 43 |
static const unsigned short CS2ReceiverPort = 15731; |
| 44 |
static const unsigned short CS2SenderPort = 15730; |
| 45 |
|
| 46 |
CS2Server() = delete; |
| 47 |
CS2Server(const CS2Server&) = delete; |
| 48 |
CS2Server& operator=(const CS2Server&) = delete; |
| 49 |
|
| 50 |
CS2Server(Manager& manager); |
| 51 |
~CS2Server(); |
| 52 |
|
| 53 |
void Start() override; |
| 54 |
|
| 55 |
void Stop() override; |
| 56 |
|
| 57 |
void Work(Network::TcpConnection* connection) override; |
| 58 |
|
| 59 |
inline const std::string& GetName() const override |
| 60 |
{ |
| 61 |
static const std::string CS2Name("CS2Server"); |
| 62 |
return CS2Name; |
| 63 |
} |
| 64 |
|
| 65 |
void Booster(const ControlType controlType, const BoosterState status) override; |
| 66 |
|
| 67 |
void LocoBaseOrientation(const ControlType controlType, |
| 68 |
const DataModel::LocoBase* loco, |
| 69 |
const Orientation direction) override; |
| 70 |
|
| 71 |
void LocoBaseFunction(const ControlType controlType, |
| 72 |
const DataModel::LocoBase* loco, |
| 73 |
const DataModel::LocoFunctionNr function, |
| 74 |
const DataModel::LocoFunctionState on) override; |
| 75 |
|
| 76 |
void LocoBaseSpeed(const ControlType controlType, |
| 77 |
const DataModel::LocoBase* loco, |
| 78 |
const Speed speed) override; |
| 79 |
|
| 80 |
void AccessoryState(const ControlType controlType, const DataModel::Accessory* accessory) override; |
| 81 |
|
| 82 |
void SwitchState(const ControlType controlType, const DataModel::Switch* mySwitch) override; |
| 83 |
|
| 84 |
void SignalState(const ControlType controlType, const DataModel::Signal* signal) override; |
| 85 |
|
| 86 |
private: |
| 87 |
Logger::Logger* logger; |
| 88 |
Manager& manager; |
| 89 |
std::vector<CS2Client*> clients; |
| 90 |
unsigned int lastClientID; |
| 91 |
|
| 92 |
bool runUdp; |
| 93 |
std::thread udpServerThread; |
| 94 |
int udpServerSocket; |
| 95 |
|
| 96 |
void AccessoryBaseState(const DataModel::AccessoryBase* accessoryBase); |
| 97 |
|
| 98 |
void CleanUpClients(); |
| 99 |
|
| 100 |
void StartUdpServer(); |
| 101 |
|
| 102 |
void TerminateUdpServer(); |
| 103 |
|
| 104 |
void UdpSocketCreateBindListen(int family, struct sockaddr* address); |
| 105 |
|
| 106 |
void UdpWorker(); |
| 107 |
}; |
| 108 |
}} |