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

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
Scene scene(args.getSceneName());
if (scene.fail())
if (scene.error())
return (1);
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0, args);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
_error = 0;
_serverSocket = 0;
_serverFd = 0;
_pollfds = 0;
_currentJob = 0;
_sceneName = args.getSceneName();
_renderer = renderer;
@ -39,6 +42,21 @@ Clusterizer::~Clusterizer(void)
{
if(_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)
@ -58,7 +76,7 @@ bool Clusterizer::getError(void)
bool Clusterizer::isServer(void)
{
return(_isServer);
return(_isServer);
}
bool Clusterizer::hasJobs(void)

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
_serverFd = 0;
}
// (void)write(_serverFd, (uint8_t []){RDY}, 1);
}
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;
scene.changeScene(_sceneName, buffers);
win.setFrameCount(0);
if(scene.fail())
if(scene.error())
throw std::runtime_error("map change failed");
(void)write(_serverFd, (uint8_t []){RDY}, 1);
}
@ -155,6 +154,10 @@ void Clusterizer::clientReceive(Scene &scene, std::vector<Buffer *> &buffers, Wi
{
if(!ret)
{
if(_currentJob)
delete _currentJob;
_currentJob = 0;
close(_serverFd);
_serverFd = 0;
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)
{
_srvReady = 0;
std::vector<float> imageFloat(WIDTH * HEIGHT * 4);
std::vector<uint8_t> buffer;
_srvReady = 0;
(void)write(_serverFd, (uint8_t []){IMG_SEND_RQ}, 1);
while(!_srvReady)
{

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
std::cerr << "server initialization error : " << e.what() << std::endl;
std::cerr << "\033[31;1mserver initialization error : " << e.what() << "\033[0m" << std::endl;
_error = 1;
}
}
@ -38,12 +38,25 @@ void Clusterizer::initServerSocket(int port)
_serverSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (_serverSocket < 0)
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_addr.s_addr = INADDR_ANY;
s_addr.sin_port = htons(port);
if (bind(_serverSocket, (struct sockaddr *)&s_addr, \
sizeof(struct sockaddr_in)) < 0)
{
std::perror("bind :");
throw std::runtime_error("can't bind socket");
}
if (::listen(_serverSocket, 50) < 0)
throw std::runtime_error("can't listen on socket");
if(_serverSocket == -1)

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
_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);
}
@ -48,7 +48,7 @@ void Scene::init(std::string &name)
if (!file.is_open())
{
std::cerr << "Can't open scene file" << std::endl;
_fail = 1;
_error = 1;
return ;
}
@ -60,7 +60,7 @@ void Scene::init(std::string &name)
{
file.close();
std::cerr << line << std::endl;
_fail = 1;
_error = 1;
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()

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
glfwTerminate();
delete _renderer;
delete _clusterizer;
}
@ -172,7 +174,7 @@ void Window::pollEvents()
bool Window::shouldClose()
{
return glfwWindowShouldClose(_window) || _renderer->shouldClose();
return glfwWindowShouldClose(_window) || _renderer->shouldClose() || _clusterizer->getError();
}
bool Window::isRendering()