feat: Ability to remove ports from active port list
This commit is contained in:
parent
b67b62a31a
commit
83a5dcaec7
85
main.c
85
main.c
|
@ -3,12 +3,30 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
bool ports[999];
|
bool ports[999];
|
||||||
|
|
||||||
|
int start_server(int* socket_fd) {
|
||||||
|
struct sockaddr_in server_addr;
|
||||||
|
server_addr.sin_family = AF_INET;
|
||||||
|
server_addr.sin_port = htons(1337);
|
||||||
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
printf("Starting Server\n");
|
||||||
|
*socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
int bound = bind(*socket_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||||||
|
if(bound < 0) {
|
||||||
|
printf("Bind Error: %s (%d)\n", strerror(errno), errno);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
listen(*socket_fd, 5);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void start_worker(int port) {
|
void start_worker(int port) {
|
||||||
char portStr[6];
|
char portStr[6];
|
||||||
sprintf(portStr, "%d", port);
|
sprintf(portStr, "%d", port);
|
||||||
|
@ -19,41 +37,50 @@ void start_worker(int port) {
|
||||||
posix_spawn(&pid, "./worker", NULL, NULL, argv, environ);
|
posix_spawn(&pid, "./worker", NULL, NULL, argv, environ);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int assign_workers(int* socket_fd) {
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(1337);
|
|
||||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
|
|
||||||
printf("Starting Server\n");
|
|
||||||
int socket_fd;
|
|
||||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
int bound = bind(socket_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
|
||||||
if(bound < 0) {
|
|
||||||
printf("Bind Error: %s (%d)\n", strerror(errno), errno);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
listen(socket_fd, 5);
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
struct sockaddr_in client_addr;
|
struct sockaddr_in client_addr;
|
||||||
int client_size = sizeof(client_addr);
|
int client_size = sizeof(client_addr);
|
||||||
int client_sock = accept(socket_fd, (struct sockaddr*)&client_addr, &client_size);
|
int client_sock = accept(*socket_fd, (struct sockaddr*)&client_addr, &client_size);
|
||||||
|
|
||||||
for(int i = 0; i < sizeof(ports); i++) {
|
char buffer[5];
|
||||||
if(!ports[i]) {
|
int bytes_received;
|
||||||
int port = 11000+i;
|
bytes_received = recv(client_sock, buffer, sizeof(buffer), 0);
|
||||||
printf("Assigning port %i to worker\n", port);
|
if(bytes_received < 0 || bytes_received == 0) {
|
||||||
char buff[4];
|
continue;
|
||||||
memcpy(buff, &port, sizeof(int));
|
}
|
||||||
write(client_sock, buff, sizeof(buff));
|
|
||||||
start_worker(port);
|
if(buffer[0] == 0x0) { // Get Port
|
||||||
ports[i] = true;
|
for(int i = 0; i < sizeof(ports); i++) {
|
||||||
shutdown(client_sock, SHUT_RDWR);
|
if(!ports[i]) {
|
||||||
close(client_sock);
|
int port = 11000+i;
|
||||||
break;
|
printf("Assigning port %i to worker\n", port);
|
||||||
|
char buff[sizeof(int)];
|
||||||
|
memcpy(buff, &port, sizeof(int));
|
||||||
|
write(client_sock, buff, sizeof(buff));
|
||||||
|
start_worker(port);
|
||||||
|
ports[i] = true;
|
||||||
|
shutdown(client_sock, SHUT_RDWR);
|
||||||
|
close(client_sock);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else if(buffer[0] == 0x1) { // Remove Worker
|
||||||
|
u_int32_t port;
|
||||||
|
memcpy(&port, buffer+1, sizeof(u_int32_t));
|
||||||
|
port = ntohl(port);
|
||||||
|
printf("Unassigning port %i\n", port);
|
||||||
|
ports[11000-port] = false;
|
||||||
|
shutdown(client_sock, SHUT_RDWR);
|
||||||
|
close(client_sock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int* socket_fd;
|
||||||
|
start_server(socket_fd);
|
||||||
|
assign_workers(socket_fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue