1 module server; 2 3 import hunt.net; 4 import hunt.logging; 5 6 import hunt.net.codec.textline; 7 import hunt.util.worker; 8 9 import std.format; 10 import core.atomic; 11 12 import hunt.io.channel.posix.AbstractStream; 13 import hunt.util.queue.SimpleQueue; 14 15 enum Host = "0.0.0.0"; 16 enum Port = 8080; 17 18 enum string ResponseContent = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nConnection: Keep-Alive\r\nContent-Type: text/plain\r\nServer: Hunt/1.0\r\nDate: Wed, 17 Apr 2013 12:00:00 GMT\r\n\r\nHello, World!"; 19 20 21 // "versions": ["HUNT_DEBUG", "HUNT_IO_DEBUG", "HUNT_NET_DEBUG", "HUNT_METRIC"], 22 23 void main() { 24 25 NetServerOptions options = new NetServerOptions(); 26 options.workerThreadSize = 32; 27 shared int counter = 0; 28 29 NetServer server = NetUtil.createNetServer(options); 30 31 // dfmt off 32 server.setHandler(new class NetConnectionHandler { 33 34 override void connectionOpened(Connection connection) { 35 // debug infof("Connection created: %s", connection.getRemoteAddress()); 36 } 37 38 override void connectionClosed(Connection connection) { 39 // debug infof("Connection closed: %s", connection.getRemoteAddress()); 40 } 41 42 override DataHandleStatus messageReceived(Connection connection, Object message) { 43 string str; 44 version(HUNT_IO_DEBUG) { 45 tracef("message type: %s", typeid(message).name); 46 str = format("data received: %s", message.toString()); 47 tracef(str); 48 } 49 50 int c = atomicOp!("+=")(counter, 1); 51 // if(c % 100 == 0) 52 // warningf("Response: %d", c); 53 54 import hunt.io.ByteBuffer; 55 ByteBuffer buffer = cast(ByteBuffer)message; 56 str = cast(string)buffer.peekRemaining(); 57 // warning(str); 58 59 // if(str.length != 176) { 60 // warning(str); 61 // } 62 63 if(str == "peek") { 64 65 Worker taskWorker = connection.getStream().taskWorker; 66 taskWorker.inspect(); 67 } 68 69 import hunt.io.BufferUtils; 70 BufferUtils.clear(buffer); 71 72 import core.thread; 73 // Thread.sleep(10.msecs); 74 75 connection.write(ResponseContent); 76 77 // if(c < 5) { 78 // return DataHandleStatus.Pending; 79 // } else { 80 81 // return DataHandleStatus.Done; 82 // } 83 return DataHandleStatus.Done; 84 } 85 86 override void exceptionCaught(Connection connection, Throwable t) { 87 warning(t); 88 } 89 90 override void failedOpeningConnection(int connectionId, Throwable t) { 91 warning(t); 92 } 93 94 override void failedAcceptingConnection(int connectionId, Throwable t) { 95 warning(t); 96 } 97 }).listen(Host, Port); 98 99 // dmft on 100 }