#define EV_STANDALONE 1 // so i don't need a config.h file
#define EV_SELECT_IS_WINSOCKET 1 // windows
//#define EV_USE_POLL 1 // uncomment me for unix
//#define EV_USE_EPOLL 1 // uncomment me for gnu/linux
//#define EV_USE_KQUEUE 1 // uncomment me for bsd/osx
//#define EV_USE_PORT 1 // uncomment me for solaris
#define EV_STAT_ENABLE 0 // disable file watching
#include "ev.c"
To compile it, do the following from that folder (you'll get a couple of warnings which you can ignore):
\dm\bin\dmc myev.c -c
This will produce a myev.obj which you'll need later
Server
1: Create a new folder for the simple 'D' server. You'll need the ev.d with the libev bindings, which you can download or copy n paste from here.
2: Copy the 'myev.obj' that you compiled earlier from the libev folder into this folder.
3: You'll then create the 'server.d' which is the main point of this article:
import tango.io.Console; import tango.net.ServerSocket, tango.net.SocketConduit; import ev; extern (C) { static void libev_cb (ev_loop_t *loop, ev_io *w, int revents) { callback(); } // http://msdn.microsoft.com/en-us/library/bdts1c9x(VS.71).aspx int _open_osfhandle (long osfhandle, int flags); } ServerSocket listener; void main() { listener = new ServerSocket (new InternetAddress(80)); int fd = _open_osfhandle(listener.fileHandle,0); // for win32: convert from socket to file descriptor // Start libev ev_loop_t* loop = ev_default_loop(0); ev_io io_watcher; ev_io_init(&io_watcher, &libev_cb, fd, READ); ev_io_start(loop, &io_watcher); ev_loop(loop, 0); } void callback() { // accept the connection SocketConduit request = listener.accept; // wait for the 'http get ...' char[1024] response; uint len = request.input.read (response); Cout (response[0..len]).newline; // you'll want to comment this out if you run apachebench against this... // send HTML request.output.write("HTTP/1.1 200 OK <html> <style>body {font-family:trebuchet ms;background:#424242;color:#fff;text-align:center;margin-top:10em;}</style> <body><h1>Your 'D' web server is alive!</h1></body> </html>"); request.close(); }
\dmd\bin\dmd server.d ev.d myev.obj
server
Next test it in your browser, by browsing to 'localhost':
[[posterous-content:zuyleIbpsAFDquIexnoo]]
Voila! And if i do an apachebench (ab -n 10000 -c 100), my requests-per-second is 2071 for an 2 years old P4-3ghz, and it only uses 2.4mb of memory, which is pretty impressive considering each Ruby on Rails Mongrel typically uses ~30megs last time i checked.
I guess the next question is: why doesn't someone make a super-scalable web framework using the speed/efficiency and ease of D?
If any of this doesn't work, make sure you post a comment so i can sort it out - cheers.
Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.
(Comp Sci, Hons - UTS)
Software Developer (Freelancer / Contractor) in Australia.
I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!
Get in touch:
[email protected]
github.com/chrishulbert
linkedin