1 module dnetd.dlistener;
2 
3 import std.socket;
4 import dnetd.dserver;
5 import core.thread;
6 import dnetd.dconnection;
7 import gogga;
8 import std.conv : to;
9 
10 public final class DListener : Thread
11 {
12     /* Associated server */
13     private DServer server;
14 
15     /* The socket */
16     private Socket serverSocket;
17 
18     /**
19     * Creates new listener with the associated server
20     * and listens on the given address
21     */
22     this(DServer server, AddressInfo addressInfo)
23     {
24         super(&dequeueLoop);
25 
26         /* Set the server */
27         this.server = server;
28 
29         // /* Get the Address */
30         Address address = addressInfo.address;
31 
32     
33 
34         gprintln("DListener: Hello there I am a new listener "~to!(string)(addressInfo));
35 
36 
37 
38         /* TODO: Check AF_FAMILY (can only be INET,INET6,UNIX) */
39         /* TODO: Check SocketType (can only be STREAM) */
40         /* TODO: Check Protocol, can only be RAW (assuming UNIX) or TCP */
41         /* address.addressFamily, addressInfo.type, addressInfo.protocol */
42 
43         /* Create the Socket and bind it */
44         serverSocket = new Socket(addressInfo);
45         serverSocket.bind(address);
46         gprintln("New listener started with address "~to!(string)(addressInfo));
47 
48         /* Start the connection dequeue thread */
49 		//start();
50     }
51 
52     private void dequeueLoop()
53 	{
54         gprintln("Starting dequeue loop...");
55         
56 		/* Start accepting-and-enqueuing connections */
57 		serverSocket.listen(0); /* TODO: Linux be lile, hehahahhahahah who gives one - I give zero */
58 		
59 		while(true)
60 		{
61 			/* Dequeue a connection */
62             gprintln("Awaiting a connection...");
63 			Socket socket = serverSocket.accept();
64             gprintln("Dequeued a socket");
65 
66 			/* Spawn a connection handler */
67 			DConnection connection = new DConnection(server, socket);
68 
69 			/* Add to the connection queue */
70 			server.addConnection(connection);
71 		}
72 	}
73 }