Documentation for module: Network

Former-commit-id: d36042138d0883eb118cc9a70f94f3522214dd46
This commit is contained in:
Gawaboumga
2016-05-30 14:22:31 +02:00
parent 96b958d655
commit 0a99058c4d
25 changed files with 1368 additions and 25 deletions

View File

@@ -20,6 +20,22 @@
namespace Nz
{
/*!
* \ingroup network
* \class Nz::TcpClient
* \brief Network class that represents a client in a TCP connection
*/
/*!
* \brief Connects to the IpAddress
* \return State of the socket
*
* \param remoteAddress Address to connect to
*
* \remark Produces a NazaraAssert if remote is invalid
* \remark Produces a NazaraAssert if remote's port is not specified
*/
SocketState TcpClient::Connect(const IpAddress& remoteAddress)
{
NazaraAssert(remoteAddress.IsValid(), "Invalid remote address");
@@ -45,6 +61,17 @@ namespace Nz
return state;
}
/*!
* \brief Connects to the hostname
* \return State of the socket
*
* \param hostName Hostname of the remote
* \param protocol Net protocol to use
* \param service Specify the protocol used
* \param error Optional argument to get the error
*/
SocketState TcpClient::Connect(const String& hostName, NetProtocol protocol, const String& service, ResolveError* error)
{
UpdateState(SocketState_Resolving);
@@ -73,6 +100,14 @@ namespace Nz
return Connect(hostnameAddress);
}
/*!
* \brief Enables low delay in emitting
*
* \param lowDelay Should low delay be used
*
* \remark This may produce lag
*/
void TcpClient::EnableLowDelay(bool lowDelay)
{
if (m_isLowDelayEnabled != lowDelay)
@@ -84,6 +119,14 @@ namespace Nz
}
}
/*!
* \brief Enables the keep alive flag
*
* \param keepAlive Should the connection be kept alive
* \param msTime Time in milliseconds before expiration
* \param msInterval Interval in milliseconds between two pings
*/
void TcpClient::EnableKeepAlive(bool keepAlive, UInt64 msTime, UInt64 msInterval)
{
if (m_isKeepAliveEnabled != keepAlive || m_keepAliveTime != msTime || m_keepAliveInterval != msInterval)
@@ -97,22 +140,51 @@ namespace Nz
}
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if there is no more available bytes
*/
bool TcpClient::EndOfStream() const
{
return QueryAvailableBytes() == 0;
}
/*!
* \brief Gets the position of the cursor
* \return 0
*
* \remark Produces a NazaraError because it is a special stream
*/
UInt64 TcpClient::GetCursorPos() const
{
NazaraError("GetCursorPos() cannot be used on sequential streams");
return 0;
}
/*!
* \brief Gets the size of the raw memory available
* \return Size of the memory available
*/
UInt64 TcpClient::GetSize() const
{
return QueryAvailableBytes();
}
/*!
* \brief Receives the data available
* \return true If data received
*
* \param buffer Raw memory to write
* \param size Size of the buffer
* \param received Optional argument to get the number of bytes received
*
* \remark Produces a NazaraAssert if socket is invalid
* \remark Produces a NazaraAssert if buffer and its size is invalid
*/
bool TcpClient::Receive(void* buffer, std::size_t size, std::size_t* received)
{
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
@@ -142,6 +214,17 @@ namespace Nz
return true;
}
/*!
* \brief Receives the packet available
* \return true If packet received
*
* \param packet Packet to receive
*
* \remark Produces a NazaraAssert if packet is invalid
* \remark Produces a NazaraAssert if packet size is inferior to the header size
* \remark Produces a NazaraWarning if packet's header is invalid
*/
bool TcpClient::ReceivePacket(NetPacket* packet)
{
//TODO: Every packet requires at least two Receive call, using an internal buffer of a fixed size would prevent this
@@ -157,6 +240,7 @@ namespace Nz
m_pendingPacket.received += received;
//TODO: Should never happen in production !
NazaraAssert(m_pendingPacket.received <= NetPacket::HeaderSize, "Received more data than header size");
if (m_pendingPacket.received >= NetPacket::HeaderSize)
{
@@ -185,6 +269,7 @@ namespace Nz
m_pendingPacket.received += received;
//TODO: Should never happen in production !
NazaraAssert(m_pendingPacket.received <= packetSize, "Received more data than packet size");
if (m_pendingPacket.received >= packetSize)
{
@@ -202,6 +287,19 @@ namespace Nz
return false;
}
/*!
* \brief Sends the data available
* \return true If data sended
*
* \param buffer Raw memory to read
* \param size Size of the buffer
* \param sent Optional argument to get the number of bytes sent
*
* \remark Large sending are handled, you do not need to call this multiple time
* \remark Produces a NazaraAssert if socket is invalid
* \remark Produces a NazaraAssert if buffer and its size is invalid
*/
bool TcpClient::Send(const void* buffer, std::size_t size, std::size_t* sent)
{
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
@@ -244,6 +342,15 @@ namespace Nz
return true;
}
/*!
* \brief Sends the packet available
* \return true If packet sent
*
* \param packet Packet to send
*
* \remark Produces a NazaraError if packet could not be prepared for sending
*/
bool TcpClient::SendPacket(const NetPacket& packet)
{
std::size_t size = 0;
@@ -258,12 +365,30 @@ namespace Nz
return Send(ptr, size, nullptr);
}
/*!
* \brief Sets the position of the cursor
* \return false
*
* \param offset Offset according to the beginning of the stream
*
* \remark Produces a NazaraError because it is a special stream
*/
bool TcpClient::SetCursorPos(UInt64 offset)
{
NazaraError("SetCursorPos() cannot be used on sequential streams");
return false;
}
/*!
* \brief Waits for being connected before time out
* \return true If connection is successful
*
* \param msTimeout Time in milliseconds before time out
*
* \remark Produces a NazaraAssert if socket is invalid
*/
bool TcpClient::WaitForConnected(UInt64 msTimeout)
{
switch (m_state)
@@ -308,10 +433,18 @@ namespace Nz
return false;
}
/*!
* \brief Flushes the stream
*/
void TcpClient::FlushStream()
{
}
/*!
* \brief Operation to do when closing socket
*/
void TcpClient::OnClose()
{
AbstractSocket::OnClose();
@@ -320,6 +453,12 @@ namespace Nz
m_peerAddress = IpAddress::Invalid;
}
/*!
* \brief Operation to do when opening socket
*
* \remark Produces a NazaraWarning if delay mode or keep alive failed
*/
void TcpClient::OnOpened()
{
AbstractSocket::OnOpened();
@@ -336,6 +475,16 @@ namespace Nz
m_openMode = OpenMode_ReadWrite;
}
/*!
* \brief Reads blocks
* \return Number of blocks read
*
* \param buffer Preallocated buffer to contain information read
* \param size Size of the read and thus of the buffer
*
* \remark Produces a NazaraAssert if socket is invalid
*/
std::size_t TcpClient::ReadBlock(void* buffer, std::size_t size)
{
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
@@ -357,6 +506,13 @@ namespace Nz
return received;
}
/*!
* \brief Resets the connection with a new socket and a peer address
*
* \param handle Socket to connect
* \param peerAddress Address to connect to
*/
void TcpClient::Reset(SocketHandle handle, const IpAddress& peerAddress)
{
Open(handle);
@@ -365,8 +521,20 @@ namespace Nz
UpdateState(SocketState_Connected);
}
/*!
* \brief Writes blocks
* \return Number of blocks written
*
* \param buffer Preallocated buffer containing information to write
* \param size Size of the writting and thus of the buffer
*
* \remark Produces a NazaraAssert if buffer is nullptr
* \remark Produces a NazaraAssert if socket is invalid
*/
std::size_t TcpClient::WriteBlock(const void* buffer, std::size_t size)
{
NazaraAssert(buffer, "Invalid buffer");
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
CallOnExit restoreBlocking;