1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 /* 25 * Conversion parameters: 26 * inFile = GSocketListener.html 27 * outPack = gio 28 * outFile = SocketListener 29 * strct = GSocketListener 30 * realStrct= 31 * ctorStrct= 32 * clss = SocketListener 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_socket_listener_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.ErrorG 47 * - glib.GException 48 * - gio.AsyncResultIF 49 * - gio.Cancellable 50 * - gio.Socket 51 * - gio.SocketAddress 52 * - gio.SocketConnection 53 * structWrap: 54 * - GAsyncResult* -> AsyncResultIF 55 * - GCancellable* -> Cancellable 56 * - GObject* -> ObjectG 57 * - GSocket* -> Socket 58 * - GSocketAddress* -> SocketAddress 59 * - GSocketConnection* -> SocketConnection 60 * module aliases: 61 * local aliases: 62 * overrides: 63 */ 64 65 module gio.SocketListener; 66 67 public import gtkc.giotypes; 68 69 private import gtkc.gio; 70 private import glib.ConstructionException; 71 private import gobject.ObjectG; 72 73 74 private import glib.ErrorG; 75 private import glib.GException; 76 private import gio.AsyncResultIF; 77 private import gio.Cancellable; 78 private import gio.Socket; 79 private import gio.SocketAddress; 80 private import gio.SocketConnection; 81 82 83 84 private import gobject.ObjectG; 85 86 /** 87 * Description 88 * A GSocketListener is an object that keeps track of a set 89 * of server sockets and helps you accept sockets from any of the 90 * socket, either sync or async. 91 * If you want to implement a network server, also look at GSocketService 92 * and GThreadedSocketService which are subclass of GSocketListener 93 * that makes this even easier. 94 */ 95 public class SocketListener : ObjectG 96 { 97 98 /** the main Gtk struct */ 99 protected GSocketListener* gSocketListener; 100 101 102 public GSocketListener* getSocketListenerStruct() 103 { 104 return gSocketListener; 105 } 106 107 108 /** the main Gtk struct as a void* */ 109 protected override void* getStruct() 110 { 111 return cast(void*)gSocketListener; 112 } 113 114 /** 115 * Sets our main struct and passes it to the parent class 116 */ 117 public this (GSocketListener* gSocketListener) 118 { 119 super(cast(GObject*)gSocketListener); 120 this.gSocketListener = gSocketListener; 121 } 122 123 protected override void setStruct(GObject* obj) 124 { 125 super.setStruct(obj); 126 gSocketListener = cast(GSocketListener*)obj; 127 } 128 129 /** 130 */ 131 132 /** 133 * Creates a new GSocketListener with no sockets to listen for. 134 * New listeners can be added with e.g. g_socket_listener_add_address() 135 * or g_socket_listener_add_inet_port(). 136 * Since 2.22 137 * Throws: ConstructionException GTK+ fails to create the object. 138 */ 139 public this () 140 { 141 // GSocketListener * g_socket_listener_new (void); 142 auto p = g_socket_listener_new(); 143 if(p is null) 144 { 145 throw new ConstructionException("null returned by g_socket_listener_new()"); 146 } 147 this(cast(GSocketListener*) p); 148 } 149 150 /** 151 * Adds socket to the set of sockets that we try to accept 152 * new clients from. The socket must be bound to a local 153 * address and listened to. 154 * source_object will be passed out in the various calls 155 * to accept to identify this particular source, which is 156 * useful if you're listening on multiple addresses and do 157 * different things depending on what address is connected to. 158 * Since 2.22 159 * Params: 160 * socket = a listening GSocket 161 * sourceObject = Optional GObject identifying this source. [allow-none] 162 * Returns: TRUE on success, FALSE on error. 163 * Throws: GException on failure. 164 */ 165 public int addSocket(Socket socket, ObjectG sourceObject) 166 { 167 // gboolean g_socket_listener_add_socket (GSocketListener *listener, GSocket *socket, GObject *source_object, GError **error); 168 GError* err = null; 169 170 auto p = g_socket_listener_add_socket(gSocketListener, (socket is null) ? null : socket.getSocketStruct(), (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err); 171 172 if (err !is null) 173 { 174 throw new GException( new ErrorG(err) ); 175 } 176 177 return p; 178 } 179 180 /** 181 * Creates a socket of type type and protocol protocol, binds 182 * it to address and adds it to the set of sockets we're accepting 183 * sockets from. 184 * Note that adding an IPv6 address, depending on the platform, 185 * may or may not result in a listener that also accepts IPv4 186 * connections. For more determinstic behaviour, see 187 * g_socket_listener_add_inet_port(). 188 * source_object will be passed out in the various calls 189 * to accept to identify this particular source, which is 190 * useful if you're listening on multiple addresses and do 191 * different things depending on what address is connected to. 192 * If successful and effective_address is non-NULL then it will 193 * be set to the address that the binding actually occured at. This 194 * is helpful for determining the port number that was used for when 195 * requesting a binding to port 0 (ie: "any port"). This address, if 196 * requested, belongs to the caller and must be freed. 197 * Since 2.22 198 * Params: 199 * address = a GSocketAddress 200 * type = a GSocketType 201 * protocol = a GSocketProtocol 202 * sourceObject = Optional GObject identifying this source. [allow-none] 203 * effectiveAddress = location to store the address that was bound to, or NULL. [out][allow-none] 204 * Returns: TRUE on success, FALSE on error. 205 * Throws: GException on failure. 206 */ 207 public int addAddress(SocketAddress address, GSocketType type, GSocketProtocol protocol, ObjectG sourceObject, out SocketAddress effectiveAddress) 208 { 209 // gboolean g_socket_listener_add_address (GSocketListener *listener, GSocketAddress *address, GSocketType type, GSocketProtocol protocol, GObject *source_object, GSocketAddress **effective_address, GError **error); 210 GSocketAddress* outeffectiveAddress = null; 211 GError* err = null; 212 213 auto p = g_socket_listener_add_address(gSocketListener, (address is null) ? null : address.getSocketAddressStruct(), type, protocol, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &outeffectiveAddress, &err); 214 215 if (err !is null) 216 { 217 throw new GException( new ErrorG(err) ); 218 } 219 220 effectiveAddress = ObjectG.getDObject!(SocketAddress)(outeffectiveAddress); 221 return p; 222 } 223 224 /** 225 * Helper function for g_socket_listener_add_address() that 226 * creates a TCP/IP socket listening on IPv4 and IPv6 (if 227 * supported) on the specified port on all interfaces. 228 * source_object will be passed out in the various calls 229 * to accept to identify this particular source, which is 230 * useful if you're listening on multiple addresses and do 231 * different things depending on what address is connected to. 232 * Since 2.22 233 * Params: 234 * port = an IP port number (non-zero) 235 * sourceObject = Optional GObject identifying this source. [allow-none] 236 * Returns: TRUE on success, FALSE on error. 237 * Throws: GException on failure. 238 */ 239 public int addInetPort(ushort port, ObjectG sourceObject) 240 { 241 // gboolean g_socket_listener_add_inet_port (GSocketListener *listener, guint16 port, GObject *source_object, GError **error); 242 GError* err = null; 243 244 auto p = g_socket_listener_add_inet_port(gSocketListener, port, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err); 245 246 if (err !is null) 247 { 248 throw new GException( new ErrorG(err) ); 249 } 250 251 return p; 252 } 253 254 /** 255 * Listens for TCP connections on any available port number for both 256 * IPv6 and IPv4 (if each are available). 257 * This is useful if you need to have a socket for incoming connections 258 * but don't care about the specific port number. 259 * source_object will be passed out in the various calls 260 * to accept to identify this particular source, which is 261 * useful if you're listening on multiple addresses and do 262 * different things depending on what address is connected to. 263 * Since 2.24 264 * Params: 265 * sourceObject = Optional GObject identifying this source. [allow-none] 266 * Returns: the port number, or 0 in case of failure. 267 * Throws: GException on failure. 268 */ 269 public ushort addAnyInetPort(ObjectG sourceObject) 270 { 271 // guint16 g_socket_listener_add_any_inet_port (GSocketListener *listener, GObject *source_object, GError **error); 272 GError* err = null; 273 274 auto p = g_socket_listener_add_any_inet_port(gSocketListener, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err); 275 276 if (err !is null) 277 { 278 throw new GException( new ErrorG(err) ); 279 } 280 281 return p; 282 } 283 284 /** 285 * Blocks waiting for a client to connect to any of the sockets added 286 * to the listener. Returns a GSocketConnection for the socket that was 287 * accepted. 288 * If source_object is not NULL it will be filled out with the source 289 * object specified when the corresponding socket or address was added 290 * to the listener. 291 * If cancellable is not NULL, then the operation can be cancelled by 292 * triggering the cancellable object from another thread. If the operation 293 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. 294 * Since 2.22 295 * Params: 296 * sourceObject = location where GObject pointer will be stored, or NULL. [out][transfer none][allow-none] 297 * cancellable = optional GCancellable object, NULL to ignore. [allow-none] 298 * Returns: a GSocketConnection on success, NULL on error. [transfer full] 299 * Throws: GException on failure. 300 */ 301 public SocketConnection accept(out ObjectG sourceObject, Cancellable cancellable) 302 { 303 // GSocketConnection * g_socket_listener_accept (GSocketListener *listener, GObject **source_object, GCancellable *cancellable, GError **error); 304 GObject* outsourceObject = null; 305 GError* err = null; 306 307 auto p = g_socket_listener_accept(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 308 309 if (err !is null) 310 { 311 throw new GException( new ErrorG(err) ); 312 } 313 314 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 315 316 if(p is null) 317 { 318 return null; 319 } 320 321 return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) p); 322 } 323 324 /** 325 * This is the asynchronous version of g_socket_listener_accept(). 326 * When the operation is finished callback will be 327 * called. You can then call g_socket_listener_accept_socket() 328 * to get the result of the operation. 329 * Since 2.22 330 * Params: 331 * cancellable = a GCancellable, or NULL. [allow-none] 332 * callback = a GAsyncReadyCallback. [scope async] 333 * userData = user data for the callback. [closure] 334 */ 335 public void acceptAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 336 { 337 // void g_socket_listener_accept_async (GSocketListener *listener, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); 338 g_socket_listener_accept_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 339 } 340 341 /** 342 * Finishes an async accept operation. See g_socket_listener_accept_async() 343 * Since 2.22 344 * Params: 345 * result = a GAsyncResult. 346 * sourceObject = Optional GObject identifying this source. [out][transfer none][allow-none] 347 * Returns: a GSocketConnection on success, NULL on error. [transfer full] 348 * Throws: GException on failure. 349 */ 350 public SocketConnection acceptFinish(AsyncResultIF result, ref ObjectG sourceObject) 351 { 352 // GSocketConnection * g_socket_listener_accept_finish (GSocketListener *listener, GAsyncResult *result, GObject **source_object, GError **error); 353 GObject* outsourceObject = (sourceObject is null) ? null : sourceObject.getObjectGStruct(); 354 GError* err = null; 355 356 auto p = g_socket_listener_accept_finish(gSocketListener, (result is null) ? null : result.getAsyncResultTStruct(), &outsourceObject, &err); 357 358 if (err !is null) 359 { 360 throw new GException( new ErrorG(err) ); 361 } 362 363 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 364 365 if(p is null) 366 { 367 return null; 368 } 369 370 return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) p); 371 } 372 373 /** 374 * Blocks waiting for a client to connect to any of the sockets added 375 * to the listener. Returns the GSocket that was accepted. 376 * If you want to accept the high-level GSocketConnection, not a GSocket, 377 * which is often the case, then you should use g_socket_listener_accept() 378 * instead. 379 * If source_object is not NULL it will be filled out with the source 380 * object specified when the corresponding socket or address was added 381 * to the listener. 382 * If cancellable is not NULL, then the operation can be cancelled by 383 * triggering the cancellable object from another thread. If the operation 384 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. 385 * Since 2.22 386 * Params: 387 * sourceObject = location where GObject pointer will be stored, or NULL. [out][transfer none][allow-none] 388 * cancellable = optional GCancellable object, NULL to ignore. [allow-none] 389 * Returns: a GSocket on success, NULL on error. [transfer full] 390 * Throws: GException on failure. 391 */ 392 public Socket acceptSocket(out ObjectG sourceObject, Cancellable cancellable) 393 { 394 // GSocket * g_socket_listener_accept_socket (GSocketListener *listener, GObject **source_object, GCancellable *cancellable, GError **error); 395 GObject* outsourceObject = null; 396 GError* err = null; 397 398 auto p = g_socket_listener_accept_socket(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 399 400 if (err !is null) 401 { 402 throw new GException( new ErrorG(err) ); 403 } 404 405 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 406 407 if(p is null) 408 { 409 return null; 410 } 411 412 return ObjectG.getDObject!(Socket)(cast(GSocket*) p); 413 } 414 415 /** 416 * This is the asynchronous version of g_socket_listener_accept_socket(). 417 * When the operation is finished callback will be 418 * called. You can then call g_socket_listener_accept_socket_finish() 419 * to get the result of the operation. 420 * Since 2.22 421 * Params: 422 * cancellable = a GCancellable, or NULL. [allow-none] 423 * callback = a GAsyncReadyCallback. [scope async] 424 * userData = user data for the callback. [closure] 425 */ 426 public void acceptSocketAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 427 { 428 // void g_socket_listener_accept_socket_async (GSocketListener *listener, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); 429 g_socket_listener_accept_socket_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 430 } 431 432 /** 433 * Finishes an async accept operation. See g_socket_listener_accept_socket_async() 434 * Since 2.22 435 * Params: 436 * result = a GAsyncResult. 437 * sourceObject = Optional GObject identifying this source. [out][transfer none][allow-none] 438 * Returns: a GSocket on success, NULL on error. [transfer full] 439 * Throws: GException on failure. 440 */ 441 public Socket acceptSocketFinish(AsyncResultIF result, ref ObjectG sourceObject) 442 { 443 // GSocket * g_socket_listener_accept_socket_finish (GSocketListener *listener, GAsyncResult *result, GObject **source_object, GError **error); 444 GObject* outsourceObject = (sourceObject is null) ? null : sourceObject.getObjectGStruct(); 445 GError* err = null; 446 447 auto p = g_socket_listener_accept_socket_finish(gSocketListener, (result is null) ? null : result.getAsyncResultTStruct(), &outsourceObject, &err); 448 449 if (err !is null) 450 { 451 throw new GException( new ErrorG(err) ); 452 } 453 454 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 455 456 if(p is null) 457 { 458 return null; 459 } 460 461 return ObjectG.getDObject!(Socket)(cast(GSocket*) p); 462 } 463 464 /** 465 * Closes all the sockets in the listener. 466 * Since 2.22 467 */ 468 public void close() 469 { 470 // void g_socket_listener_close (GSocketListener *listener); 471 g_socket_listener_close(gSocketListener); 472 } 473 474 /** 475 * Sets the listen backlog on the sockets in the listener. 476 * See g_socket_set_listen_backlog() for details 477 * Since 2.22 478 * Params: 479 * listenBacklog = an integer 480 */ 481 public void setBacklog(int listenBacklog) 482 { 483 // void g_socket_listener_set_backlog (GSocketListener *listener, int listen_backlog); 484 g_socket_listener_set_backlog(gSocketListener, listenBacklog); 485 } 486 }