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();
}
void Socket::poll(const OnPollCallback& onPollCallback, int timeoutSecs)
PollResultType Socket::poll(int timeoutSecs)
{
if (_sockfd == -1)
{
if (onPollCallback) onPollCallback(PollResultType::Error);
return;
return PollResultType::Error;
}
PollResultType pollResult = isReadyToRead(1000 * timeoutSecs);
if (onPollCallback) onPollCallback(pollResult);
return isReadyToRead(1000 * timeoutSecs);
}
PollResultType Socket::select(bool readyToRead, int timeoutMs)

View File

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

View File

@ -232,9 +232,15 @@ namespace ix
void WebSocketTransport::poll()
{
_socket->poll(
[this](PollResultType pollResult)
PollResultType pollResult = _socket->poll(_pingIntervalOrTimeoutGCDSecs);
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 (1) ping timeout is enabled and (2) duration since last received ping response (PONG)
@ -252,6 +258,18 @@ namespace ix
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
// there can be a lot of it for large messages.
@ -322,8 +340,6 @@ namespace ix
{
_socket->close();
}
},
_pingIntervalOrTimeoutGCDSecs);
}
bool WebSocketTransport::isSendBufferEmpty() const