Skip to content

Commit

Permalink
fix: winsocket refactor, exceptions, bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Mar 28, 2024
1 parent 5ee7a4a commit 338e0bc
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 147 deletions.
4 changes: 2 additions & 2 deletions include/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace tpt
DEBUG,
INFO,
WARNING,
ERROR,
ERR,
CRITICAL
};

Expand All @@ -34,7 +34,7 @@ namespace tpt
#define LOG_DEBUG(logger, message) LOG(logger, tpt::LogLevel::DEBUG, message)
#define LOG_INFO(logger, message) LOG(logger, tpt::LogLevel::INFO, message)
#define LOG_WARNING(logger, message) LOG(logger, tpt::LogLevel::WARNING, message)
#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERROR, message)
#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERR, message)
#define LOG_CRITICAL(logger, message) LOG(logger, tpt::LogLevel::CRITICAL, message)
};

Expand Down
1 change: 0 additions & 1 deletion include/teapot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#endif

#ifdef _WIN32
#include <windows.h>
#include "win_socket.hpp"
#endif

Expand Down
2 changes: 2 additions & 0 deletions include/win_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ namespace tpt
unsigned int port;
unsigned int max_connections;
std::string ip_address;
std::string client_ip;
std::vector<std::string> ip_blacklist;
std::vector<SOCKET> client_sockets;
ConsoleLogger logger;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/console_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace tpt
case LogLevel::WARNING:
colorCode = "\033[33m"; // Yellow
break;
case LogLevel::ERROR:
case LogLevel::ERR:
colorCode = "\033[31m"; // Red
break;
case LogLevel::CRITICAL:
Expand Down
2 changes: 1 addition & 1 deletion src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace tpt
return "INFO";
case LogLevel::WARNING:
return "WARNING";
case LogLevel::ERROR:
case LogLevel::ERR:
return "ERROR";
case LogLevel::CRITICAL:
return "CRITICAL";
Expand Down
2 changes: 1 addition & 1 deletion src/teapot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_conne
this->socket = tpt::UnixSocket(this->logger, this->ip_address, this->port, this->max_connections);
#endif
#ifdef _WIN32
this->socket = tpt::WinSocket(this->ip_address, this->port, this->max_connections);
this->socket = tpt::WinSocket(this->logger, this->ip_address, this->port, this->max_connections);
#endif
}

Expand Down
165 changes: 25 additions & 140 deletions src/win_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,31 @@

using namespace tpt;

WinSocket::WinSocket()
{
this->ip_address = "127.0.0.1";
this->port = 8000;
this->max_connections = 10;
this->logger = ConsoleLogger();

if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::printf("Failed. Error Code : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}

std::cout << "Creating socket ..." << std::endl;
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->server_socket == INVALID_SOCKET)
{
std::printf("Could not create socket: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}

this->server_address.sin_family = AF_INET;
this->server_address.sin_port = htons(this->port);
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());

Utils::fillIPBlacklist(this->ip_blacklist);
}

WinSocket::WinSocket(ConsoleLogger logger)
{
this->ip_address = "127.0.0.1";
this->port = 8000;
this->max_connections = 10;
this->logger = logger;

if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::printf("Failed. Error Code : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}

std::cout << "Creating socket ..." << std::endl;
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->server_socket == INVALID_SOCKET)
{
std::printf("Could not create socket: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}

this->server_address.sin_family = AF_INET;
this->server_address.sin_port = htons(this->port);
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());

Utils::fillIPBlacklist(this->ip_blacklist);
}

WinSocket::WinSocket(ConsoleLogger logger, unsigned int port)
{
this->ip_address = "127.0.0.1";
this->port = port;
this->max_connections = 10;
this->logger = logger;

if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::printf("Failed. Error Code : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}
WinSocket::WinSocket() : WinSocket(ConsoleLogger()) {}

std::cout << "Creating socket ..." << std::endl;
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->server_socket == INVALID_SOCKET)
{
std::printf("Could not create socket: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}

this->server_address.sin_family = AF_INET;
this->server_address.sin_port = htons(this->port);
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());

Utils::fillIPBlacklist(this->ip_blacklist);
}

WinSocket::WinSocket(ConsoleLogger logger, std::string ip_address, unsigned int port)
{
this->ip_address = ip_address;
this->port = port;
this->max_connections = 10;
this->logger = logger;

if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::printf("Failed. Error Code : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}

std::cout << "Creating socket ..." << std::endl;
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->server_socket == INVALID_SOCKET)
{
std::printf("Could not create socket: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}
WinSocket::WinSocket(ConsoleLogger logger) : WinSocket(logger, "127.0.0.1", 8000, 10) {}

this->server_address.sin_family = AF_INET;
this->server_address.sin_port = htons(this->port);
this->server_address.sin_addr.s_addr = inet_addr(this->ip_address.c_str());
WinSocket::WinSocket(ConsoleLogger logger, unsigned int port) : WinSocket(logger, "127.0.0.1", port, 10) {}

Utils::fillIPBlacklist(this->ip_blacklist);
}
WinSocket::WinSocket(ConsoleLogger logger, std::string ip_address, unsigned int port) : WinSocket(logger, ip_address, port, 10) {}

WinSocket::WinSocket(ConsoleLogger logger, std::string ip_address, unsigned int port, unsigned int max_connections)
{
this->ip_address = ip_address;
this->port = port;
this->max_connections = 10;
this->max_connections = max_connections;
this->logger = logger;

if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::printf("Failed. Error Code : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
throw SocketCreationException("WSAStartup error", WSAGetLastError());
}

std::cout << "Creating socket ..." << std::endl;
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->server_socket == INVALID_SOCKET)
{
std::printf("Could not create socket: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
throw SocketCreationException("could not create socket", WSAGetLastError());
}

this->server_address.sin_family = AF_INET;
Expand All @@ -158,11 +42,8 @@ void WinSocket::bindSocket()
std::cout << "Binding socket ..." << std::endl;
if ((bind(this->server_socket, (struct sockaddr *)&this->server_address, sizeof(server_address))) == SOCKET_ERROR)
{
std::printf("Binding failed: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
throw SocketBindingException("binding failed", WSAGetLastError());
}
this->setSocketTimeout(this->server_socket, 5);
std::cout << "Binding done!" << std::endl;
std::cout << "Listening to connections ..." << std::endl;
}
Expand All @@ -171,22 +52,20 @@ void WinSocket::listenToConnections()
{
if ((listen(this->server_socket, this->max_connections)) == SOCKET_ERROR)
{
std::printf("Listening failed: %d\n", WSAGetLastError());
closesocket(this->server_socket);
WSACleanup();
exit(EXIT_FAILURE);
throw SocketListenException("listening failed", WSAGetLastError());
}
}

void WinSocket::acceptConnection(SOCKET &client_socket, void *client_address)
{
int client_addr_size = sizeof(sockaddr_in);
client_socket = accept(this->server_socket, static_cast<sockaddr *>(client_address), &client_addr_size);
struct sockaddr_storage client_addr_storage;
int client_addr_size = sizeof(client_addr_storage);

//this->setSocketTimeout(this->server_socket, 5);
client_socket = accept(this->server_socket, (sockaddr *)&client_addr_storage, &client_addr_size);
if (client_socket == INVALID_SOCKET)
{
std::printf("Error accepting connections: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
throw SocketAcceptException("error accepting connections", WSAGetLastError());
}

// Assuming client_address is meant to store the result
Expand Down Expand Up @@ -225,16 +104,17 @@ ssize_t WinSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int
ssize_t data = recv(client_socket, buffer, buffer_size, 0);
if (data < 0)
{
std::printf("Receive error\n");
WSACleanup();
exit(EXIT_FAILURE);
throw SocketReceiveException("recv error", WSAGetLastError());
}
return data;
}

void WinSocket::sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags)
{
send(client_socket, (char *)buffer, buffer_size, flags);
if (send(client_socket, (char *)buffer, buffer_size, flags) == -1)
{
throw SocketSendException();
}
}

void WinSocket::closeSocket()
Expand Down Expand Up @@ -267,4 +147,9 @@ void WinSocket::setSocketTimeout(SOCKET sock, int timeoutSec)

WinSocket::~WinSocket() {}

std::string WinSocket::getClientIp()
{
return this->client_ip;
}

#endif
6 changes: 5 additions & 1 deletion teapot.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\console_logger.cpp" />
<ClCompile Include="src\cors_middleware.cpp" />
<ClCompile Include="src\http_status.cpp" />
<ClCompile Include="src\logger.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\request.cpp" />
<ClCompile Include="src\response.cpp" />
Expand All @@ -118,9 +120,11 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\base_exceptions.hpp" />
<ClInclude Include="include\console_logger.hpp" />
<ClInclude Include="include\context.hpp" />
<ClInclude Include="include\cors_middleware.hpp" />
<ClInclude Include="include\http_status.hpp" />
<ClInclude Include="include\logger.hpp" />
<ClInclude Include="include\middleware.hpp" />
<ClInclude Include="include\request.hpp" />
<ClInclude Include="include\response.hpp" />
Expand All @@ -142,4 +146,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
12 changes: 12 additions & 0 deletions teapot.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<ClCompile Include="src\win_socket.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\console_logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\base_exceptions.hpp">
Expand Down Expand Up @@ -92,6 +98,12 @@
<ClInclude Include="include\win_socket.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\logger.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\console_logger.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="static\201.html" />
Expand Down

0 comments on commit 338e0bc

Please sign in to comment.