Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -12,7 +12,8 @@ endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
set(CMAKE_CXX_FLAGS "-pedantic -Wall -Wextra")
|
||||
# This causes an unhinged compiler error.
|
||||
#set(CMAKE_CXX_FLAGS "-pedantic -Wall -Wextra")
|
||||
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
|
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <cerrno>
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#include <arpa/inet.h>
|
||||
@@ -7,13 +10,16 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#elif _WIN32
|
||||
#include <winsock32.h>
|
||||
#elif defined(WIN32)
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2ipdef.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <cerrno>
|
||||
|
||||
|
||||
#define FDR_UNUSED(expr){ (void)(expr); }
|
||||
#define FDR_ON_ERROR std::function<void(int, std::string)> onError = [] (int errorCode, std::string errorMessage) {FDR_UNUSED(errorCode); FDR_UNUSED(errorMessage) }
|
||||
@@ -32,8 +38,12 @@ public:
|
||||
sockaddr_in address;
|
||||
|
||||
void Close() {
|
||||
#ifdef WIN32
|
||||
|
||||
#elif __linux__
|
||||
shutdown(this->sock, SHUT_RDWR);
|
||||
close(this->sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string RemoteAddress() const { return IPToString(this->address); };
|
||||
|
@@ -3,11 +3,22 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#elif WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2ipdef.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace Sockets
|
||||
{
|
||||
/// Internet Protocol Version
|
||||
@@ -89,4 +100,4 @@ namespace Sockets
|
||||
freeaddrinfo(res); // free the linked list
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,13 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#ifdef __linux__
|
||||
#include <netdb.h>
|
||||
#elif WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
#include <memory>
|
||||
|
||||
namespace Socket {
|
||||
@@ -20,7 +26,7 @@ namespace Socket {
|
||||
|
||||
class BadUriException : public SocketException {
|
||||
public:
|
||||
BadUriException(char *msg) : SocketException{msg} {}
|
||||
BadUriException(const char *msg) : SocketException{msg} {}
|
||||
};
|
||||
|
||||
class SocketBindingException : public SocketException {
|
||||
|
@@ -6,10 +6,16 @@
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#ifdef __linux__
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#elif WIN32
|
||||
#include <winsock2.h>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#include <Sockets/Exceptions.hpp>
|
||||
|
||||
|
@@ -2,13 +2,19 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#ifdef __linux__
|
||||
#include <netdb.h>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#elif WIN32
|
||||
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
#include <Sockets/Sockets.hpp>
|
||||
|
||||
#include <Sockets/IPAddress.hpp>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "BaseSocket.hpp"
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
|
||||
@@ -9,7 +9,7 @@ class UDPSocket : public BaseSocket
|
||||
{
|
||||
public:
|
||||
std::function<void(std::string, std::string, std::uint16_t)> onMessageReceived;
|
||||
std::function<void(const char*, ssize_t, std::string, std::uint16_t)> onRawMessageReceived;
|
||||
std::function<void(const char*, size_t, std::string, std::uint16_t)> onRawMessageReceived;
|
||||
|
||||
explicit UDPSocket(bool useConnect = false, FDR_ON_ERROR, int socketId = -1): BaseSocket(onError, SocketType::UDP, socketId)
|
||||
{
|
||||
@@ -26,7 +26,7 @@ public:
|
||||
}
|
||||
|
||||
// Send raw bytes to a spesific `host` & `port` with no connection
|
||||
ssize_t SendTo(const char* bytes, size_t byteslength, const char* host, uint16_t port, FDR_ON_ERROR)
|
||||
size_t SendTo(const char* bytes, size_t byteslength, const char* host, uint16_t port, FDR_ON_ERROR)
|
||||
{
|
||||
sockaddr_in hostAddr;
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
hostAddr.sin_port = htons(port);
|
||||
hostAddr.sin_family = AF_INET;
|
||||
|
||||
ssize_t sent_length = sendto(this->sock, bytes, byteslength, 0, (sockaddr*)&hostAddr, sizeof(hostAddr));
|
||||
size_t sent_length = sendto(this->sock, bytes, byteslength, 0, (sockaddr*)&hostAddr, sizeof(hostAddr));
|
||||
if (sent_length == -1)
|
||||
{
|
||||
onError(errno, "Cannot send message to the address.");
|
||||
@@ -66,25 +66,25 @@ public:
|
||||
return sent_length;
|
||||
}
|
||||
// Send raw bytes to a spesific `host` & `port` with no connection
|
||||
ssize_t SendTo(const char* bytes, size_t byteslength, const std::string& host, uint16_t port, FDR_ON_ERROR)
|
||||
size_t SendTo(const char* bytes, size_t byteslength, const std::string& host, uint16_t port, FDR_ON_ERROR)
|
||||
{
|
||||
return this->SendTo(bytes, byteslength, host.c_str(), port, onError);
|
||||
}
|
||||
// Send std::string to a spesific `host` & `port` with no connection
|
||||
ssize_t SendTo(const std::string& message, const char* host, uint16_t port, FDR_ON_ERROR)
|
||||
size_t SendTo(const std::string& message, const char* host, uint16_t port, FDR_ON_ERROR)
|
||||
{
|
||||
return this->SendTo(message.c_str(), message.length(), host, port, onError);
|
||||
}
|
||||
// Send std::string to a spesific `host` & `port` with no connection
|
||||
ssize_t SendTo(const std::string& message, const std::string& host, uint16_t port, FDR_ON_ERROR)
|
||||
size_t SendTo(const std::string& message, const std::string& host, uint16_t port, FDR_ON_ERROR)
|
||||
{
|
||||
return this->SendTo(message.c_str(), message.length(), host.c_str(), port, onError);
|
||||
}
|
||||
|
||||
// Send raw bytes to the `Connect()`ed server.
|
||||
ssize_t Send(const char* bytes, size_t byteslength) { return send(this->sock, bytes, byteslength, 0); }
|
||||
size_t Send(const char* bytes, size_t byteslength) { return send(this->sock, bytes, byteslength, 0); }
|
||||
// Send std::string to the `Connect()`ed server.
|
||||
ssize_t Send(const std::string& message) { return this->Send(message.c_str(), message.length()); }
|
||||
size_t Send(const std::string& message) { return this->Send(message.c_str(), message.length()); }
|
||||
|
||||
// Connect to a server with raw `uint32_t ipv4` and `uint16_t port` values.
|
||||
void Connect(uint32_t ipv4, uint16_t port, FDR_ON_ERROR)
|
||||
@@ -136,7 +136,7 @@ private:
|
||||
static void Receive(UDPSocket* udpSocket)
|
||||
{
|
||||
char tempBuffer[BUFFER_SIZE+1];
|
||||
ssize_t messageLength;
|
||||
size_t messageLength;
|
||||
|
||||
while ((messageLength = recv(udpSocket->sock, tempBuffer, BUFFER_SIZE, 0)) != -1)
|
||||
{
|
||||
@@ -155,7 +155,7 @@ private:
|
||||
socklen_t hostAddrSize = sizeof(hostAddr);
|
||||
|
||||
char tempBuffer[BUFFER_SIZE+1];
|
||||
ssize_t messageLength;
|
||||
size_t messageLength;
|
||||
|
||||
while ((messageLength = recvfrom(udpSocket->sock, tempBuffer, BUFFER_SIZE, 0, (sockaddr* )&hostAddr, &hostAddrSize)) != -1)
|
||||
{
|
||||
|
@@ -2,6 +2,13 @@
|
||||
|
||||
#include <Sockets/IPAddress.hpp>
|
||||
#include <string>
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
|
||||
#elif defined(_WIN32)
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// Game-specific Network code written against GameNetworkingSocket API
|
||||
|
||||
|
@@ -1,7 +1,14 @@
|
||||
#include <Sockets/Endianness.hpp>
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#elif __linux__
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace Socket
|
||||
|
@@ -1,4 +1,9 @@
|
||||
#include "Sockets/IPAddress.hpp"
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#elif defined(_WIN32)
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
#include <Sockets/Exceptions.hpp>
|
||||
#include <Sockets/Commons.hpp>
|
||||
|
||||
|
@@ -1,7 +1,9 @@
|
||||
#include <Sockets/Sockets.hpp>
|
||||
#include <Sockets/Exceptions.hpp>
|
||||
#include <cstring>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
namespace Socket
|
||||
{
|
||||
|
||||
@@ -82,7 +84,10 @@ namespace Socket
|
||||
if (errcode == EBADF) throw error("[close] fd isn't a valid open file descriptor.");
|
||||
if (errcode == EINTR) throw error("[close] The close() call was interrupted by a signal; see signal(7)");
|
||||
if (errcode == EIO) throw error("[close] An I/O error occured.");
|
||||
if (errcode == ENOSPC || errcode == EDQUOT) throw error("[close] NFS Specific error, consult close() manpage");
|
||||
if (errcode == ENOSPC) throw error("[close] NFS Specific error, consult close() manpage");
|
||||
#ifdef __linux__
|
||||
if (errcode == EDQUOT) throw error("[close] NFS Specific error, consult close() manpage");
|
||||
#endif
|
||||
|
||||
throw error(strerror(errcode)); // No other error codes match, but something's still fucked up!
|
||||
}
|
||||
@@ -214,7 +219,7 @@ namespace Socket
|
||||
void CheckErrors_getaddrinfo(int errcode) {
|
||||
using error = SocketException;
|
||||
|
||||
if (errcode == EAI_ADDRFAMILY) throw error("[getaddrinfo] The specified network host does not have any network addresses in the requested address family.");
|
||||
|
||||
if (errcode == EAI_AGAIN) throw error("[getaddrinfo] The name server returned a temporary failure indication. Try again later.");
|
||||
if (errcode == EAI_BADFLAGS) throw error("[getaddrinfo] hints.ai_flags contains invalid flags; or hints.ai_flags included AI_CANONNAME and name was NULL.");
|
||||
if (errcode == EAI_FAIL) throw error("[getaddrinfo] The name server returned a permanent failure indication.");
|
||||
@@ -224,7 +229,10 @@ namespace Socket
|
||||
if (errcode == EAI_NONAME) throw error("[getaddrinfo] The node or service is not known; or both node and service are NULL; or AI_NUMERICSERV was specified in hints.ai_flags and service was not a numeric port-number string.");
|
||||
if (errcode == EAI_SERVICE) throw error("[getaddrinfo] The requested service is not available for the requested socket type. It may be available through another socket type. For example, this error could occur if service was 'shell'.");
|
||||
if (errcode == EAI_SOCKTYPE) throw error("[getaddrinfo] The requested socket type is not supported. This could occur, for example, if hints.ai_socktype and hints.ai_protocol are inconsistent.");
|
||||
#if __linux
|
||||
if (errcode == EAI_ADDRFAMILY) throw error("[getaddrinfo] The specified network host does not have any network addresses in the requested address family.");
|
||||
if (errcode == EAI_SYSTEM) throw error("[getaddrinfo] Other system error; errno is set to indicate the error");
|
||||
#endif
|
||||
}
|
||||
|
||||
void CheckErrors_sendto(int errcode) {
|
||||
@@ -260,4 +268,4 @@ namespace Socket
|
||||
|
||||
throw error(strerror(errcode)); // No other error codes match, but something's still fucked up!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,16 +2,26 @@
|
||||
#include <Sockets/TcpSocket_Old.hpp>
|
||||
#include <Sockets/Sockets.hpp>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <jlog/Logger.hpp>
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#elif defined(WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
namespace Socket {
|
||||
|
||||
@@ -148,9 +158,8 @@ namespace Socket {
|
||||
int status = 0;
|
||||
int total_sent = 0;
|
||||
unsigned int left_to_send = length;
|
||||
while(total_sent < length)
|
||||
{
|
||||
status = ::send(mSock, buff + total_sent, left_to_send, flags);
|
||||
while(total_sent < length) {
|
||||
status = ::send(mSock, reinterpret_cast<const char*>(buff + total_sent), left_to_send, flags);
|
||||
if (status == -1)
|
||||
CheckErrors_send(errno);
|
||||
else {
|
||||
@@ -163,7 +172,7 @@ namespace Socket {
|
||||
bool TcpSocket_Old::Receive(u8* msg, int len, int flags)
|
||||
{
|
||||
int status = 0;
|
||||
if ((status = ::recv(mSock, msg, len, flags)) == -1)
|
||||
if ((status = ::recv(mSock, reinterpret_cast<char*>(msg), len, flags)) == -1)
|
||||
CheckErrors_recv(errno);
|
||||
else if (status == 0)
|
||||
return false;
|
||||
@@ -173,12 +182,19 @@ namespace Socket {
|
||||
|
||||
void TcpSocket_Old::Close()
|
||||
{
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
//::shutdown(mSock, 2);
|
||||
|
||||
if (::close(mSock) == -1)
|
||||
CheckErrors_close(errno);
|
||||
else
|
||||
mClosed = true;
|
||||
#elif defined(_WIN32)
|
||||
if (::closesocket(mSock == -1))
|
||||
CheckErrors_close(errno);
|
||||
else
|
||||
mClosed = true;
|
||||
#endif
|
||||
}
|
||||
// Private:
|
||||
void TcpSocket_Old::setInfo(int port)
|
||||
@@ -227,11 +243,19 @@ namespace Socket {
|
||||
} address;
|
||||
socklen_t addressSize = sizeof(sockaddr_storage);
|
||||
int newSock;
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
if ((newSock = ::accept(mSock, (struct sockaddr *) 0, (unsigned int *) 0)) == -1) {
|
||||
//if ((newSock = ::accept(mSock, (sockaddr*)&address.s, &addressSize)) == -1) {
|
||||
jlog::Debug(strerror(errno));
|
||||
CheckErrors_accept(errno);
|
||||
}
|
||||
#elif defined(_WIN32)
|
||||
if ((newSock = ::accept(mSock, (struct sockaddr *) 0, (int *) 0)) == -1) {
|
||||
//if ((newSock = ::accept(mSock, (sockaddr*)&address.s, &addressSize)) == -1) {
|
||||
jlog::Debug(strerror(errno));
|
||||
CheckErrors_accept(errno);
|
||||
}
|
||||
#endif
|
||||
jlog::Debug("1 client accepted");
|
||||
|
||||
addrinfo info;
|
||||
|
@@ -91,7 +91,7 @@ namespace Socket
|
||||
void UdpSocket_Old::Connect(const std::string& hostname, u16 port)
|
||||
{
|
||||
auto ipaddr = IPAddress::Resolve(hostname, port);
|
||||
this->Connect(ipaddr, port);
|
||||
this->Connect(ipaddr);
|
||||
}
|
||||
void UdpSocket_Old::Connect(const IPAddress& ipaddr)
|
||||
{
|
||||
@@ -160,7 +160,7 @@ namespace Socket
|
||||
|
||||
int UdpSocket_Old::Send(const u8 *data, unsigned int length, int flags)
|
||||
{
|
||||
int ret = ::send(sock, data, length, flags);
|
||||
int ret = ::send(sock,reinterpret_cast<const char*>(data), length, flags);
|
||||
|
||||
if (ret < 0)
|
||||
CheckErrors_send(errno);
|
||||
@@ -169,7 +169,7 @@ namespace Socket
|
||||
|
||||
int UdpSocket_Old::Send(const std::vector<u8>& payload)
|
||||
{
|
||||
this->Send(payload.data(), payload.size());
|
||||
return this->Send(payload.data(), payload.size());
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user