Skip to content

Commit 7bae8b1

Browse files
committed
Will have to debug clients[i]->error and waitpid()
1 parent 37555f2 commit 7bae8b1

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

ddcd.cpp

+48-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ char host_address[100] = "127.0.0.1";
3939
int decimation = 0;
4040
int bufsize = 1024;
4141
int bufsizeall;
42+
int pipe_max_size;
4243
char* buf;
4344

4445
int set_nonblocking(int fd)
@@ -137,6 +138,22 @@ int main(int argc, char* argv[])
137138
FD_SET(STDIN_FILENO, &select_fds);
138139
int highfd = ((listen_socket>STDIN_FILENO)?listen_socket:STDIN_FILENO) + 1;
139140

141+
FILE* tempfile = fopen("/proc/sys/fs/pipe-max-size","r");
142+
if(!tempfile)
143+
{
144+
perror(MSG_START "cannot read /proc/sys/fs/pipe-max-size");
145+
}
146+
else
147+
{
148+
char pipe_max_size_str[100];
149+
int tfread = fread(pipe_max_size_str, 1, 100, tempfile);
150+
pipe_max_size_str[tfread]='\0';
151+
pipe_max_size = atoi(pipe_max_size_str);
152+
//fprintf(stderr, MSG_START "note: pipe_max_size = %d\n", pipe_max_size);
153+
//if(pipe_max_size>4096 && fcntl(STDIN_FILENO, F_SETPIPE_SZ, pipe_max_size)==-1)
154+
// perror("failed to fcntl(STDIN_FILENO, F_SETPIPE_SZ, ...)");
155+
}
156+
140157
for(;;)
141158
{
142159
//Let's wait until there is any new data to read, or any new connection!
@@ -146,13 +163,16 @@ int main(int argc, char* argv[])
146163
if( (new_socket = accept(listen_socket, (struct sockaddr*)&addr_cli, &addr_cli_len)) != -1)
147164
{
148165
this_client = new client_t;
166+
this_client->error = 0;
149167
memcpy(&this_client->addr, &addr_cli, sizeof(this_client->addr));
150168
this_client->socket = new_socket;
151169
if(pipe(this_client->pipefd) == -1)
152170
{
153171
perror(MSG_START "cannot open new pipe() for the client.\n");
154172
continue;
155173
}
174+
if(fcntl(this_client->pipefd[1], F_SETPIPE_SZ, pipe_max_size) == -1)
175+
perror("failed to F_SETPIPE_SZ for the client pipe!");
156176
if(this_client->pid = fork())
157177
{
158178
//We're the parent
@@ -175,14 +195,33 @@ int main(int argc, char* argv[])
175195
}
176196
else if(retval != -1)
177197
{
178-
for (int i=0;i<clients.size(); i++)
198+
for (int i=0; i<clients.size(); i++)
179199
{
180200
if(write(clients[i]->pipefd[1], buf, retval)==-1)
181-
print_client(clients[i], "lost buffer, failed to write pipe");
201+
{
202+
203+
if(!clients[i]->error) print_client(clients[i], "lost buffer, failed to write pipe");
204+
else clients[i]->error=1;
205+
//fprintf(stderr, MSG_START "errno is %d\n", errno); //usually 11
206+
int wpstatus;
207+
int wpresult = waitpid(clients[i]->pid, &wpstatus, WNOHANG);
208+
if(wpresult == -1) print_client(clients[i], "error while waitpid()!");
209+
else if(wpresult == 0)
210+
{
211+
//Client exited!
212+
print_client(clients[i], "closing client from main process.");
213+
close(clients[i]->pipefd[1]);
214+
close(clients[i]->socket);
215+
delete clients[i];
216+
clients.erase(clients.begin()+i);
217+
print_client(clients[i], "okay.");
218+
}
219+
}
220+
else clients[i]->error=0;
182221
}
183222
}
184223
//TODO: at the end, server closes pipefd[1] for client
185-
//close(this_client->pipefd[1]);
224+
//
186225
}
187226

188227
return 0;
@@ -200,11 +239,15 @@ void client_cleanup()
200239

201240
void client()
202241
{
203-
printf("I'm the client\n");
242+
fprintf(stderr, "I'm the client\n");
204243
for(;;)
205244
{
206245
read(this_client->pipefd[0],buf,bufsizeall);
207-
send(this_client->socket,buf,bufsizeall,0);
246+
if(send(this_client->socket,buf,bufsizeall,0)==-1)
247+
{
248+
print_client(this_client, "closing.");
249+
exit(0);
250+
}
208251
}
209252
}
210253

ddcd.h

+3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
#include <vector>
1414
#include <unistd.h>
1515
#include <fcntl.h>
16+
#include <errno.h>
17+
#include <sys/wait.h>
1618

1719
typedef struct client_s
1820
{
1921
struct sockaddr_in addr;
2022
int socket;
2123
pid_t pid;
2224
int pipefd[2];
25+
int error;
2326
} client_t;
2427

2528

0 commit comments

Comments
 (0)