mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
Add support for socket send/receive timeouts.
Only the receive timeout is hooked up yet. You can change the timeout by calling setsockopt(..., SOL_SOCKET, SO_RCVTIMEO, ...). Use this mechanism to make /bin/ping report timeouts.
This commit is contained in:
parent
7bcd386338
commit
562663df7c
12 changed files with 212 additions and 12 deletions
|
@ -60,3 +60,63 @@ KResult Socket::queue_connection_from(Socket& peer)
|
|||
m_pending.append(peer);
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
KResult Socket::setsockopt(int level, int option, const void* value, socklen_t value_size)
|
||||
{
|
||||
ASSERT(level == SOL_SOCKET);
|
||||
switch (option) {
|
||||
case SO_SNDTIMEO:
|
||||
if (value_size != sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
m_send_timeout = *(timeval*)value;
|
||||
return KSuccess;
|
||||
case SO_RCVTIMEO:
|
||||
if (value_size != sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
m_receive_timeout = *(timeval*)value;
|
||||
return KSuccess;
|
||||
default:
|
||||
kprintf("%s(%u): setsockopt() at SOL_SOCKET with unimplemented option %d\n", option);
|
||||
return KResult(-ENOPROTOOPT);
|
||||
}
|
||||
}
|
||||
|
||||
KResult Socket::getsockopt(int level, int option, void* value, socklen_t* value_size)
|
||||
{
|
||||
ASSERT(level == SOL_SOCKET);
|
||||
switch (option) {
|
||||
case SO_SNDTIMEO:
|
||||
if (*value_size < sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
*(timeval*)value = m_send_timeout;
|
||||
*value_size = sizeof(timeval);
|
||||
return KSuccess;
|
||||
case SO_RCVTIMEO:
|
||||
if (*value_size < sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
*(timeval*)value = m_receive_timeout;
|
||||
*value_size = sizeof(timeval);
|
||||
return KSuccess;
|
||||
default:
|
||||
kprintf("%s(%u): getsockopt() at SOL_SOCKET with unimplemented option %d\n", option);
|
||||
return KResult(-ENOPROTOOPT);
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::load_receive_deadline()
|
||||
{
|
||||
kgettimeofday(m_receive_deadline);
|
||||
m_receive_deadline.tv_sec += m_receive_timeout.tv_sec;
|
||||
m_receive_deadline.tv_usec += m_receive_timeout.tv_usec;
|
||||
m_receive_deadline.tv_sec += (m_send_timeout.tv_usec / 1000000) * 1;
|
||||
m_receive_deadline.tv_usec %= 1000000;
|
||||
}
|
||||
|
||||
void Socket::load_send_deadline()
|
||||
{
|
||||
kgettimeofday(m_send_deadline);
|
||||
m_send_deadline.tv_sec += m_send_timeout.tv_sec;
|
||||
m_send_deadline.tv_usec += m_send_timeout.tv_usec;
|
||||
m_send_deadline.tv_sec += (m_send_timeout.tv_usec / 1000000) * 1;
|
||||
m_send_deadline.tv_usec %= 1000000;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue