update cspot

This commit is contained in:
philippe44
2022-11-17 14:06:00 -08:00
parent a81d0e0513
commit 7e5f27af12
137 changed files with 6046 additions and 836 deletions

View File

@@ -1,10 +1,26 @@
#include "PlainConnection.h"
#include <cstring>
#ifdef _WIN32
#include <ws2tcpip.h>
#else
#include <netinet/tcp.h>
#endif
#include <errno.h>
#include "Logger.h"
static int getErrno()
{
#ifdef _WIN32
int code = WSAGetLastError();
if (code == WSAETIMEDOUT) return ETIMEDOUT;
if (code == WSAEINTR) return EINTR;
return code;
#else
return errno;
#endif
}
PlainConnection::PlainConnection()
{
this->apSock = -1;
@@ -46,11 +62,15 @@ void PlainConnection::connectToAp(std::string apAddress)
(struct sockaddr *)ai->ai_addr,
ai->ai_addrlen) != -1)
{
#ifdef _WIN32
uint32_t tv = 3000;
#else
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 0;
setsockopt(this->apSock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv);
setsockopt(this->apSock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tv, sizeof tv);
#endif
setsockopt(this->apSock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
setsockopt(this->apSock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof tv);
int flag = 1;
setsockopt(this->apSock, /* socket affected */
@@ -75,7 +95,6 @@ std::vector<uint8_t> PlainConnection::recvPacket()
// Read packet size
auto sizeData = readBlock(4);
uint32_t packetSize = ntohl(extract<uint32_t>(sizeData, 0));
// Read actual data
auto data = readBlock(packetSize - 4);
sizeData.insert(sizeData.end(), data.begin(), data.end());
@@ -110,9 +129,9 @@ std::vector<uint8_t> PlainConnection::readBlock(size_t size)
while (idx < size)
{
READ:
if ((n = recv(this->apSock, &buf[idx], size - idx, 0)) <= 0)
if ((n = recv(this->apSock, (char*) &buf[idx], size - idx, 0)) <= 0)
{
switch (errno)
switch (getErrno())
{
case EAGAIN:
case ETIMEDOUT:
@@ -145,9 +164,9 @@ size_t PlainConnection::writeBlock(const std::vector<uint8_t> &data)
while (idx < data.size())
{
WRITE:
if ((n = send(this->apSock, &data[idx], data.size() - idx < 64 ? data.size() - idx : 64, 0)) <= 0)
if ((n = send(this->apSock, (char*) &data[idx], data.size() - idx < 64 ? data.size() - idx : 64, 0)) <= 0)
{
switch (errno)
switch (getErrno())
{
case EAGAIN:
case ETIMEDOUT: