summaryrefslogtreecommitdiffstats
path: root/examples/network_server/server.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2014-07-20 14:44:41 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2014-07-20 14:44:41 +0300
commiteaa3c7b157ffd2a308dfc6d35d79309a9aacbcef (patch)
treec7822d7097348208e3dfcce82bf626bf59a5cfd2 /examples/network_server/server.c
parent3cf9668c755560a69e4858dea4f3c415bf807441 (diff)
Cleanup and comment the code of network_server example.
Update issue 123 Status: FixedInGit
Diffstat (limited to 'examples/network_server/server.c')
-rw-r--r--examples/network_server/server.c85
1 files changed, 56 insertions, 29 deletions
diff --git a/examples/network_server/server.c b/examples/network_server/server.c
index 9a9c2644..46a5f38d 100644
--- a/examples/network_server/server.c
+++ b/examples/network_server/server.c
@@ -23,11 +23,16 @@
#include "fileproto.pb.h"
#include "common.h"
+/* This callback function will be called once during the encoding.
+ * It will write out any number of FileInfo entries, without consuming unnecessary memory.
+ * This is accomplished by fetching the filenames one at a time and encoding them
+ * immediately.
+ */
bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
DIR *dir = (DIR*) *arg;
struct dirent *file;
- FileInfo fileinfo;
+ FileInfo fileinfo = {};
while ((file = readdir(dir)) != NULL)
{
@@ -35,9 +40,12 @@ bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * cons
strncpy(fileinfo.name, file->d_name, sizeof(fileinfo.name));
fileinfo.name[sizeof(fileinfo.name) - 1] = '\0';
+ /* This encodes the header for the field, based on the constant info
+ * from pb_field_t. */
if (!pb_encode_tag_for_field(stream, field))
return false;
+ /* This encodes the data for the field, based on our FileInfo structure. */
if (!pb_encode_submessage(stream, FileInfo_fields, &fileinfo))
return false;
}
@@ -45,43 +53,59 @@ bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * cons
return true;
}
+/* Handle one arriving client connection.
+ * Clients are expected to send a ListFilesRequest, terminated by a '0'.
+ * Server will respond with a ListFilesResponse message.
+ */
void handle_connection(int connfd)
{
- ListFilesRequest request;
- ListFilesResponse response;
- pb_istream_t input = pb_istream_from_socket(connfd);
- pb_ostream_t output = pb_ostream_from_socket(connfd);
- DIR *directory;
+ DIR *directory = NULL;
- if (!pb_decode(&input, ListFilesRequest_fields, &request))
+ /* Decode the message from the client and open the requested directory. */
{
- printf("Decode failed: %s\n", PB_GET_ERROR(&input));
- return;
+ ListFilesRequest request = {};
+ pb_istream_t input = pb_istream_from_socket(connfd);
+
+ if (!pb_decode(&input, ListFilesRequest_fields, &request))
+ {
+ printf("Decode failed: %s\n", PB_GET_ERROR(&input));
+ return;
+ }
+
+ directory = opendir(request.path);
+ printf("Listing directory: %s\n", request.path);
}
- directory = opendir(request.path);
-
- printf("Listing directory: %s\n", request.path);
-
- if (directory == NULL)
+ /* List the files in the directory and transmit the response to client */
{
- perror("opendir");
+ ListFilesResponse response = {};
+ pb_ostream_t output = pb_ostream_from_socket(connfd);
- response.has_path_error = true;
- response.path_error = true;
- response.file.funcs.encode = NULL;
- }
- else
- {
- response.has_path_error = false;
- response.file.funcs.encode = &listdir_callback;
- response.file.arg = directory;
+ if (directory == NULL)
+ {
+ perror("opendir");
+
+ /* Directory was not found, transmit error status */
+ response.has_path_error = true;
+ response.path_error = true;
+ response.file.funcs.encode = NULL;
+ }
+ else
+ {
+ /* Directory was found, transmit filenames */
+ response.has_path_error = false;
+ response.file.funcs.encode = &listdir_callback;
+ response.file.arg = directory;
+ }
+
+ if (!pb_encode(&output, ListFilesResponse_fields, &response))
+ {
+ printf("Encoding failed: %s\n", PB_GET_ERROR(&output));
+ }
}
- if (!pb_encode(&output, ListFilesResponse_fields, &response))
- {
- printf("Encoding failed.\n");
- }
+ if (directory != NULL)
+ closedir(directory);
}
int main(int argc, char **argv)
@@ -90,8 +114,8 @@ int main(int argc, char **argv)
struct sockaddr_in servaddr;
int reuse = 1;
+ /* Listen on localhost:1234 for TCP connections */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
memset(&servaddr, 0, sizeof(servaddr));
@@ -112,6 +136,7 @@ int main(int argc, char **argv)
for(;;)
{
+ /* Wait for a client */
connfd = accept(listenfd, NULL, NULL);
if (connfd < 0)
@@ -128,4 +153,6 @@ int main(int argc, char **argv)
close(connfd);
}
+
+ return 0;
}