summaryrefslogtreecommitdiff
path: root/src/ClientConnection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ClientConnection.cpp')
-rw-r--r--src/ClientConnection.cpp23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/ClientConnection.cpp b/src/ClientConnection.cpp
index c434212..e8e6692 100644
--- a/src/ClientConnection.cpp
+++ b/src/ClientConnection.cpp
@@ -3,7 +3,13 @@
#include <cstring>
#include <iostream>
#include <sstream>
-#include <unistd.h>
+
+#ifdef _WIN32
+ #include <winsock2.h>
+ #pragma comment(lib, "ws2_32.lib")
+#else
+ #include <unistd.h>
+#endif
ClientConnection::ClientConnection(int socket)
: m_socket_fd(socket)
@@ -19,7 +25,12 @@ HttpRequest ClientConnection::read_request() const
int n = 0;
memset(buffer, 0, BUFFER_SIZE);
- while ((n = read(m_socket_fd, buffer, BUFFER_SIZE - 1)) > 0) {
+#ifdef _WIN32
+ n = recv(m_socket_fd, buffer, BUFFER_SIZE - 1, 0);
+#else
+ n = read(m_socket_fd, buffer, BUFFER_SIZE - 1);
+#endif
+ while (n > 0) {
if (buffer[n - 1] == '\n')
break;
@@ -35,7 +46,11 @@ bool ClientConnection::send(const HttpResponse& response) const
return false;
std::string result = response.to_string();
+#ifdef _WIN32
+ ::send(m_socket_fd, result.c_str(), result.length(), 0);
+#else
write(m_socket_fd, result.c_str(), result.length());
+#endif
return true;
}
@@ -43,5 +58,9 @@ bool ClientConnection::send(const HttpResponse& response) const
void ClientConnection::close_connection()
{
m_is_open = false;
+#ifdef _WIN32
+ closesocket(m_socket_fd);
+#else
close(m_socket_fd);
+#endif
}