]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/Network/SocketTCP.hpp
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / Network / SocketTCP.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_SOCKETTCP_HPP\r
26 #define SFML_SOCKETTCP_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 /// SocketTCP wraps a socket using TCP protocol to\r
43 /// send data safely (but a bit slower)\r
44 ////////////////////////////////////////////////////////////\r
45 class SFML_API SocketTCP\r
46 {\r
47 public :\r
48 \r
49     ////////////////////////////////////////////////////////////\r
50     /// Default constructor\r
51     ///\r
52     ////////////////////////////////////////////////////////////\r
53     SocketTCP();\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     /// Connect to another computer on a specified port\r
66     ///\r
67     /// \param Port :        Port to use for transfers (warning : ports < 1024 are reserved)\r
68     /// \param HostAddress : IP Address of the host to connect to\r
69     /// \param Timeout :     Maximum time to wait, in seconds (0 by default : no timeout) (this parameter is ignored for non-blocking sockets)\r
70     ///\r
71     /// \return True if operation has been successful\r
72     ///\r
73     ////////////////////////////////////////////////////////////\r
74     Socket::Status Connect(unsigned short Port, const IPAddress& HostAddress, float Timeout = 0.f);\r
75 \r
76     ////////////////////////////////////////////////////////////\r
77     /// Listen to a specified port for incoming data or connections\r
78     ///\r
79     /// \param Port : Port to listen to\r
80     ///\r
81     /// \return True if operation has been successful\r
82     ///\r
83     ////////////////////////////////////////////////////////////\r
84     bool Listen(unsigned short Port);\r
85 \r
86     ////////////////////////////////////////////////////////////\r
87     /// Wait for a connection (must be listening to a port).\r
88     /// This function will block if the socket is blocking\r
89     ///\r
90     /// \param Connected : Socket containing the connection with the connected client\r
91     /// \param Address :   Pointer to an address to fill with client infos (NULL by default)\r
92     ///\r
93     /// \return Status code\r
94     ///\r
95     ////////////////////////////////////////////////////////////\r
96     Socket::Status Accept(SocketTCP& Connected, IPAddress* Address = NULL);\r
97 \r
98     ////////////////////////////////////////////////////////////\r
99     /// Send an array of bytes to the host (must be connected first)\r
100     ///\r
101     /// \param Data : Pointer to the bytes to send\r
102     /// \param Size : Number of bytes to send\r
103     ///\r
104     /// \return Status code\r
105     ///\r
106     ////////////////////////////////////////////////////////////\r
107     Socket::Status Send(const char* Data, std::size_t Size);\r
108 \r
109     ////////////////////////////////////////////////////////////\r
110     /// Receive an array of bytes from the host (must be connected first).\r
111     /// This function will block if the socket is blocking\r
112     ///\r
113     /// \param Data :         Pointer to a byte array to fill (make sure it is big enough)\r
114     /// \param MaxSize :      Maximum number of bytes to read\r
115     /// \param SizeReceived : Number of bytes received\r
116     ///\r
117     /// \return Status code\r
118     ///\r
119     ////////////////////////////////////////////////////////////\r
120     Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived);\r
121 \r
122     ////////////////////////////////////////////////////////////\r
123     /// Send a packet of data to the host (must be connected first)\r
124     ///\r
125     /// \param PacketToSend : Packet to send\r
126     ///\r
127     /// \return Status code\r
128     ///\r
129     ////////////////////////////////////////////////////////////\r
130     Socket::Status Send(Packet& PacketToSend);\r
131 \r
132     ////////////////////////////////////////////////////////////\r
133     /// Receive a packet from the host (must be connected first).\r
134     /// This function will block if the socket is blocking\r
135     ///\r
136     /// \param PacketToReceive : Packet to fill with received data\r
137     ///\r
138     /// \return Status code\r
139     ///\r
140     ////////////////////////////////////////////////////////////\r
141     Socket::Status Receive(Packet& PacketToReceive);\r
142 \r
143     ////////////////////////////////////////////////////////////\r
144     /// Close the socket\r
145     ///\r
146     /// \return True if operation has been successful\r
147     ///\r
148     ////////////////////////////////////////////////////////////\r
149     bool Close();\r
150 \r
151     ////////////////////////////////////////////////////////////\r
152     /// Check if the socket is in a valid state ; this function\r
153     /// can be called any time to check if the socket is OK\r
154     ///\r
155     /// \return True if the socket is valid\r
156     ///\r
157     ////////////////////////////////////////////////////////////\r
158     bool IsValid() 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 SocketTCP& 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 SocketTCP& 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 SocketTCP& Other) const;\r
191 \r
192 private :\r
193 \r
194     friend class Selector<SocketTCP>;\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     SocketTCP(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 descriptor\r
217     Uint32                   myPendingHeader;     ///< Data of the current pending packet header, if any\r
218     Uint32                   myPendingHeaderSize; ///< Size of the current pending packet header, if any\r
219     std::vector<char>        myPendingPacket;     ///< Data of the current pending packet, if any\r
220     Int32                    myPendingPacketSize; ///< Size of the current pending packet, if any\r
221     bool                     myIsBlocking;        ///< Is the socket blocking or non-blocking ?\r
222 };\r
223 \r
224 } // namespace sf\r
225 \r
226 \r
227 #endif // SFML_SOCKETTCP_HPP\r