refactor: simplifying project until runs on embedded system
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* SPITSocket is the base class for WindowsSocket, UnixSocket,..<br>
|
||||
* SPITSocket is the base class for WindowsSocket, UnixSocket,.
|
||||
* Used by SPIT_Client and SAD_Client.
|
||||
* File: SPITSocket.h
|
||||
* Author: Hagi
|
||||
@@ -15,66 +15,92 @@
|
||||
|
||||
#ifndef SPITSOCKET_H
|
||||
#define SPITSOCKET_H
|
||||
#include "_SPITMACROS.h"
|
||||
#include "Mutex.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
#include <string>
|
||||
#include "esp_netif.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include "Task.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** Returns an guid */
|
||||
string createGUID();
|
||||
/** Returns the name of the client's host computer*/
|
||||
string getClientHostName();
|
||||
|
||||
class SPITSocket{
|
||||
public:
|
||||
static int FAMILY_IP4;
|
||||
static int FAMILY_IP6;
|
||||
|
||||
//-------- nested classes --------------
|
||||
|
||||
public:
|
||||
static const int FAMILY_IP4 = AF_INET;
|
||||
|
||||
/**
|
||||
* IPAddress class is used to fetch all available IP addresses of the computer.<br>
|
||||
* This is for internal usage.<br>
|
||||
* IPAddress class is used to fetch all available IP addresses of the computer
|
||||
* This is for internal usage
|
||||
*/
|
||||
class IPAddress {
|
||||
struct IPAddress {
|
||||
public:
|
||||
IPAddress();
|
||||
~IPAddress();
|
||||
private:
|
||||
string m_IPAddress;
|
||||
string m_SubnetMask;
|
||||
bool m_Found;
|
||||
public:
|
||||
const string& getIPAddress() {
|
||||
return m_IPAddress;
|
||||
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 string& getSubnetMask() {
|
||||
return m_SubnetMask;
|
||||
}
|
||||
bool getFound() {
|
||||
return m_Found;
|
||||
}
|
||||
|
||||
void setIPAddress(const string& p_IPAddress);
|
||||
void setSubnetMask(const string& p_SubnetMask);
|
||||
void setFound(bool p_Found);
|
||||
};
|
||||
|
||||
const IPAddress mOwnIp;
|
||||
static IPAddress getAdaptersInfo();
|
||||
bool createSocket_UDP();
|
||||
static const int INVALID_SOCKET = -1;
|
||||
|
||||
private:
|
||||
static fos::Mutex s_MutexIPAddresses;
|
||||
int m_UDPSocketIP4;
|
||||
|
||||
public:
|
||||
static unordered_map<string, IPAddress*> s_IPAddresses;
|
||||
static void addIPAddress(string p_IPAddress, string p_SubnetMask);
|
||||
static void removeIPAddress(string p_IPAddress);
|
||||
static int getIPAddressCount();
|
||||
static IPAddress* getIPAddress(int p_Index);
|
||||
static void clearIPAddresses();
|
||||
static void resetFoundIPAddresses();
|
||||
static void removeNotFoundIPAddresses();
|
||||
|
||||
|
||||
//-------- class --------------
|
||||
|
||||
SPITSocket();
|
||||
virtual ~SPITSocket();
|
||||
SPITSocket(IPAddress ip) : mOwnIp(ip) {
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
|
||||
m_ServerIP = "";
|
||||
m_ServerPort = "";
|
||||
m_ClientIP = "";
|
||||
m_ClientPort = "";
|
||||
m_BoundClientPort = "";
|
||||
m_BoundClientFamily = 0;
|
||||
m_IsBound = false;
|
||||
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;
|
||||
@@ -83,7 +109,7 @@ private:
|
||||
string m_BoundClientPort; //port set after bind
|
||||
int m_BoundClientFamily;
|
||||
bool m_IsBound;
|
||||
fos::Mutex m_MutexSocket;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the ip address and port for server and client.
|
||||
@@ -92,79 +118,49 @@ public:
|
||||
* @param p_ClientIP
|
||||
* @param p_ClientPort
|
||||
*/
|
||||
void setConnectionParams(string p_ServerIP, string p_ServerPort, string p_ClientIP, string p_ClientPort);
|
||||
void setConnectionParams(string p_ServerIP, string p_ServerPort, string p_ClientIP, string p_ClientPort) {
|
||||
m_ServerIP = p_ServerIP;
|
||||
m_ServerPort = p_ServerPort;
|
||||
m_ClientIP = p_ClientIP;
|
||||
m_ClientPort = p_ClientPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the server as set by setConnectionParams.<br>
|
||||
* Send a message to the server as set by setConnectionParams
|
||||
* @param p_MessageBytes byte buffer containing the message
|
||||
* @param p_MessageLength length of the messaeg
|
||||
* @return
|
||||
*/
|
||||
int sendMessage(const char* p_MessageBytes, int p_MessageLength);
|
||||
/**
|
||||
* Call this for receiving a message.<br>
|
||||
* This will block until a message is received or the underlying socket is closed.<br>
|
||||
* @param p_InetFamily either AF_INET or AF_INET6<br>
|
||||
* The SPITSocket creates two udp sockets, one for IP4 and one for IP6.<br>
|
||||
* This parameter decides from which socket should a message recieved.<br>
|
||||
* @param p_Buffer the buffer containing the received message later.
|
||||
* @param p_BufferLength the size of the buffer
|
||||
* @param p_ServerIP a pointer to a string, when a messages is received the string will contain the ip address of the sender/server.<br>
|
||||
* if p_ServerIP is NULL - no detection of the server will be done.
|
||||
* @return
|
||||
*/
|
||||
virtual int receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP) = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the broadcast address of an ip4 address.<br>
|
||||
* @param p_IPAdress
|
||||
* @return
|
||||
*/
|
||||
virtual string getBroadcastIP4(IPAddress* p_IPAdress) = 0;
|
||||
/**
|
||||
* Set whether the sockat are brodcast sockets or not
|
||||
* @param p_Broadcast true = the sockets are broadcast sockets
|
||||
* @return 0 = ok, -1 = error
|
||||
*/
|
||||
virtual int setBroadcast(bool p_Broadcast) = 0;
|
||||
/**
|
||||
* Bind the client's udp socket to an ip address and port.<br>
|
||||
* @param p_ClientIP the ip address either IP4 or IP6
|
||||
* @param p_ClientPort the client port number.<br>
|
||||
* "0" = bind to any free port.<br>
|
||||
* @return
|
||||
*/
|
||||
virtual int bindSocket(string p_ClientIP, string p_ClientPort) = 0;
|
||||
/**
|
||||
* Internal usage - don't use this manually.<br>
|
||||
* Use sendMessage instead.<br>
|
||||
* @param p_InetFamily
|
||||
* @param p_ServerIP
|
||||
* @param p_Port
|
||||
* @param p_MessageBytes
|
||||
* @param p_MessageLength
|
||||
* @return
|
||||
*/
|
||||
virtual int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_Port, const char* p_MessageBytes, int p_MessageLength) = 0;
|
||||
/**
|
||||
* Internal usage - don't use this manually.<br>
|
||||
*/
|
||||
virtual void getAdaptersInfo() = 0;
|
||||
|
||||
|
||||
string getServerIP();
|
||||
string getServerPort();
|
||||
string getClientIP();
|
||||
string getClientPort();
|
||||
string getBoundClientPort();
|
||||
void setBoundClientPort(string p_Port);
|
||||
void setBoundClientFamily(int p_Family);
|
||||
int getBoundClientFamily();
|
||||
void setIsBound(bool p_IsBound);
|
||||
bool getIsBound();
|
||||
|
||||
int sendMessage(const char* p_MessageBytes, int p_MessageLength) {
|
||||
if (m_ServerPort.length() <= 0)
|
||||
return -1;
|
||||
int intResult;
|
||||
{
|
||||
if (m_IsBound == false)
|
||||
return -1;
|
||||
intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength);
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
int receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP);
|
||||
int setBroadcast(bool p_Broadcast);
|
||||
int bindSocket(string p_ClientIP, string p_ClientPort);
|
||||
|
||||
// * Internal usage - don't use this manually
|
||||
// * Use sendMessage instead
|
||||
int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_Port, const char* p_MessageBytes, int p_MessageLength);
|
||||
|
||||
string getServerIP() { return m_ServerIP; }
|
||||
string getServerPort(){ return m_ServerPort; }
|
||||
string getClientIP() { return m_ClientIP; }
|
||||
string getClientPort() { return m_ClientPort; }
|
||||
string getBoundClientPort() { return m_BoundClientPort; }
|
||||
void setBoundClientPort(string p_Port) { m_BoundClientPort = p_Port; }
|
||||
void setBoundClientFamily(int p_Family) { m_BoundClientFamily = p_Family; }
|
||||
int getBoundClientFamily() { return m_BoundClientFamily; }
|
||||
void setIsBound(bool p_IsBound) { m_IsBound = p_IsBound; }
|
||||
bool getIsBound() { return m_IsBound; }
|
||||
};
|
||||
|
||||
#endif /* SPITSOCKET_H */
|
||||
|
||||
Reference in New Issue
Block a user