TcpSocket.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
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 CPP3DS_TCPSOCKET_HPP
26 #define CPP3DS_TCPSOCKET_HPP
27 
29 // Headers
31 #include <cpp3ds/Network/Socket.hpp>
32 #include <cpp3ds/System/Time.hpp>
33 
34 
35 namespace cpp3ds
36 {
37 class TcpListener;
38 class IpAddress;
39 class Packet;
40 
45 class TcpSocket : public Socket
46 {
47 public:
48 
53  TcpSocket();
54 
65  unsigned short getLocalPort() const;
66 
79 
91  unsigned short getRemotePort() const;
92 
110  Status connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout = Time::Zero);
111 
121  void disconnect();
122 
139  Status send(const void* data, std::size_t size);
140 
155  Status send(const void* data, std::size_t size, std::size_t& sent);
156 
173  Status receive(void* data, std::size_t size, std::size_t& received);
174 
191  Status send(Packet& packet);
192 
207  Status receive(Packet& packet);
208 
209 private:
210 
211  friend class TcpListener;
212 
217  struct PendingPacket
218  {
219  PendingPacket();
220 
221  Uint32 Size;
222  std::size_t SizeReceived;
223  std::vector<char> Data;
224  };
225 
227  // Member data
229  PendingPacket m_pendingPacket;
230 };
231 
232 } // namespace cpp3ds
233 
234 
235 #endif // CPP3DS_TCPSOCKET_HPP
236 
237 
Encapsulate an IPv4 network address.
Definition: IpAddress.hpp:43
unsigned short getLocalPort() const
Get the port to which the socket is bound locally.
Utility class to build blocks of data to transfer over the network.
Definition: Packet.hpp:47
Base class for all the socket types.
Definition: Socket.hpp:44
void disconnect()
Disconnect the socket from its remote peer.
TcpSocket()
Default constructor.
Represents a time value.
Definition: Time.hpp:37
Status send(const void *data, std::size_t size)
Send raw data to the remote peer.
Status connect(const IpAddress &remoteAddress, unsigned short remotePort, Time timeout=Time::Zero)
Connect the socket to a remote peer.
Status receive(void *data, std::size_t size, std::size_t &received)
Receive raw data from the remote peer.
unsigned short getRemotePort() const
Get the port of the connected peer to which the socket is connected.
IpAddress getRemoteAddress() const
Get the address of the connected peer.
Specialized socket using the TCP protocol.
Definition: TcpSocket.hpp:45
Status
Status codes that may be returned by socket functions.
Definition: Socket.hpp:52
Socket that listens to new TCP connections.
Definition: TcpListener.hpp:42
static const Time Zero
Predefined "zero" time value.
Definition: Time.hpp:82