1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5  *
6  * This software is provided 'as-is', without any express or implied warranty.
7  * In no event will the authors be held liable for any damages arising from the
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  *
24  *
25  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29  * This class mainly defines internal stuff to be used by derived classes.
30  *
31  * The only public features that it defines, and which is therefore common to
32  * all the socket classes, is the blocking state. All sockets can be set as
33  * blocking or non-blocking.
34  *
35  * In blocking mode, socket functions will hang until the operation completes,
36  * which means that the entire program (well, in fact the current thread if you
37  * use multiple ones) will be stuck waiting for your socket operation to
38  * complete.
39  *
40  * In non-blocking mode, all the socket functions will return immediately. If
41  * the socket is not ready to complete the requested operation, the function
42  * simply returns the proper status code (Socket.Status.NotReady).
43  *
44  * The default mode, which is blocking, is the one that is generally used, in
45  * combination with threads or selectors. The non-blocking mode is rather used
46  * in real-time applications that run an endless loop that can poll the socket
47  * often enough, and cannot afford blocking this loop.
48  *
49  * See_Also:
50  * $(TCPLISTENER_LINK), $(TCPSOCKET_LINK), $(UDPSOCKET_LINK)
51  */
52 module nudsfml.network.socket;
53 
54 /// Base interface for all the socket types.
55 interface Socket
56 {
57     //TODO: Add methods to this so that they can be overridden by the socket classes?
58 
59     ///Status codes that may be returned by socket functions.
60     enum Status
61     {
62         /// The socket has sent / received the data
63         Done,
64         /// The socket is not ready to send / receive data yet
65         NotReady,
66         /// The TCP socket has been disconnected
67         Disconnected,
68         /// An unexpected error happened
69         Error
70     }
71 
72     /// Special value that tells the system to pick any available port.
73     enum AnyPort = 0;
74 
75     /**
76      * Set the blocking state of the socket.
77      *
78      * In blocking mode, calls will not return until they have completed their
79      * task. For example, a call to `receive` in blocking mode won't return
80      * until some data was actually received.
81      *
82      * In non-blocking mode, calls will
83      * always return immediately, using the return code to signal whether there
84      * was data available or not. By default, all sockets are blocking.
85      *
86      * By default, all sockets are blocking.
87      *
88      * Params:
89      * blocking = true to set the socket as blocking, false for non-blocking
90      */
91     void setBlocking(bool blocking);
92 
93     /**
94      * Tell whether the socket is in blocking or non-blocking mode.
95      *
96      * Returns: true if the socket is blocking, false otherwise.
97      */
98     bool isBlocking() const;
99 }