Commit 86bb5003 by Michael Pastushkov

fix

parent 97e431d6
...@@ -248,14 +248,25 @@ int wait_connection(void) { ...@@ -248,14 +248,25 @@ int wait_connection(void) {
client_addr_size = sizeof(struct sockaddr_in); client_addr_size = sizeof(struct sockaddr_in);
if (options.proto == SOCK_DGRAM) { if (options.proto == SOCK_DGRAM) {
fd_set io; char buf[BUFFER_SIZE];
FD_ZERO(&io); int bytes = recvfrom(rc.client_socket, buf, sizeof(buf), MSG_PEEK, (struct sockaddr *)&rc.client_addr, &client_addr_size);
FD_SET(rc.client_socket, &io); if (bytes < 0) {
if (select(rc.client_socket+1, &io, NULL, NULL, NULL) < 0) {
if (errno != EINTR) if (errno != EINTR)
perror("wait_connection: select()"); perror("wait_connection: accept()");
return 1; return 1;
} }
// if (options.fork && options.proto == SOCK_DGRAM && !is_child) {
// if (udp_fork(client_addr) == 1)
// continue;
// }
// fd_set io;
// FD_ZERO(&io);
// FD_SET(rc.client_socket, &io);
// if (select(rc.client_socket+1, &io, NULL, NULL, NULL) < 0) {
// if (errno != EINTR)
// perror("wait_connection: select()");
// return 1;
// }
} else { } else {
rc.client_socket = accept(rc.server_socket, (struct sockaddr *) &rc.client_addr, &client_addr_size); rc.client_socket = accept(rc.server_socket, (struct sockaddr *) &rc.client_addr, &client_addr_size);
if (rc.client_socket < 0) { if (rc.client_socket < 0) {
...@@ -272,7 +283,7 @@ int wait_connection(void) { ...@@ -272,7 +283,7 @@ int wait_connection(void) {
return 1; return 1;
} }
if (options.log && options.proto == SOCK_STREAM) if (options.log)
printf("%s request from %s\n", get_current_timestamp(), inet_ntoa(rc.client_addr.sin_addr)); printf("%s request from %s\n", get_current_timestamp(), inet_ntoa(rc.client_addr.sin_addr));
return 0; return 0;
...@@ -335,30 +346,6 @@ printf("client %s found %d ...\n", inet_ntoa(client_addr.sin_addr), i); ...@@ -335,30 +346,6 @@ printf("client %s found %d ...\n", inet_ntoa(client_addr.sin_addr), i);
return -1; return -1;
} }
int is_child = 0;
int udp_fork(struct sockaddr_in client_addr) {
pid_t child_pid;
int client_index = find_udp_client(client_addr);
if (client_index == -1) {
if ((child_pid = fork()) == 0) {
printf("new udp request from %s, forking %d ...\n", inet_ntoa(client_addr.sin_addr), getpid());
is_child = 1;
return 0;
}
add_udp_client(client_addr, child_pid);
// while (waitpid(-1, NULL, WNOHANG) > 0)
// ; /* Reap any zombie processes (clients that exited) */
} else {
printf("existing udp client %s, hanled by %d ...\n", inet_ntoa(client_addr.sin_addr), clients[client_index].pid);
}
return 1;
}
int use(void) int use(void)
{ {
fd_set io; fd_set io;
...@@ -394,18 +381,11 @@ int use(void) ...@@ -394,18 +381,11 @@ int use(void)
close(rc.remote_socket); close(rc.remote_socket);
return 1; return 1;
} }
if (count_recv == 0) { if (count_recv == 0) {
close(rc.client_socket); close(rc.client_socket);
close(rc.remote_socket); close(rc.remote_socket);
return 0; return 0;
} }
if (options.fork && options.proto == SOCK_DGRAM && !is_child) {
if (udp_fork(client_addr) == 1)
continue;
}
(options.mode == MODE_SERVER) ? (options.mode == MODE_SERVER) ?
decode(buffer, count_recv) : decode(buffer, count_recv) :
encode(buffer, count_recv); encode(buffer, count_recv);
...@@ -457,19 +437,46 @@ void run(void) { ...@@ -457,19 +437,46 @@ void run(void) {
use(); use();
} }
void serve(void) { int fork_udp() {
#ifdef __MINGW32__ pid_t child_pid;
run(); int client_index = find_udp_client(rc.client_addr);
#else if (client_index == -1) {
if (options.fork && options.proto == SOCK_STREAM) { if ((child_pid = fork()) == 0) {
if (fork() == 0) {
if (options.log) if (options.log)
printf("forking TCP %d ...\n", getpid()); printf("forking for %s, %d ...\n", inet_ntoa(rc.client_addr.sin_addr), getpid());
close(rc.server_socket);
run(); run();
exit(0); exit(0);
} }
close(rc.client_socket); add_udp_client(rc.client_addr, child_pid);
// while (waitpid(-1, NULL, WNOHANG) > 0)
// ; /* Reap any zombie processes (clients that exited) */
} else {
printf("existing udp client %s, handled by %d ...\n", inet_ntoa(rc.client_addr.sin_addr), clients[client_index].pid);
}
return 1;
}
void fork_tcp() {
if (fork() == 0) {
if (options.log)
printf("forking %d ...\n", getpid());
close(rc.server_socket);
run();
exit(0);
}
close(rc.client_socket);
}
void serve(void) {
#ifdef __MINGW32__
run();
#else
if (options.fork) {
(options.proto == SOCK_DGRAM) ?
fork_udp() :
fork_tcp();
} }
else { else {
run(); run();
......
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