]> git.sesse.net Git - casparcg/blob - dependencies64/sfml/include/SFML/Network/SocketUDP.hpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / dependencies64 / sfml / include / SFML / Network / SocketUDP.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\r
6 // This software is provided 'as-is', without any express or implied warranty.\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.\r
8 //\r
9 // Permission is granted to anyone to use this software for any purpose,\r
10 // including commercial applications, and to alter it and redistribute it freely,\r
11 // subject to the following restrictions:\r
12 //\r
13 // 1. The origin of this software must not be misrepresented;\r
14 //    you must not claim that you wrote the original software.\r
15 //    If you use this software in a product, an acknowledgment\r
16 //    in the product documentation would be appreciated but is not required.\r
17 //\r
18 // 2. Altered source versions must be plainly marked as such,\r
19 //    and must not be misrepresented as being the original software.\r
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_SOCKETUDP_HPP\r
26 #define SFML_SOCKETUDP_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/Network/SocketHelper.hpp>\r
32 #include <vector>\r
33 \r
34 \r
35 namespace sf\r
36 {\r
37 class Packet;\r
38 class IPAddress;\r
39 template <typename> class Selector;\r
40 \r
41 ////////////////////////////////////////////////////////////\r
42 /// SocketUDP wraps a socket using UDP protocol to\r
43 /// send data fastly (but with less safety)\r
44 ////////////////////////////////////////////////////////////\r
45 class SFML_API SocketUDP\r
46 {\r
47 public :\r
48 \r
49     ////////////////////////////////////////////////////////////\r
50     /// Default constructor\r
51     ///\r
52     ////////////////////////////////////////////////////////////\r
53     SocketUDP();\r
54 \r
55     ////////////////////////////////////////////////////////////\r
56     /// Change the blocking state of the socket.\r
57     /// The default behaviour of a socket is blocking\r
58     ///\r
59     /// \param Blocking : Pass true to set the socket as blocking, or false for non-blocking\r
60     ///\r
61     ////////////////////////////////////////////////////////////\r
62     void SetBlocking(bool Blocking);\r
63 \r
64     ////////////////////////////////////////////////////////////\r
65     /// Bind the socket to a specific port\r
66     ///\r
67     /// \param Port : Port to bind the socket to\r
68     ///\r
69     /// \return True if operation has been successful\r
70     ///\r
71     ////////////////////////////////////////////////////////////\r
72     bool Bind(unsigned short Port);\r
73 \r
74     ////////////////////////////////////////////////////////////\r
75     /// Unbind the socket from its previous port, if any\r
76     ///\r
77     /// \return True if operation has been successful\r
78     ///\r
79     ////////////////////////////////////////////////////////////\r
80     bool Unbind();\r
81 \r
82     ////////////////////////////////////////////////////////////\r
83     /// Send an array of bytes\r
84     ///\r
85     /// \param Data :    Pointer to the bytes to send\r
86     /// \param Size :    Number of bytes to send\r
87     /// \param Address : Address of the computer to send the packet to\r
88     /// \param Port :    Port to send the data to\r
89     ///\r
90     /// \return Status code\r
91     ///\r
92     ////////////////////////////////////////////////////////////\r
93     Socket::Status Send(const char* Data, std::size_t Size, const IPAddress& Address, unsigned short Port);\r
94 \r
95     ////////////////////////////////////////////////////////////\r
96     /// Receive an array of bytes.\r
97     /// This function will block if the socket is blocking\r
98     ///\r
99     /// \param Data :         Pointer to a byte array to fill (make sure it is big enough)\r
100     /// \param MaxSize :      Maximum number of bytes to read\r
101     /// \param SizeReceived : Number of bytes received\r
102     /// \param Address :      Address of the computer which sent the data\r
103     /// \param Port :         Port on which the remote computer sent the data\r
104     ///\r
105     /// \return Status code\r
106     ///\r
107     ////////////////////////////////////////////////////////////\r
108     Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived, IPAddress& Address, unsigned short& Port);\r
109 \r
110     ////////////////////////////////////////////////////////////\r
111     /// Send a packet of data\r
112     ///\r
113     /// \param PacketToSend : Packet to send\r
114     /// \param Address :      Address of the computer to send the packet to\r
115     /// \param Port :         Port to send the data to\r
116     ///\r
117     /// \return Status code\r
118     ///\r
119     ////////////////////////////////////////////////////////////\r
120     Socket::Status Send(Packet& PacketToSend, const IPAddress& Address, unsigned short Port);\r
121 \r
122     ////////////////////////////////////////////////////////////\r
123     /// Receive a packet.\r
124     /// This function will block if the socket is blocking\r
125     ///\r
126     /// \param PacketToReceive : Packet to fill with received data\r
127     /// \param Address :         Address of the computer which sent the packet\r
128     /// \param Port :            Port on which the remote computer sent the data\r
129     ///\r
130     /// \return Status code\r
131     ///\r
132     ////////////////////////////////////////////////////////////\r
133     Socket::Status Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port);\r
134 \r
135     ////////////////////////////////////////////////////////////\r
136     /// Close the socket\r
137     ///\r
138     /// \return True if operation has been successful\r
139     ///\r
140     ////////////////////////////////////////////////////////////\r
141     bool Close();\r
142 \r
143     ////////////////////////////////////////////////////////////\r
144     /// Check if the socket is in a valid state ; this function\r
145     /// can be called any time to check if the socket is OK\r
146     ///\r
147     /// \return True if the socket is valid\r
148     ///\r
149     ////////////////////////////////////////////////////////////\r
150     bool IsValid() const;\r
151 \r
152     ////////////////////////////////////////////////////////////\r
153     /// Get the port the socket is currently bound to\r
154     ///\r
155     /// \return Current port (0 means the socket is not bound)\r
156     ///\r
157     ////////////////////////////////////////////////////////////\r
158     unsigned short GetPort() const;\r
159 \r
160     ////////////////////////////////////////////////////////////\r
161     /// Comparison operator ==\r
162     ///\r
163     /// \param Other : Socket to compare\r
164     ///\r
165     /// \return True if *this == Other\r
166     ///\r
167     ////////////////////////////////////////////////////////////\r
168     bool operator ==(const SocketUDP& Other) const;\r
169 \r
170     ////////////////////////////////////////////////////////////\r
171     /// Comparison operator !=\r
172     ///\r
173     /// \param Other : Socket to compare\r
174     ///\r
175     /// \return True if *this != Other\r
176     ///\r
177     ////////////////////////////////////////////////////////////\r
178     bool operator !=(const SocketUDP& Other) const;\r
179 \r
180     ////////////////////////////////////////////////////////////\r
181     /// Comparison operator <.\r
182     /// Provided for compatibility with standard containers, as\r
183     /// comparing two sockets doesn't make much sense...\r
184     ///\r
185     /// \param Other : Socket to compare\r
186     ///\r
187     /// \return True if *this < Other\r
188     ///\r
189     ////////////////////////////////////////////////////////////\r
190     bool operator <(const SocketUDP& Other) const;\r
191 \r
192 private :\r
193 \r
194     friend class Selector<SocketUDP>;\r
195 \r
196     ////////////////////////////////////////////////////////////\r
197     /// Construct the socket from a socket descriptor\r
198     /// (for internal use only)\r
199     ///\r
200     /// \param Descriptor : Socket descriptor\r
201     ///\r
202     ////////////////////////////////////////////////////////////\r
203     SocketUDP(SocketHelper::SocketType Descriptor);\r
204 \r
205     ////////////////////////////////////////////////////////////\r
206     /// Create the socket\r
207     ///\r
208     /// \param Descriptor : System socket descriptor to use (0 by default -- create a new socket)\r
209     ///\r
210     ////////////////////////////////////////////////////////////\r
211     void Create(SocketHelper::SocketType Descriptor = 0);\r
212 \r
213     ////////////////////////////////////////////////////////////\r
214     // Member data\r
215     ////////////////////////////////////////////////////////////\r
216     SocketHelper::SocketType mySocket;            ///< Socket identifier\r
217     unsigned short           myPort;              ///< Port to which the socket is bound\r
218     Uint32                   myPendingHeader;     ///< Data of the current pending packet header, if any\r
219     Uint32                   myPendingHeaderSize; ///< Size of the current pending packet header, if any\r
220     std::vector<char>        myPendingPacket;     ///< Data of the current pending packet, if any\r
221     Int32                    myPendingPacketSize; ///< Size of the current pending packet, if any\r
222     bool                     myIsBlocking;        ///< Is the socket blocking or non-blocking ?\r
223 };\r
224 \r
225 } // namespace sf\r
226 \r
227 \r
228 #endif // SFML_SOCKETUDP_HPP\r