handle some special cases (disconnect during some moments) and fix "bind adress already in use" error

This commit is contained in:
2025-03-18 14:11:37 +01:00
parent 0103c22882
commit 8f60baded0
9 changed files with 62 additions and 24 deletions

View File

@ -125,6 +125,7 @@ else
@$(RM) $(OBJS_DIR) @$(RM) $(OBJS_DIR)
endif endif
re: fclean $(NAME) re: fclean
$(MAKE) all -j$(shell nproc)
.PHONY: all clean fclean re windows linux .PHONY: all clean fclean re windows linux

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 18:25:18 by tomoron #+# #+# */ /* Created: 2025/02/20 18:25:18 by tomoron #+# #+# */
/* Updated: 2025/03/17 18:05:15 by tomoron ### ########.fr */ /* Updated: 2025/03/18 12:56:01 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -75,7 +75,6 @@ class Clusterizer
std::string _sceneName; std::string _sceneName;
Renderer *_renderer; Renderer *_renderer;
std::vector<t_job *> _jobs[3];
void imguiJobStat(void); void imguiJobStat(void);
void imguiClients(void); void imguiClients(void);
@ -124,4 +123,6 @@ class Clusterizer
struct pollfd *_pollfds; struct pollfd *_pollfds;
std::map<int, t_client> _clients; std::map<int, t_client> _clients;
size_t _curFrame; size_t _curFrame;
std::vector<t_job *> _jobs[3];
}; };

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */ /* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */
/* Updated: 2025/03/17 15:26:14 by tomoron ### ########.fr */ /* Updated: 2025/03/18 13:37:23 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -139,7 +139,7 @@ class Scene
Camera *getCamera(void) const; Camera *getCamera(void) const;
GPUMaterial getMaterial(int material_index); GPUMaterial getMaterial(int material_index);
bool fail(void) const; bool error(void) const;
void changeScene(std::string &name, std::vector<Buffer *> &buffers); void changeScene(std::string &name, std::vector<Buffer *> &buffers);
@ -148,7 +148,7 @@ class Scene
private: private:
void init(std::string &name); void init(std::string &name);
bool _fail; bool _error;
std::vector<GPUBvhData> _gpu_bvh_data; std::vector<GPUBvhData> _gpu_bvh_data;
std::vector<GPUBvh> _gpu_bvh; std::vector<GPUBvh> _gpu_bvh;

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
/* Updated: 2025/03/17 14:54:43 by tomoron ### ########.fr */ /* Updated: 2025/03/18 13:36:35 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,7 +28,7 @@ int main(int argc, char **argv)
return (1); return (1);
Scene scene(args.getSceneName()); Scene scene(args.getSceneName());
if (scene.fail()) if (scene.error())
return (1); return (1);
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0, args); Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0, args);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 18:24:39 by tomoron #+# #+# */ /* Created: 2025/02/20 18:24:39 by tomoron #+# #+# */
/* Updated: 2025/03/17 15:16:43 by tomoron ### ########.fr */ /* Updated: 2025/03/18 14:00:59 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,9 @@ Clusterizer::Clusterizer(Arguments &args, Renderer *renderer)
_isServer = 0; _isServer = 0;
_error = 0; _error = 0;
_serverSocket = 0; _serverSocket = 0;
_serverFd = 0;
_pollfds = 0;
_currentJob = 0;
_sceneName = args.getSceneName(); _sceneName = args.getSceneName();
_renderer = renderer; _renderer = renderer;
@ -39,6 +42,21 @@ Clusterizer::~Clusterizer(void)
{ {
if(_serverSocket) if(_serverSocket)
close(_serverSocket); close(_serverSocket);
if(_serverFd)
close(_serverFd);
if(_pollfds)
delete[] _pollfds;
abortJobs();
for(auto it = _clients.begin(); it != _clients.end(); it++)
{
std::cout << "closing fd " << it->first << std::endl;
close(it->first);
}
if(_clients.size())
updateServer();
if(_currentJob)
delete _currentJob;
} }
void Clusterizer::update(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram, std::vector<Buffer *> &buffers) void Clusterizer::update(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram, std::vector<Buffer *> &buffers)
@ -58,7 +76,7 @@ bool Clusterizer::getError(void)
bool Clusterizer::isServer(void) bool Clusterizer::isServer(void)
{ {
return(_isServer); return(_isServer);
} }
bool Clusterizer::hasJobs(void) bool Clusterizer::hasJobs(void)

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */ /* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */
/* Updated: 2025/03/17 18:05:26 by tomoron ### ########.fr */ /* Updated: 2025/03/18 13:44:57 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -58,7 +58,6 @@ void Clusterizer::openClientConnection(const char *ip, int port)
close(_serverFd); close(_serverFd);
_serverFd = 0; _serverFd = 0;
} }
// (void)write(_serverFd, (uint8_t []){RDY}, 1);
} }
void Clusterizer::clientGetJob(void) void Clusterizer::clientGetJob(void)
@ -97,7 +96,7 @@ void Clusterizer::changeMap(Scene &scene, std::vector<Buffer *> &buffers, Window
std::cout << "changing scene to name :" << _sceneName << std::endl; std::cout << "changing scene to name :" << _sceneName << std::endl;
scene.changeScene(_sceneName, buffers); scene.changeScene(_sceneName, buffers);
win.setFrameCount(0); win.setFrameCount(0);
if(scene.fail()) if(scene.error())
throw std::runtime_error("map change failed"); throw std::runtime_error("map change failed");
(void)write(_serverFd, (uint8_t []){RDY}, 1); (void)write(_serverFd, (uint8_t []){RDY}, 1);
} }
@ -155,6 +154,10 @@ void Clusterizer::clientReceive(Scene &scene, std::vector<Buffer *> &buffers, Wi
{ {
if(!ret) if(!ret)
{ {
if(_currentJob)
delete _currentJob;
_currentJob = 0;
close(_serverFd); close(_serverFd);
_serverFd = 0; _serverFd = 0;
return ; return ;
@ -198,10 +201,10 @@ std::vector<uint8_t> Clusterizer::rgb32fToRgb24i(std::vector<float> &imageFloat)
void Clusterizer::sendImageToServer(Scene &scene, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram, std::vector<Buffer *> &buffers, Window &win) void Clusterizer::sendImageToServer(Scene &scene, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram, std::vector<Buffer *> &buffers, Window &win)
{ {
_srvReady = 0;
std::vector<float> imageFloat(WIDTH * HEIGHT * 4); std::vector<float> imageFloat(WIDTH * HEIGHT * 4);
std::vector<uint8_t> buffer; std::vector<uint8_t> buffer;
_srvReady = 0;
(void)write(_serverFd, (uint8_t []){IMG_SEND_RQ}, 1); (void)write(_serverFd, (uint8_t []){IMG_SEND_RQ}, 1);
while(!_srvReady) while(!_srvReady)
{ {

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */ /* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */
/* Updated: 2025/03/16 17:39:06 by tomoron ### ########.fr */ /* Updated: 2025/03/18 14:03:32 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ void Clusterizer::initServer(std::string port)
} }
catch(std::exception &e) catch(std::exception &e)
{ {
std::cerr << "server initialization error : " << e.what() << std::endl; std::cerr << "\033[31;1mserver initialization error : " << e.what() << "\033[0m" << std::endl;
_error = 1; _error = 1;
} }
} }
@ -38,12 +38,25 @@ void Clusterizer::initServerSocket(int port)
_serverSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); _serverSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (_serverSocket < 0) if (_serverSocket < 0)
throw std::runtime_error("can't create socket"); throw std::runtime_error("can't create socket");
int opt = 1;
if (setsockopt(_serverSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
{
perror("setsockopt");
throw std::runtime_error("can't set socket options");
}
s_addr.sin_family = AF_INET; s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = INADDR_ANY; s_addr.sin_addr.s_addr = INADDR_ANY;
s_addr.sin_port = htons(port); s_addr.sin_port = htons(port);
if (bind(_serverSocket, (struct sockaddr *)&s_addr, \ if (bind(_serverSocket, (struct sockaddr *)&s_addr, \
sizeof(struct sockaddr_in)) < 0) sizeof(struct sockaddr_in)) < 0)
{
std::perror("bind :");
throw std::runtime_error("can't bind socket"); throw std::runtime_error("can't bind socket");
}
if (::listen(_serverSocket, 50) < 0) if (::listen(_serverSocket, 50) < 0)
throw std::runtime_error("can't listen on socket"); throw std::runtime_error("can't listen on socket");
if(_serverSocket == -1) if(_serverSocket == -1)

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */ /* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
/* Updated: 2025/03/17 15:28:10 by tomoron ### ########.fr */ /* Updated: 2025/03/18 13:37:16 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@
Scene::Scene(std::string &name) Scene::Scene(std::string &name)
{ {
_camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f); _camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f);
_fail = 0; _error = 0;
init(name); init(name);
} }
@ -48,7 +48,7 @@ void Scene::init(std::string &name)
if (!file.is_open()) if (!file.is_open())
{ {
std::cerr << "Can't open scene file" << std::endl; std::cerr << "Can't open scene file" << std::endl;
_fail = 1; _error = 1;
return ; return ;
} }
@ -60,7 +60,7 @@ void Scene::init(std::string &name)
{ {
file.close(); file.close();
std::cerr << line << std::endl; std::cerr << line << std::endl;
_fail = 1; _error = 1;
return ; return ;
} }
} }
@ -322,9 +322,9 @@ void Scene::updateLightAndObjects(int mat_id)
} }
} }
bool Scene::fail(void) const bool Scene::error(void) const
{ {
return(_fail); return(_error);
} }
std::set<int> Scene::getGPULights() std::set<int> Scene::getGPULights()

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */
/* Updated: 2025/03/17 15:16:19 by tomoron ### ########.fr */ /* Updated: 2025/03/18 13:34:26 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -65,6 +65,8 @@ Window::Window(Scene *scene, int width, int height, const char *title, int sleep
Window::~Window(void) Window::~Window(void)
{ {
glfwTerminate(); glfwTerminate();
delete _renderer;
delete _clusterizer;
} }
@ -172,7 +174,7 @@ void Window::pollEvents()
bool Window::shouldClose() bool Window::shouldClose()
{ {
return glfwWindowShouldClose(_window) || _renderer->shouldClose(); return glfwWindowShouldClose(_window) || _renderer->shouldClose() || _clusterizer->getError();
} }
bool Window::isRendering() bool Window::isRendering()