Socket::Poll does not need a callback

This commit is contained in:
Benjamin Sergeant 2019-04-18 16:42:44 -07:00
parent 309b5ee1b3
commit 91106b7456
3 changed files with 87 additions and 77 deletions

View File

@ -45,17 +45,14 @@ namespace ix
close(); close();
} }
void Socket::poll(const OnPollCallback& onPollCallback, int timeoutSecs) PollResultType Socket::poll(int timeoutSecs)
{ {
if (_sockfd == -1) if (_sockfd == -1)
{ {
if (onPollCallback) onPollCallback(PollResultType::Error); return PollResultType::Error;
return;
} }
PollResultType pollResult = isReadyToRead(1000 * timeoutSecs); return isReadyToRead(1000 * timeoutSecs);
if (onPollCallback) onPollCallback(pollResult);
} }
PollResultType Socket::select(bool readyToRead, int timeoutMs) PollResultType Socket::select(bool readyToRead, int timeoutMs)

View File

@ -37,8 +37,6 @@ namespace ix
class Socket { class Socket {
public: public:
using OnPollCallback = std::function<void(PollResultType)>;
Socket(int fd = -1); Socket(int fd = -1);
virtual ~Socket(); virtual ~Socket();
bool init(std::string& errorMsg); bool init(std::string& errorMsg);
@ -46,8 +44,7 @@ namespace ix
void configure(); void configure();
// Functions to check whether there is activity on the socket // Functions to check whether there is activity on the socket
void poll(const OnPollCallback& onPollCallback, PollResultType poll(int timeoutSecs = kDefaultPollTimeout);
int timeoutSecs = kDefaultPollTimeout);
bool wakeUpFromPoll(uint8_t wakeUpCode); bool wakeUpFromPoll(uint8_t wakeUpCode);
PollResultType isReadyToWrite(int timeoutMs); PollResultType isReadyToWrite(int timeoutMs);

View File

@ -232,9 +232,15 @@ namespace ix
void WebSocketTransport::poll() void WebSocketTransport::poll()
{ {
_socket->poll( PollResultType pollResult = _socket->poll(_pingIntervalOrTimeoutGCDSecs);
[this](PollResultType pollResult)
if (_readyState == OPEN)
{ {
// if (1) ping timeout is enabled and (2) duration since last received ping response (PONG)
// exceeds the maximum delay, then close the connection
if (pingTimeoutExceeded())
{
<<<<<<< HEAD
if (_readyState == OPEN) if (_readyState == OPEN)
{ {
// if (1) ping timeout is enabled and (2) duration since last received ping response (PONG) // if (1) ping timeout is enabled and (2) duration since last received ping response (PONG)
@ -252,6 +258,18 @@ namespace ix
sendPing(ss.str()); sendPing(ss.str());
} }
} }
=======
close(1011, "Ping timeout");
}
// If (1) ping is enabled and no ping has been sent for a duration
// exceeding our ping interval, send a ping to the server.
else if (pingIntervalExceeded())
{
std::stringstream ss;
ss << kPingMessage << "::" << _pingIntervalSecs << "s";
sendPing(ss.str());
}
}
// Make sure we send all the buffered data // Make sure we send all the buffered data
// there can be a lot of it for large messages. // there can be a lot of it for large messages.
@ -322,8 +340,6 @@ namespace ix
{ {
_socket->close(); _socket->close();
} }
},
_pingIntervalOrTimeoutGCDSecs);
} }
bool WebSocketTransport::isSendBufferEmpty() const bool WebSocketTransport::isSendBufferEmpty() const