Files
Leo-ElixirLights/components/spit/include/SPITSocket.h
T

150 lines
4.3 KiB
C++

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SPITSocket is the base class for WindowsSocket, UnixSocket,.
* Used by SPIT_Client and SAD_Client.
* File: SPITSocket.h
* Author: Hagi
*
* Created on 12. April 2019, 15:40
*/
#ifndef SPITSOCKET_H
#define SPITSOCKET_H
#include "Task.hpp"
#include "esp_log.h"
#include "esp_netif.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <unistd.h>
using namespace std;
/** Returns an guid */
string createGUID();
/** Returns the name of the client's host computer*/
string getClientHostName();
class SPITSocket {
public:
static const int FAMILY_IP4 = AF_INET;
/**
* IPAddress class is used to fetch all available IP addresses of the computer
* This is for internal usage
*/
struct IPAddress {
public:
IPAddress(const string &p_IPAddress, const string &p_SubnetMask)
: m_IPAddress(p_IPAddress), m_SubnetMask(p_SubnetMask) {}
const string m_IPAddress;
const string m_SubnetMask;
/**
* Calculates the broadcast address from a ip4 address
* @param p_IPAddress the ip4 address
* @return returns the broadcast address
*/
string getBroadcastIP4() const {
struct in_addr broadcast;
unsigned long ulIPAddress;
unsigned long ulSubnetMask;
ulIPAddress = inet_addr(m_IPAddress.data());
ulSubnetMask = inet_addr(m_SubnetMask.data());
broadcast.s_addr = ulIPAddress | ~ulSubnetMask;
return inet_ntoa(broadcast);
}
};
const IPAddress mOwnIp;
static IPAddress getAdaptersInfo();
bool createSocket_UDP();
static const int INVALID_SOCKET = -1;
private:
int m_UDPSocketIP4;
public:
//-------- class --------------
SPITSocket(IPAddress ip) : mOwnIp(ip) {
m_UDPSocketIP4 = INVALID_SOCKET;
createSocket_UDP();
}
virtual ~SPITSocket() {
ESP_LOGD("UDP", "Shutting down ...");
if (m_UDPSocketIP4 != INVALID_SOCKET) {
shutdown(m_UDPSocketIP4, 2);
close(m_UDPSocketIP4);
m_UDPSocketIP4 = INVALID_SOCKET;
}
fos::Task::delayMs(100);
m_IsBound = false;
}
private:
string m_ServerIP{};
string m_ServerPort{};
string m_ClientIP{};
string m_ClientPort{}; // port given by the user maybe "0"
string m_BoundClientPort{}; // port set after bind
bool m_IsBound{};
public:
// Sets the ip address and port for server and client.
void setConnectionParams(const string &serverIp, const string &serverPort, const string &clientIp, const string &clientPort) {
m_ServerIP = serverIp;
m_ServerPort = serverPort;
m_ClientIP = clientIp;
m_ClientPort = clientPort;
}
/**
* Send a message to the server as set by setConnectionParams
* @param message byte buffer containing the message
* @param length length of the message
* @return
*/
int sendMessage(const char *message, int length) {
if (m_ServerPort.length() <= 0) {
return -1;
}
if (m_IsBound == false) {
return -1;
}
int intResult = sendMessageTo(m_ServerIP, m_ServerPort, message, length);
return intResult;
}
int receiveMessage(char *p_Buffer, int p_BufferLength, string *serverIp) const;
int setBroadcast(bool p_Broadcast) const;
int bindSocket(string clientIp, string clientPort);
int sendMessageTo(const string &ip, const string &port, const char *message, int msgLength);
string getServerIP() const { return m_ServerIP; }
string getServerPort() const { return m_ServerPort; }
string getClientIP() const { return m_ClientIP; }
string getClientPort() const { return m_ClientPort; }
string getBoundClientPort() const { return m_BoundClientPort; }
void setBoundClientPort(const string &p_Port) { m_BoundClientPort = p_Port; }
void setIsBound(bool p_IsBound) { m_IsBound = p_IsBound; }
bool getIsBound() const { return m_IsBound; }
private:
};
#endif /* SPITSOCKET_H */