@@ -39,6 +39,7 @@ char host_address[100] = "127.0.0.1";
39
39
int decimation = 0 ;
40
40
int bufsize = 1024 ;
41
41
int bufsizeall;
42
+ int pipe_max_size;
42
43
char * buf;
43
44
44
45
int set_nonblocking (int fd)
@@ -137,6 +138,22 @@ int main(int argc, char* argv[])
137
138
FD_SET (STDIN_FILENO, &select_fds);
138
139
int highfd = ((listen_socket>STDIN_FILENO)?listen_socket:STDIN_FILENO) + 1 ;
139
140
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
+
140
157
for (;;)
141
158
{
142
159
// 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[])
146
163
if ( (new_socket = accept (listen_socket, (struct sockaddr *)&addr_cli, &addr_cli_len)) != -1 )
147
164
{
148
165
this_client = new client_t ;
166
+ this_client->error = 0 ;
149
167
memcpy (&this_client->addr , &addr_cli, sizeof (this_client->addr ));
150
168
this_client->socket = new_socket;
151
169
if (pipe (this_client->pipefd ) == -1 )
152
170
{
153
171
perror (MSG_START " cannot open new pipe() for the client.\n " );
154
172
continue ;
155
173
}
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!" );
156
176
if (this_client->pid = fork ())
157
177
{
158
178
// We're the parent
@@ -175,14 +195,33 @@ int main(int argc, char* argv[])
175
195
}
176
196
else if (retval != -1 )
177
197
{
178
- for (int i=0 ;i<clients.size (); i++)
198
+ for (int i=0 ; i<clients.size (); i++)
179
199
{
180
200
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 ;
182
221
}
183
222
}
184
223
// TODO: at the end, server closes pipefd[1] for client
185
- // close(this_client->pipefd[1]);
224
+ //
186
225
}
187
226
188
227
return 0 ;
@@ -200,11 +239,15 @@ void client_cleanup()
200
239
201
240
void client ()
202
241
{
203
- printf ( " I'm the client\n " );
242
+ fprintf (stderr, " I'm the client\n " );
204
243
for (;;)
205
244
{
206
245
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
+ }
208
251
}
209
252
}
210
253
0 commit comments