Initial stab at integrating netLib

parent cfba8b82
CC = gcc
CFLAGS = -fopenmp -Ideps -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer -D_GNU_SOURCE
LDFLAGS = -static -static-libgcc -lm
CFLAGS = -fopenmp -Ideps -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer -D_GNU_SOURCE -DMS_WINDOWS
LDFLAGS = -static -static-libgcc -lm -lwsock32
ifeq ($(OS),Windows_NT)
BIN = dukboot.exe
......@@ -13,7 +13,7 @@ endif
SRC = $(wildcard src/*.c)
HEADERS = $(SRC:.c=.h)
OBJS = $(SRC:.c=.o)
DEPS = $(wildcard deps/duktape/*.c)
DEPS = $(wildcard deps/duktape/*.c) deps/netLib/netSocket.c
DEPOBJS = $(DEPS:.c=.o)
main: $(MAKEHEADERS) $(HEADERS) $(DEPOBJS) $(OBJS)
......
function (args) {
var env = args.environ;
var argv = args.argv;
var modules = args.modules;
var Queues = args.Queues;
var timers = Queues.timers;
var compile = modules.compile;
modules.webserver();
return
}
......@@ -78,6 +78,8 @@ int main(int argc, char *argv[] /* char *environ[] */) {
duk_put_prop_string(ctx, -2, "assert_safe_path");
duk_push_c_function(ctx, native_sleep, 1);
duk_put_prop_string(ctx, -2, "sleep");
duk_push_c_function(ctx, native_socket, 0);
duk_put_prop_string(ctx, -2, "webserver");
duk_put_prop_string(ctx, -2, "modules");
// Provide direct access to the event loop
duk_push_object(ctx);
......
#include "duktape/duktape.h"
#include "native_print.h"
#include "netLib/netSocket.h"
// Working command in real Windows (not Windows Subsystem for Linux):
// gcc webserver.c netSocket.c -o webserver -DMS_WINDOWS -lwsock32
const int port = 8192;
const int buffsize = 2000;
/* Default HTTP page with HTTP headers */
char webpage[] =
"HTTP/1.0 200 OK\n"
"Server: BareMetal (http://www.returninfinity.com)\n"
"Content-type: text/html\n"
"\n"
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<title>Hello world</title>\n"
"</head>\n"
"<body>\n"
"Hello World!\n"
"</body>\n"
"</html>\n";
duk_ret_t native_socket(duk_context *ctx) {
int tint;
netSocket *clientsocket, *listensocket;
char tempbuffer[buffsize];
printf("Web Server demo for netLib v0.4\nListening on port %d\n", port);
// Start the netLib library
netInit();
// Initiate the sockets
clientsocket = netInitSocket();
listensocket = netInitSocket();
// Set the server to listen
netListen(listensocket, port);
printf("\n\n");
// Main program loop
for (;;)
{
// Check for an incoming connection
if (netAccept(listensocket, clientsocket) > 0)
{
// A client has connected!
printf("\nHello.");
// Clear the buffer
memset(tempbuffer, 0, buffsize);
// Check for data to receive and print if there was something
tint = netRecv(clientsocket, tempbuffer, buffsize);
if (tint > 0)
{
printf("\nReceived %d bytes.", tint);
// printf("\n--------------------\n%s", tempbuffer);
}
// Send data to the client
tint = netSend(clientsocket, webpage, strlen(webpage));
printf("\nSent %d bytes.", tint);
// Disconnect the client
netDisconnect(clientsocket);
printf("\nGoodbye.\n\n");
}
// Why isn't this done on its own...
fflush(stdout);
}
netDisconnect(listensocket);
// Stop the netLib library
netStop();
printf("\n");
return 0;
}
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