1 module hunt.net.NetServer; 2 3 import hunt.net.Connection; 4 import hunt.net.codec; 5 import hunt.net.NetServerOptions; 6 import hunt.event.EventLoopGroup; 7 import hunt.util.Lifecycle; 8 9 import std.socket; 10 11 12 /** 13 * Represents a TCP server 14 * 15 * @author <a href="http://tfox.org">Tim Fox</a> 16 */ 17 interface NetServer { 18 19 EventLoopGroup eventLoopGroup(); 20 21 NetServerOptions getOptions(); 22 23 // NetServer setOptions(NetServerOptions options); 24 25 Codec getCodec(); 26 27 NetServer setCodec(Codec codec); 28 29 /** 30 * @return the handler which will handle all connections managed by this server. 31 */ 32 NetConnectionHandler getHandler(); 33 34 /** 35 * Sets the handler which will handle all connections managed by this server. 36 * 37 * @param handler The NetConnectionHandler to use 38 */ 39 NetServer setHandler(NetConnectionHandler handler); 40 41 42 /** 43 * Return the connect stream for this server. The server can only have at most one handler at any one time. 44 * As the server accepts TCP or SSL connections it creates an instance of {@link Connection} and passes it to the 45 * connect stream {@link ReadStream#handler(hunt.net.NetEventHandler)}. 46 * 47 * @return the connect stream 48 */ 49 // ReadStream!(Connection) connectStream(); 50 51 /** 52 * Supply a connect handler for this server. The server can only have at most one connect handler at any one time. 53 * As the server accepts TCP or SSL connections it creates an instance of {@link Connection} and passes it to the 54 * connect handler. 55 * 56 * @return a reference to this, so the API can be used fluently 57 */ 58 // NetServer connectHandler(ConnectHandler handler); 59 60 61 // ConnectHandler connectHandler(); 62 63 /** 64 * Start listening on the port and host as configured in the {@link hunt.net.NetServerOptions} used when 65 * creating the server. 66 * <p> 67 * The server may not be listening until some time after the call to listen has returned. 68 * 69 * @return a reference to this, so the API can be used fluently 70 */ 71 72 void listen(); 73 74 /** 75 * Start listening on the specified port and host, ignoring port and host configured in the {@link hunt.net.NetServerOptions} used when 76 * creating the server. 77 * <p> 78 * Port {@code 0} can be specified meaning "choose an random port". 79 * <p> 80 * Host {@code 0.0.0.0} can be specified meaning "listen on all available interfaces". 81 * <p> 82 * The server may not be listening until some time after the call to listen has returned. 83 * 84 * @return a reference to this, so the API can be used fluently 85 */ 86 87 void listen(string host, int port); 88 89 /** 90 * Start listening on the specified port and host "0.0.0.0", ignoring port and host configured in the 91 * {@link hunt.net.NetServerOptions} used when creating the server. 92 * <p> 93 * Port {@code 0} can be specified meaning "choose an random port". 94 * <p> 95 * The server may not be listening until some time after the call to listen has returned. 96 * 97 * @return a reference to this, so the API can be used fluently 98 */ 99 100 void listen(int port); 101 102 /** 103 * Start listening on the specified local address, ignoring port and host configured in the {@link hunt.net.NetServerOptions} used when 104 * creating the server. 105 * <p> 106 * The server may not be listening until some time after the call to listen has returned. 107 * 108 * @param localAddress the local address to listen on 109 * @return a reference to this, so the API can be used fluently 110 */ 111 112 // NetServer listen(SocketAddress localAddress); 113 114 /** 115 * Like {@link #listen(SocketAddress)} but providing a handler that will be notified when the server is listening, or fails. 116 * 117 * @param localAddress the local address to listen on 118 * @param listenHandler handler that will be notified when listening or failed 119 * @return a reference to this, so the API can be used fluently 120 */ 121 122 // NetServer listen(SocketAddress localAddress, ListenHandler listenHandler); 123 124 125 /** 126 * Close the server. This will close any currently open connections. The close may not complete until after this 127 * method has returned. 128 */ 129 void close(); 130 131 /** 132 * Like {@link #close} but supplying a handler that will be notified when close is complete. 133 * 134 * @param completionHandler the handler 135 */ 136 // void close(AsyncVoidResultHandler completionHandler); 137 138 /** 139 * The actual port the server is listening on. This is useful if you bound the server specifying 0 as port number 140 * signifying an ephemeral port 141 * 142 * @return the actual port the server is listening on. 143 */ 144 int actualPort(); 145 146 bool isOpen(); 147 } 148 149 150 /** 151 */ 152 // abstract class AbstractServer : AbstractLifecycle, NetServer { 153 // protected Address _address; 154 // // protected ConnectHandler _connectHandler; 155 156 // @property Address bindingAddress() { 157 // return _address; 158 // } 159 160 // NetConnectionHandler getHandler() { 161 162 // } 163 164 // /** 165 // * Sets the handler which will handle all connections managed by this server. 166 // * 167 // * @param handler The NetConnectionHandler to use 168 // */ 169 // void setHandler(NetConnectionHandler handler); 170 171 // void close() { 172 // stop(); 173 // } 174 175 // // AbstractServer connectHandler(ConnectHandler handler) { 176 // // _connectHandler = handler; 177 // // return this; 178 // // } 179 180 // abstract void listen(string host = "0.0.0.0", int port = 0); 181 // }