SocketTCP.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@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 use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef SFML_SOCKETTCP_HPP
26#define SFML_SOCKETTCP_HPP
27
29// Headers
31#include <SFML/Network/SocketHelper.hpp>
32#include <vector>
33
34
35namespace sf
36{
37class Packet;
38class IPAddress;
39template <typename> class Selector;
40
45class SFML_API SocketTCP
46{
47public :
48
53 SocketTCP();
54
62 void SetBlocking(bool Blocking);
63
74 Socket::Status Connect(unsigned short Port, const IPAddress& HostAddress, float Timeout = 0.f);
75
84 bool Listen(unsigned short Port);
85
96 Socket::Status Accept(SocketTCP& Connected, IPAddress* Address = NULL);
97
107 Socket::Status Send(const char* Data, std::size_t Size);
108
120 Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived);
121
130 Socket::Status Send(Packet& PacketToSend);
131
141 Socket::Status Receive(Packet& PacketToReceive);
142
149 bool Close();
150
158 bool IsValid() const;
159
168 bool operator ==(const SocketTCP& Other) const;
169
178 bool operator !=(const SocketTCP& Other) const;
179
190 bool operator <(const SocketTCP& Other) const;
191
192private :
193
194 friend class Selector<SocketTCP>;
195
203 SocketTCP(SocketHelper::SocketType Descriptor);
204
211 void Create(SocketHelper::SocketType Descriptor = 0);
212
214 // Member data
216 SocketHelper::SocketType mySocket;
217 Uint32 myPendingHeader;
218 Uint32 myPendingHeaderSize;
219 std::vector<char> myPendingPacket;
220 Int32 myPendingPacketSize;
221 bool myIsBlocking;
222};
223
224} // namespace sf
225
226
227#endif // SFML_SOCKETTCP_HPP
IPAddress provides easy manipulation of IP v4 addresses.
Definition IPAddress.hpp:43
Packet wraps data to send / to receive through the network.
Definition Packet.hpp:42
Selector allow reading from multiple sockets without blocking.
Definition Selector.hpp:45
SocketTCP()
Default constructor.
Definition SocketTCP.cpp:47
bool IsValid() const
Check if the socket is in a valid state ; this function can be called any time to check if the socket...
bool Listen(unsigned short Port)
Listen to a specified port for incoming data or connections.
Socket::Status Receive(char *Data, std::size_t MaxSize, std::size_t &SizeReceived)
Receive an array of bytes from the host (must be connected first).
Socket::Status Send(const char *Data, std::size_t Size)
Send an array of bytes to the host (must be connected first).
bool Close()
Close the socket.
Socket::Status Accept(SocketTCP &Connected, IPAddress *Address=NULL)
Wait for a connection (must be listening to a port).
void SetBlocking(bool Blocking)
Change the blocking state of the socket.
Definition SocketTCP.cpp:56
Socket::Status Connect(unsigned short Port, const IPAddress &HostAddress, float Timeout=0.f)
Connect to another computer on a specified port.
Definition SocketTCP.cpp:70