Commit c4ff9549 authored by PierreEngelstein's avatar PierreEngelstein
Browse files

commit

parent 557aa229
......@@ -14,8 +14,9 @@ LINUX_BUILD = build/linux/server
LINUX_INCLUDE = -Isrc/
MINGW = x86_64-w64-mingw32-g++
WINDOWS_FLAGS = -std=c++17 -lws2_32 -pthread
WINDOWS_FLAGS = -std=c++17 -lws2_32 -static-libgcc -static-libstdc++ -pthread
WINDOWS_BUILD = build/windows/server.exe
WINDOWS_INCLUDE = -Isrc/
build_linux: $(obj_linux)
mkdir -p build/linux
......@@ -35,7 +36,7 @@ build_windows: $(obj_windows)
rm -f $(obj_windows)
%.ow:%.cpp
$(MINGW) -Wall -c $^ -o $@
$(MINGW) -Wall -c $^ -o $@ $(WINDOWS_INCLUDE)
run_windows:build_windows
wine build/windows/server.exe
......
#include <iostream>
#include <string.h>
#include "cross.hpp"
void Error()
{
#ifdef __WIN32
fprintf(stderr, "WSA Error Code : %d\n", WSAGetLastError());
#elif defined __linux__
fprintf(stderr, "%s\n", strerror(errno));
#endif
}
\ No newline at end of file
#ifdef __WIN32
#include <winsock2.h>
#endif
void Error();
\ No newline at end of file
......@@ -6,6 +6,8 @@
#include <memory>
#include <signal.h>
#include "cross.hpp"
threads::DelayThread *delay;
threads::ThreadPool *pool;
......@@ -20,9 +22,40 @@ void handle_close(int signal_num)
running = false;
}
#ifdef __WIN32
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
case CTRL_C_EVENT:
handle_close(0);
return TRUE;
default:
return FALSE;
}
}
#endif
int main()
{
#ifdef __WIN32
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err < 0)
{
fprintf(stderr, "Failed to start WSA !\n");
Error();
exit(EXIT_FAILURE);
}
/* Register the ctrl - c to the console host */
if(SetConsoleCtrlHandler(CtrlHandler, TRUE))
{
fprintf(stdout, "Registered control handler !\n");
}
signal(SIGINT, handle_close);
#elif defined __linux__
signal(SIGINT, handle_close);
#endif
server_socket = threads::SocketThread::CreateServerSocket(9999, "tcp", 10);
......
......@@ -7,11 +7,7 @@ namespace threads
DelayThread::~DelayThread()
{
for(auto iter = cooldown_list.begin(); iter != cooldown_list.end();)
{
iter = cooldown_list.erase(iter);
}
// DelayThread::Detach();
}
std::string DelayThread::ToString()
......@@ -62,7 +58,6 @@ namespace threads
void DelayThread::AddIP(std::string ip)
{
printf("Coucou !\n");
ip_cooldown cooldown(ip, 10);
cooldown_list.push_back(cooldown);
printf("Added ip %s in cooldown !\n", ip.c_str());
......
......@@ -2,21 +2,14 @@
namespace threads
{
void EmptyRun(void)
{
fprintf(stdout, "Coucou :)\n");
}
IThread::IThread(int id)
{
// printf("Creating thread of id %d\n", id);
IThread::_id = id;
IThread::_running = false;
}
IThread::~IThread()
{
fprintf(stdout, "Trying to end thread !\n");
IThread::Detach();
}
......@@ -29,8 +22,6 @@ namespace threads
fprintf(stderr, "%s\n", strerror(errno));
return 1;
}
// fprintf(stdout, "Created thread %lu\n", IThread::_thread.native_handle());
// _thread.detach();
IThread::_running = true;
return 0;
}
......
......@@ -19,19 +19,4 @@ namespace threads
else
return -1;
}
void PrintData(Message message)
{
std::string str = "Message :";
str.append(message.Message);
for(int i = 0; i < DATLEN; i++)
{
if(message.data[i].id >= 0 && message.data[i].id != 65535)
{
str.append(message.data[i].ToString());
str.append("\n");
}
}
printf("%s\n", str.c_str());
}
}
\ No newline at end of file
......@@ -47,20 +47,19 @@ namespace threads
{
std::string str = "Message :";
str.append(Message);
for(int i = 0; i < DATLEN; i++)
{
if(data[i].id >= 0 && data[i].id != 65535)
{
str.append(data[i].ToString());
str.append("\n");
}
}
// for(int i = 0; i < DATLEN; i++)
// {
// if(data[i].id >= 0 && data[i].id != 65535)
// {
// str.append(data[i].ToString());
// str.append("\n");
// }
// }
return str;
}
};
int TreatData(std::vector<char> buffer_in, Message *message);
void PrintData(Message message);
}
#endif
\ No newline at end of file
......@@ -40,7 +40,7 @@ namespace threads
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(socket_address.sin_addr), ip, INET_ADDRSTRLEN);
#elif defined __WIN32
char *ip = inet_ntoa(address.sin_addr);
char *ip = inet_ntoa(socket_address.sin_addr);
#endif
Client::ip = std::string(ip);
Client::port = (int16_t) ntohs(socket_address.sin_port);
......@@ -88,7 +88,6 @@ namespace threads
fprintf(stderr, "%s\n", strerror(errno));
return 1;
}
fprintf(stdout, "Socket closed !\n");
return 0;
}
......
......@@ -11,6 +11,11 @@
#include <arpa/inet.h>
#elif defined __WIN32
#include <winsock2.h>
#include <windows.h>
#include <stdint.h>
#include <windef.h>
#include <ws2tcpip.h>
typedef int __socklen_t;
#endif
#define ACCEPT_MAX 5
......
......@@ -2,6 +2,7 @@
#include "../ThreadPool.hpp"
#include "Client.hpp"
#include "../Message/MessageParser.hpp"
#include "../../cross.hpp"
namespace threads
{
......@@ -44,7 +45,7 @@ namespace threads
/* Set protocol name */
p_protocol = getprotobyname(protocol.c_str());
if(!p_protocol)
if(p_protocol == NULL)
{
fprintf(stderr, "%s protocol error !\n", protocol.c_str());
return -1;
......@@ -156,15 +157,14 @@ namespace threads
recv_result = recv(client.Socket(), &(buffer_in[0]), 1450, 0);
/* Treat the data of the client */
// printf("%s\n", buffer_in.data());
Message message;
if(!TreatData(buffer_in, &message))
{
fprintf(stdout, "%s\n", message.ToString().c_str());
// PrintData(message);
}else
{
not_valid++;
}
total++;
if(recv_result == 0)
......@@ -194,9 +194,6 @@ namespace threads
if(SocketThread::_inPool)
_pool->RemoveIP(SocketThread::client.IP());
// Client::nb_client_socket--;
// printf("Pool = %s", _pool->ToString().c_str());
}
void SocketThread::RegisterPool(ThreadPool *pool)
......
......@@ -29,20 +29,19 @@ namespace threads
void RegisterPool(ThreadPool *pool);
void RegisterDelay(DelayThread *delay);
private:
// char buffer_in[1450];
// char buffer_out[1450];
std::vector<char> buffer_in;
std::vector<char> buffer_out;
// Client client;
Client client;
bool _hasClient;
ThreadPool *_pool;
bool _inPool;
DelayThread *_delay;
bool _inDelay;
void RemoveClient();
// std::shared_ptr<Client> client;
// std::shared_ptr<ThreadPool> _pool;
};
}
......
......@@ -54,7 +54,6 @@ namespace threads
int cooldown = 0;
if((cooldown = delay->IsOnCooldown(thread->IP())) != -1)
{
fprintf(stderr, "IP %s is still in cooldown mode, please wait %d seconds before connecting !\n", thread->IP().c_str(), cooldown);
Client::nb_client_socket--;
thread->End();
thread->Close();
......@@ -69,11 +68,6 @@ namespace threads
std::string str = "Thread pool containing ";
str.append(std::to_string(ThreadPool::pool.size()));
str.append(" elements");
// for(auto iter = ThreadPool::pool.begin(); iter < ThreadPool::pool.end(); iter++)
// {
// str.append(iter->get()->ToString());
// }
// str.append("\n");
return str;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment