Giter Club home page Giter Club logo

lc19's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

lc19's Issues

Serving multiple clients in parallel with `fork`

Hi! This patch adds support for serving multiple clients in parallel using fork. It does not require any new library, and it just adds 11 lines of code. I tested as much as I could, and I think everything exits as clean as it should.

I hope it is of any use!

diff --git a/lc19.c b/lc19.c
index b3be73f..15fc13c 100644
--- a/lc19.c
+++ b/lc19.c
@@ -119,6 +119,7 @@ int main(int argc, char **argv) {
 	// Assign signal handlers
 	signal(SIGINT, handle_sigint);
 	signal(SIGHUP, handle_sighup);
+	signal(SIGCHLD, SIG_IGN);
 
 	// Parse command-line arguments
 	argp_parse(&parser, argc, argv, 0, 0, &arguments);
@@ -145,7 +146,7 @@ int main(int argc, char **argv) {
 		// Initialize incoming client
 		struct sockaddr_in addr;
 		socklen_t addrlen = sizeof(addr);
-		SSL *ssl = SSL_new(ctx);
+		SSL *ssl;
 
 		// Accept connection
 		int client;
@@ -153,10 +154,20 @@ int main(int argc, char **argv) {
 			fprintf(stderr, "Error: Failed to accept incoming connection");
 			continue;
 		}
-		printf("New: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
-		SSL_set_fd(ssl, client);
 
-		handle(ssl, e);
+		printf("New: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
+		switch (fork()) {
+		case 0:
+			ssl = SSL_new(ctx);
+			SSL_set_fd(ssl, client);
+			handle(ssl, e);
+			exit(0);
+			break;
+		case -1:
+			fprintf(stderr,"Error: Forking failed.\n");
+		default:
+			close(client);
+		}
 	}
 
 	close(server);

Binary search of the endpoints

Hi! I have another patch, this one is less useful.

This patch orders the endpoints by the url_path when the list is created. Later, when a responses is needed, it uses a binary search to find the file path and mimetype. This should result in faster response time when the number of endpoints is large.

It uses the stdlib's qsort and bsearch, so again no new libs, it should be efficient, and in total it add just 4 new lines of code. Still I'm not sure if it is worthy. If you like it, just apply it.

Bye!

diff --git a/endpoint.c b/endpoint.c
index dbd2a5c..1b8d621 100644
--- a/endpoint.c
+++ b/endpoint.c
@@ -14,2 +14,10 @@
 
+int lines;
+
+static int comp_ep(const void *i1, const void *i2) {
+	const endpoint *ep1 = i1;
+	const endpoint *ep2 = i2;
+	return strcmp(ep1->url_path, ep2->url_path);
+}
+
 // Gets the number of lines in a file
@@ -71,16 +79,12 @@ response url_to_response(char *url, endpoint *endpoints) {
 	// Find the endpoint at the url
-	int e_index = -1;
-	for (int i = 0; endpoints[i].end; i++)  {
-		if (strcmp(url_path, endpoints[i].url_path) == 0) {
-			e_index = i;
-			break;
-		}
-	}
-	if (e_index == -1)
+	endpoint *e, key;
+	strcpy(key.url_path, url_path);
+	e = bsearch(&key, endpoints, lines, sizeof(endpoints[0]), comp_ep);
+
+	if (e == NULL)
 		return (response){ .code = 51, .mime = "Not found, à la 404" };
-	endpoint e = endpoints[e_index];
 
 	response resp = (response){ .code = 20 };
-	strcpy(resp.file_path, e.file_path);
-	strcpy(resp.mime, e.mime);
+	strcpy(resp.file_path, e->file_path);
+	strcpy(resp.mime, e->mime);
 	return resp;
@@ -125,3 +129,2 @@ void get_endpoints(endpoint *endpoints, char *path) {
 		// Copy the parsed data
-		endpoints[line_num] = (endpoint){ .end = 1 };
 		strcpy(endpoints[line_num].mime, mime);
@@ -141,3 +144,5 @@ void get_endpoints(endpoint *endpoints, char *path) {
 
-	endpoints[line_num] = (endpoint){ .end = 0 };
+	lines = line_num;
+	qsort(endpoints, lines, sizeof(endpoints[0]), comp_ep);
+
 	fclose(fp);
diff --git a/endpoint.h b/endpoint.h
index 77b9361..f2e6bcd 100644
--- a/endpoint.h
+++ b/endpoint.h
@@ -4,3 +4,2 @@ typedef struct {
 	char url_path[256];
-	int end;
 } endpoint;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.