mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-08-11 01:01:25 +03:00
Added setSendTimeout and setReceiveTimeout to TcpSocket and UdpSocket.
--HG-- branch : dev
This commit is contained in:
@@ -48,8 +48,7 @@ class EE_API Socket : NonCopyable {
|
||||
bool isBlocking() const;
|
||||
protected :
|
||||
/** @brief Types of protocols that the socket can use */
|
||||
enum Type
|
||||
{
|
||||
enum Type {
|
||||
Tcp, ///< TCP protocol
|
||||
Udp ///< UDP protocol
|
||||
};
|
||||
|
||||
@@ -113,6 +113,14 @@ class EE_API TcpSocket : public Socket {
|
||||
** @see Send */
|
||||
virtual Status receive(Packet& packet);
|
||||
|
||||
/** Set the send timeout. Only callable after connect ( after the socket
|
||||
** has been initialized ). */
|
||||
void setSendTimeout(SocketHandle sock, const Time& timeout);
|
||||
|
||||
/** Set the receive timeout Only callable after connect ( after the socket
|
||||
** has been initialized ). */
|
||||
void setReceiveTimeout(SocketHandle sock, const Time& timeout);
|
||||
|
||||
private:
|
||||
|
||||
friend class TcpListener;
|
||||
|
||||
@@ -98,6 +98,14 @@ class EE_API UdpSocket : public Socket {
|
||||
** @return Status code
|
||||
** @see Send */
|
||||
Status receive(Packet& packet, IpAddress& remoteAddress, unsigned short& remotePort);
|
||||
|
||||
/** Set the send timeout. Only callable after bind ( after the socket
|
||||
** has been initialized ). */
|
||||
void setSendTimeout(SocketHandle sock, const Time& timeout);
|
||||
|
||||
/** Set the receive timeout Only callable after bind ( after the socket
|
||||
** has been initialized ). */
|
||||
void setReceiveTimeout(SocketHandle sock, const Time& timeout);
|
||||
private:
|
||||
// Member data
|
||||
std::vector<char> mBuffer; ///< Temporary buffer holding the received data in Receive(Packet)
|
||||
|
||||
@@ -57,6 +57,20 @@ Socket::Status SocketImpl::getErrorStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
void SocketImpl::setSendTimeout(SocketHandle sock, const Time& timeout) {
|
||||
struct timeval time;
|
||||
time.tv_sec = static_cast<long>(timeout.asMicroseconds() / 1000000);
|
||||
time.tv_usec = static_cast<long>(timeout.asMicroseconds() % 1000000);
|
||||
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&time, sizeof time);
|
||||
}
|
||||
|
||||
void SocketImpl::setReceiveTimeout(SocketHandle sock, const Time & timeout) {
|
||||
struct timeval time;
|
||||
time.tv_sec = static_cast<long>(timeout.asMicroseconds() / 1000000);
|
||||
time.tv_usec = static_cast<long>(timeout.asMicroseconds() % 1000000);
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&time, sizeof time);
|
||||
}
|
||||
|
||||
}}}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#if defined( EE_PLATFORM_POSIX )
|
||||
|
||||
#include <eepp/system/time.hpp>
|
||||
#include <eepp/network/socket.hpp>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -14,6 +15,8 @@
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace EE::System;
|
||||
|
||||
namespace EE { namespace Network { namespace Private {
|
||||
|
||||
/** @brief Helper class implementing all the non-portable socket stuff; this is the Unix version */
|
||||
@@ -44,6 +47,12 @@ class SocketImpl {
|
||||
/** Get the last socket error status
|
||||
** @return Status corresponding to the last socket error */
|
||||
static Socket::Status getErrorStatus();
|
||||
|
||||
/** Set the send timeout */
|
||||
static void setSendTimeout(SocketHandle sock, const Time& timeout);
|
||||
|
||||
/** Set the receive timeout */
|
||||
static void setReceiveTimeout(SocketHandle sock, const Time& timeout);
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
@@ -42,6 +42,16 @@ Socket::Status SocketImpl::getErrorStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
void SocketImpl::setSendTimeout(SocketHandle sock, const Time& timeout) {
|
||||
DWORD time = timeout.asMilliseconds();
|
||||
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&time, sizeof time);
|
||||
}
|
||||
|
||||
void SocketImpl::setReceiveTimeout(SocketHandle sock, const Time & timeout) {
|
||||
DWORD time = timeout.asMilliseconds();
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&time, sizeof time);
|
||||
}
|
||||
|
||||
/** Windows needs some initialization and cleanup to get
|
||||
** sockets working properly... so let's create a class that will do it automatically */
|
||||
struct SocketInitializer
|
||||
|
||||
@@ -47,6 +47,12 @@ class SocketImpl {
|
||||
/** Get the last socket error status
|
||||
** @return Status corresponding to the last socket error */
|
||||
static Socket::Status getErrorStatus();
|
||||
|
||||
/** Set the send timeout */
|
||||
static void setSendTimeout(SocketHandle sock, const Time& timeout);
|
||||
|
||||
/** Set the receive timeout */
|
||||
static void setReceiveTimeout(SocketHandle sock, const Time& timeout);
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
@@ -316,6 +316,18 @@ Socket::Status TcpSocket::receive(Packet& packet) {
|
||||
return Done;
|
||||
}
|
||||
|
||||
void TcpSocket::setSendTimeout(SocketHandle sock, const Time& timeout) {
|
||||
if (getHandle() != Private::SocketImpl::invalidSocket()) {
|
||||
Private::SocketImpl::setSendTimeout(getHandle(), timeout);
|
||||
}
|
||||
}
|
||||
|
||||
void TcpSocket::setReceiveTimeout(SocketHandle sock, const Time& timeout) {
|
||||
if (getHandle() != Private::SocketImpl::invalidSocket()) {
|
||||
Private::SocketImpl::setReceiveTimeout(getHandle(), timeout);
|
||||
}
|
||||
}
|
||||
|
||||
TcpSocket::PendingPacket::PendingPacket() :
|
||||
Size (0),
|
||||
SizeReceived(0),
|
||||
|
||||
@@ -61,8 +61,7 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, const IpAddre
|
||||
create();
|
||||
|
||||
// Make sure that all the data will fit in one datagram
|
||||
if (size > MaxDatagramSize)
|
||||
{
|
||||
if (size > MaxDatagramSize) {
|
||||
eePRINTL( "Cannot send data over the network (the number of bytes to send is greater than UdpSocket::MaxDatagramSize)" );
|
||||
return Error;
|
||||
}
|
||||
@@ -143,5 +142,16 @@ Socket::Status UdpSocket::receive(Packet& packet, IpAddress& remoteAddress, unsi
|
||||
return status;
|
||||
}
|
||||
|
||||
void UdpSocket::setSendTimeout(SocketHandle sock, const Time& timeout) {
|
||||
if (getHandle() != Private::SocketImpl::invalidSocket()) {
|
||||
Private::SocketImpl::setSendTimeout(getHandle(), timeout);
|
||||
}
|
||||
}
|
||||
|
||||
void UdpSocket::setReceiveTimeout(SocketHandle sock, const Time& timeout) {
|
||||
if (getHandle() != Private::SocketImpl::invalidSocket()) {
|
||||
Private::SocketImpl::setReceiveTimeout(getHandle(), timeout);
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user