NIO+Multithreaded TCPSocket listener, very low cpu utilisation
ade90036
andrea.rizzini at gmail.com
Wed Nov 15 11:13:52 UTC 2017
So thanks for the suggestions, i have fixed HTTP response not
postman cal also parse the headers correctly!! happy days.
I have removed the duration from the Socket.select but the
application seems to process a bunch or requests and then it
stalls for several seconds (3/5) and then it resumes.
The httpclinet which i'm using to test the application is
reporting: "connection timeout".
Could this be caused by the GC?
```updated code
import std.algorithm : remove;
import std.conv : to;
import core.thread: Thread;
import std.socket : InternetAddress, Socket, SocketException,
SocketSet, TcpSocket, SocketShutdown;
import core.time : Duration, dur;
import std.stdio : writeln, writefln;
import std.parallelism : task, TaskPool;
string to_retlf (string s)
{
import std.algorithm;
import std.string;
return s
.lineSplitter
.map!(a => chomp (a))
.join ("\r\n");
}
void main(string[] args)
{
ushort port;
if (args.length >= 2)
port = to!ushort(args[1]);
else
port = 4444;
auto listener = new TcpSocket();
assert(listener.isAlive);
listener.blocking = false;
listener.bind(new InternetAddress(port));
listener.listen(100);
writefln("Listening on port %d.", port);
auto taskPool = new TaskPool(8);
string response = "HTTP/1.1 200 OK
Server: dland:v2.076.1
Date: Tue, 11 Nov 2017 15:56:02 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 51
<html><head></head><body>Hello World!</body></html>".to_retlf;
new Thread({
auto listeningSet = new SocketSet();
while(true) {
listeningSet.add(listener);
if (Socket.select(listeningSet, null, null)) {
if (listeningSet.isSet(listener)) //
connection request
{
Socket socket = listener.accept();
assert(socket.isAlive);
//writefln("Connection from %s established.",
socket.remoteAddress().toString());
auto task = task!handle_socket(socket,
response);
taskPool.put(task);
}
}
listeningSet.reset();
}
}).start();
}
void handle_socket(Socket socket, string response) {
auto socketSet = new SocketSet();
while(true) {
socketSet.add(socket);
if (Socket.select(socketSet, null, null)) {
char[1024] buf;
auto datLength = socket.receive(buf[]);
if (datLength == Socket.ERROR)
writeln("Connection error.");
else if (datLength != 0)
{
//writefln("Received %d bytes from %s: \"%s\"",
datLength, socket.remoteAddress().toString(), buf[0..datLength]);
//writefln("Writing response");
socket.send(response);
}
// release socket resources now
socket.close();
break;
}
socketSet.reset();
}
```
Regards
More information about the Digitalmars-d-learn
mailing list