#define DEFINES_H
typedef unsigned char UCHAR;
-typedef unsigned short USHORT;
#include <stdint.h>
else if (checkA) // If no checkC, must be checkA
{
LogNT::getInstance()->info(TAG, "Starting with IP: {} {}", lircIP, lircPort);
- tr = tcp.connect(lircIP, static_cast<USHORT>(lircPort));
+ tr = tcp.connect(lircIP, static_cast<u2>(lircPort));
}
if (!tr)
log->debug(TAG, "Starting InputUDP command server on port {}", port);
- if (!udp6.init(static_cast<USHORT>(port)))
+ if (!udp6.init(static_cast<u2>(port)))
{
log->debug(TAG, "UDP6 init error");
initted = false;
current+=sizeof(u4);
return 0;
}
-int SerializeBuffer::encodeShort(USHORT data) {
- if (checkSpace( (int)sizeof(USHORT))!=0) return -1;
- *((USHORT *)(current))=htons(data);
- current+=sizeof(USHORT);
+int SerializeBuffer::encodeShort(u2 data) {
+ if (checkSpace( (int)sizeof(u2))!=0) return -1;
+ *((u2 *)(current))=htons(data);
+ current+=sizeof(u2);
return 0;
}
int SerializeBuffer::encodeByte(UCHAR data) {
current+=sizeof(u4);
return 0;
}
-int SerializeBuffer::decodeShort(USHORT &data) {
- if (checkSpace( (int)sizeof(USHORT))!=0) return -1;
- data=ntohs(*((USHORT *)(current)));
- current+=sizeof(USHORT);
+int SerializeBuffer::decodeShort(u2 &data) {
+ if (checkSpace( (int)sizeof(u2))!=0) return -1;
+ data=ntohs(*((u2 *)(current)));
+ current+=sizeof(u2);
return 0;
}
int SerializeBuffer::decodeByte(UCHAR &data) {
return rt;
}
-USHORT Serializable::getVersion() {
+u2 Serializable::getVersion() {
return version;
}
}
int Serializable::deserialize(SerializeBuffer *b) {
- USHORT vers=0;
+ u2 vers=0;
if (b->decodeShort(vers) != 0) return -1;
u4 len=0;
if (b->decodeLong(len) != 0) return -1;
}
SerializableList::~SerializableList(){}
-int SerializableList::addParam(Serializable *p,USHORT v) {
+int SerializableList::addParam(Serializable *p,u2 v) {
if (v < version || p == NULL) return -1;
Pentry entry;
entry.ptype=TSER;
version=v;
return 0;
}
-int SerializableList::addParam(USHORT *p,USHORT v) {
+int SerializableList::addParam(u2 *p,u2 v) {
if (v < version || p == NULL) return -1;
Pentry entry;
- entry.ptype=TUSHORT;
+ entry.ptype=Tu2;
entry.ptr.pshort=p;
entry.version=v;
list.push_back(entry);
version=v;
return 0;
}
-int SerializableList::addParam(u4 *p,USHORT v) {
+int SerializableList::addParam(u4 *p,u2 v) {
if (v < version || p == NULL) return -1;
Pentry entry;
entry.ptype=TULONG;
version=v;
return 0;
}
-int SerializableList::addParam(u8 *p,USHORT v) {
+int SerializableList::addParam(u8 *p,u2 v) {
if (v < version || p == NULL) return -1;
Pentry entry;
entry.ptype=TULLONG;
version=v;
return 0;
}
-int SerializableList::addParam(char **p,USHORT v) {
+int SerializableList::addParam(char **p,u2 v) {
if (v < version || p == NULL) return -1;
Pentry entry;
entry.ptype=TCHAR;
bool SerializableList::Pentry::isEqual(void *p,SerializableList::Ptypes t) {
void *cmp=NULL;
switch(t) {
- case TUSHORT:
+ case Tu2:
cmp=(void *)ptr.pshort;
break;
case TULONG:
if (!e) return false;
return e->isDeserialized;
}
-bool SerializableList::isDeserialized(USHORT *p){
- SerializableList::Pentry *e=findEntry(p,TUSHORT);
+bool SerializableList::isDeserialized(u2 *p){
+ SerializableList::Pentry *e=findEntry(p,Tu2);
if (!e) return false;
return e->isDeserialized;
}
int rt=0;
for (vector<Pentry>::iterator it=list.begin();it<list.end();it++) {
switch((*it).ptype){
- case TUSHORT:
- rt+=sizeof(USHORT);
+ case Tu2:
+ rt+=sizeof(u2);
break;
case TULONG:
rt+=sizeof(u4);
int SerializableList::serializeImpl(SerializeBuffer *b){
for (vector<Pentry>::iterator it=list.begin();it<list.end();it++) {
switch((*it).ptype){
- case TUSHORT:
+ case Tu2:
if (b->encodeShort(*(*it).ptr.pshort) != 0) return -1;
break;
case TULONG:
break;
}
switch((*it).ptype){
- case TUSHORT:
+ case Tu2:
if (b->decodeShort(*(*it).ptr.pshort) != 0) return -1;
break;
case TULONG:
//always return != 0 on error
int encodeLong(u4 data);
int encodeLongLong(u8 data);
- int encodeShort(USHORT data);
+ int encodeShort(u2 data);
int encodeString(const char *data);
int encodeByte(UCHAR data);
int decodeLong(u4 & data);
int decodeLong(int & data);
int decodeLongLong(u8 & data);
- int decodeShort(USHORT & data);
+ int decodeShort(u2 & data);
int decodeString(u4 &len,char * &data);
int decodeByte(UCHAR &data);
int deserialize(SerializeBuffer *b);
//or the received version after deserialize
//can be queried with deserializeImpl
- USHORT getVersion();
+ u2 getVersion();
//helper
static int getSerializedStringLen(const char *str);
protected:
virtual int getSerializedLenImpl()=0;
virtual int serializeImpl(SerializeBuffer *b)=0;
virtual int deserializeImpl(SerializeBuffer *b)=0;
- USHORT version;
+ u2 version;
};
/**
* by correct usage.
* usage example:
* 1. version
- * USHORT myP1=0;
+ * u2 myP1=0;
* u4 myP2=0;
* SerializableList myList();
* myList.addParam(&myP1);
* myList.addParam(&myP2);
* //now serialize/deserialize...
* //later - second version:
- * USHORT myP1=0;
+ * u2 myP1=0;
* u4 myP2=0;
* char *myString=NULL;
* SerializableList myList();
* compatibility
* will return !=0 if adding is not possible (e.g. adding with a smaller version)
*/
- int addParam(Serializable *p,USHORT version=1);
- int addParam(USHORT *p,USHORT version=1);
- int addParam(u4 *p,USHORT version=1);
- int addParam(u8 *p,USHORT version=1);
- int addParam(char **p,USHORT version=1);
+ int addParam(Serializable *p,u2 version=1);
+ int addParam(u2 *p,u2 version=1);
+ int addParam(u4 *p,u2 version=1);
+ int addParam(u8 *p,u2 version=1);
+ int addParam(char **p,u2 version=1);
/**
* for lists only intended for encoding also
* the non-const
* After having called one of this methods deserialize will always fail!
*/
- int addParam(const Serializable *p,USHORT vs=1){
+ int addParam(const Serializable *p,u2 vs=1){
encodeOnly=true;
return addParam(const_cast<Serializable*>(p),vs);
}
- int addParam(const USHORT *p,USHORT vs=1){
+ int addParam(const u2 *p,u2 vs=1){
encodeOnly=true;
- return addParam(const_cast<USHORT*>(p),vs);
+ return addParam(const_cast<u2*>(p),vs);
}
- int addParam(const u4 *p,USHORT vs=1){
+ int addParam(const u4 *p,u2 vs=1){
encodeOnly=true;
return addParam(const_cast<u4*>(p),vs);
}
- int addParam(const int *p,USHORT vs=1){
+ int addParam(const int *p,u2 vs=1){
encodeOnly=true;
return addParam(reinterpret_cast<u4*>(const_cast<int*>(p)), vs);
}
- int addParam(const u8 *p,USHORT vs=1){
+ int addParam(const u8 *p,u2 vs=1){
encodeOnly=true;
return addParam(const_cast<u8*>(p),vs);
}
- int addParam(const char **p,USHORT vs=1){
+ int addParam(const char **p,u2 vs=1){
encodeOnly=true;
return addParam(const_cast<char**>(p),vs);
}
* during deserialize
*/
bool isDeserialized(Serializable *p);
- bool isDeserialized(USHORT *p);
+ bool isDeserialized(u2 *p);
bool isDeserialized(u4 *p);
bool isDeserialized(u8 *p);
bool isDeserialized(char **p);
typedef enum{
TUNKNOWN,
TSER,
- TUSHORT,
+ Tu2,
TULONG,
TULLONG,
TCHAR } Ptypes;
struct Pentry {
Ptypes ptype;
bool isDeserialized;
- USHORT version;
+ u2 version;
union {
Serializable *pser;
- USHORT *pshort;
+ u2 *pshort;
u4 *plong;
u8 *pllong;
char **pchar;
}
#endif
-bool TCP::connect(const std::string& ip, USHORT port)
+bool TCP::connect(const std::string& ip, u2 port)
{
if (connected) return false;
void shutdown(); // Optional if this is to be deleted. Closes connection. Can call connect() next
void abortCall(); // causes a read/connect call to immediately abort
- bool connect(const std::string& ip, USHORT port);
+ bool connect(const std::string& ip, u2 port);
#ifndef WIN32
bool connectSocket(const std::string& socketFile);
#endif
if (initted) shutdown();
}
-int UDP4::init(USHORT tport)
+int UDP4::init(u2 tport)
{
if (initted) return 0;
const char* UDP4::getFromIPA() const { return fromIPA; }
short UDP4::getFromPort() const { return fromPort; }
-bool UDP4::send(const char *ipa, USHORT port, char *message, int length)
+bool UDP4::send(const char *ipa, u2 port, char *message, int length)
{
if (!initted) return false;
{
public:
~UDP4();
- int init(USHORT port);
+ int init(u2 port);
void shutdown();
#ifdef WIN32
unsigned char waitforMessage(unsigned char, SOCKET quitPipe = 0); // uchar =0-block =1-new wait =2-continue wait
const void* getData() const; // returns a pointer to the data
const char* getFromIPA() const; // returns a pointer to from IP address
short getFromPort() const;
- bool send(const char *, USHORT, char *, int); // send wants: IP Address ddn style, port, data, length of data
+ bool send(const char *, u2, char *, int); // send wants: IP Address ddn style, port, data, length of data
private:
bool initted{};
u4 getIPNumber(u4 num);
u4 iterate_ip{};
int socketnum; // Socket descriptor
- USHORT myPort{}; // My port number
+ u2 myPort{}; // My port number
struct sockaddr_in myAddr; // My address
struct sockaddr_in theirAddr; // User address
socklen_t addrlen; // length of sockaddr struct
if (initted) shutdown();
}
-int UDP6::init(USHORT tPort)
+int UDP6::init(u2 tPort)
{
if (initted) return 0;
myPort = tPort;
short UDP6::getFromPort() const { return fromPort; }
bool UDP6::getFromIPisLL() const { return fromIPisLL; }
-bool UDP6::send(const char *ipa, USHORT port, char *message, int length, bool mcast)
+bool UDP6::send(const char *ipa, u2 port, char *message, int length, bool mcast)
{
if (!initted) return false;
int sentLength = 0;
{
public:
~UDP6();
- int init(USHORT port);
+ int init(u2 port);
void shutdown();
#ifdef WIN32
unsigned char waitforMessage(unsigned char, SOCKET quitPipe = 0); // uchar =0-block =1-new wait =2-continue wait
const char* getFromIPA() const; // returns a pointer to from IP address
short getFromPort() const;
bool getFromIPisLL() const;
- bool send(const char *, USHORT, char *, int, bool mcast = false); // send wants: text IP, port, data, length of data, is_mcast
+ bool send(const char *, u2, char *, int, bool mcast = false); // send wants: text IP, port, data, length of data, is_mcast
private:
bool initted{};
u4 getIPNumber(u4 num);
u4 iterate_ip{};
int socketnum; // Socket descriptor
- USHORT myPort{}; // My port number
+ u2 myPort{}; // My port number
struct sockaddr_in6 myAddr; // My address
struct sockaddr_in6 theirAddr; // User address
socklen_t addrlen; // length of sockaddr struct
int newPort; // copy around because of type mismatch
if (config->getInt("server", "port", newPort))
{
- serverFromConfig.port = static_cast<USHORT>(newPort);
+ serverFromConfig.port = static_cast<u2>(newPort);
logger->debug(TAG, "Port read fron config: {}", serverFromConfig.port);
}
{
logger = LogNT::getInstance();
- AddServerCallback addFunc( [this] (int ipVersion, const char* ip, const char* name, USHORT port, u4 version)
+ AddServerCallback addFunc( [this] (int ipVersion, const char* ip, const char* name, u2 port, u4 version)
{
std::lock_guard<std::mutex> lg(serversLock);
{
// FIXME upgrade this to look for a return IP in the reply packet
- USHORT newServerPort;
+ u2 newServerPort;
memcpy(&newServerPort, &vdpreply[26], 2);
u4 newServerVersion;
{
// FIXME upgrade this to look for a return IP in the reply packet
- USHORT newServerPort;
+ u2 newServerPort;
memcpy(&newServerPort, &vdpreply[26], 2);
u4 newServerVersion;
int ipVersion;
std::string ip;
std::string name;
- USHORT port;
+ u2 port;
u4 version;
VDRServer() {}
- VDRServer(int tipVersion, const std::string tip, const std::string tname, USHORT tport, u4 tversion)
+ VDRServer(int tipVersion, const std::string tip, const std::string tname, u2 tport, u4 tversion)
: ipVersion(tipVersion), ip(tip), name(tname), port(tport), version(tversion) {}
};
{
private:
LogNT* logger{};
- typedef std::function<void(int, const char*, const char*, USHORT, u4)> AddServerCallback;
+ typedef std::function<void(int, const char*, const char*, u2, u4)> AddServerCallback;
// Nested classes
#ifdef IPV4
strcpy(serverIP, newIP);
}
-void VDR::setServerPort(USHORT newPort)
+void VDR::setServerPort(u2 newPort)
{
serverPort = newPort;
}
*/
void setServerIP(const char*);
- void setServerPort(USHORT);
+ void setServerPort(u2);
void setReceiveWindow(size_t size);
bool connect();
void disconnect();
int findingServer{};
TCP tcp;
char serverIP[40];
- USHORT serverPort;
+ u2 serverPort;
u4 maxChannelNumber{};
bool doVDRShutdown{};
int channelNumberWidth{1};