mirror of https://github.com/adamdruppe/arsd.git
basic threading
This commit is contained in:
parent
5b38267e09
commit
bedea1ec31
21
httpd.d
21
httpd.d
|
@ -89,29 +89,32 @@ class HttpdConnection(CustomCgi) : Connection /* if(is(CustomCgi : Cgi)) */ {
|
||||||
Cgi cgi;
|
Cgi cgi;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cgi = new CustomCgi(headers, data, peerAddress(),
|
cgi = new CustomCgi(headers, data, peerAddress(),
|
||||||
cast(void delegate(const(ubyte)[])) &this.write);
|
cast(void delegate(const(ubyte)[])) &this.write);
|
||||||
} catch(Throwable t) {
|
} catch(Throwable t) {
|
||||||
write("HTTP/1.1 400 Bad Request\r\n");
|
write("HTTP/1.1 400 Bad Request\r\n");
|
||||||
write("Content-Type: text/plain\r\n");
|
write("Content-Type: text/plain\r\n");
|
||||||
|
string s = t.toString();
|
||||||
|
write("Content-Length: "~to!string(s.length)~"\r\n");
|
||||||
write("Connection: close\r\n");
|
write("Connection: close\r\n");
|
||||||
write("\r\n");
|
write("\r\n");
|
||||||
write(t.toString());
|
write(s);
|
||||||
|
|
||||||
|
closeConnection = true;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
scope(exit) {
|
||||||
handler(cgi);
|
|
||||||
cgi.close();
|
cgi.close();
|
||||||
cgi.dispose();
|
cgi.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
handler(cgi);
|
||||||
} catch(Throwable e) {
|
} catch(Throwable e) {
|
||||||
cgi.setResponseStatus("500 Internal Server Error");
|
cgi.setResponseStatus("500 Internal Server Error");
|
||||||
cgi.write(e.toString());
|
cgi.write(e.toString());
|
||||||
cgi.close();
|
|
||||||
cgi.dispose();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
41
netman.d
41
netman.d
|
@ -54,7 +54,7 @@ class ConnectionException : Exception {
|
||||||
class NetworkManager {
|
class NetworkManager {
|
||||||
// you might want to override this to construct subclasses of connection
|
// you might want to override this to construct subclasses of connection
|
||||||
protected:
|
protected:
|
||||||
Connection allocConnection(int port){
|
Connection allocConnection(int port) {
|
||||||
if(auto a = port in allocs)
|
if(auto a = port in allocs)
|
||||||
return (*a)();
|
return (*a)();
|
||||||
assert(0);
|
assert(0);
|
||||||
|
@ -266,10 +266,19 @@ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, on.sizeof);
|
||||||
if(s == -1)
|
if(s == -1)
|
||||||
throw new Exception("accept");
|
throw new Exception("accept");
|
||||||
|
|
||||||
auto con = allocConnection(c.port);
|
version(threaded_connections) {
|
||||||
con.parentManager = this;
|
auto con = allocConnection(c.port);
|
||||||
addConnection(con, s, addr, c.port);
|
con.socket = s;
|
||||||
con.onRemoteConnect();
|
con.addr = addr;
|
||||||
|
con.port = c.port;
|
||||||
|
auto t = new ConnectionThread(con);
|
||||||
|
t.start();
|
||||||
|
} else {
|
||||||
|
auto con = allocConnection(c.port);
|
||||||
|
con.parentManager = this;
|
||||||
|
addConnection(con, s, addr, c.port);
|
||||||
|
con.onRemoteConnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,3 +485,25 @@ int now() {
|
||||||
return cast(int) getUTCtime();
|
return cast(int) getUTCtime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
version(threaded_connections) {
|
||||||
|
import core.thread;
|
||||||
|
import std.stdio : writeln;
|
||||||
|
class ConnectionThread : Thread {
|
||||||
|
Connection connection;
|
||||||
|
this(Connection c) {
|
||||||
|
connection = c;
|
||||||
|
super(&run);
|
||||||
|
}
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
scope(exit)
|
||||||
|
connection.disconnectNow();
|
||||||
|
auto manager = new NetworkManager();
|
||||||
|
connection.parentManager = manager;
|
||||||
|
manager.addConnection(connection, connection.socket, connection.addr, connection.port);
|
||||||
|
while(manager.proceed()) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue