mirror of
https://github.com/RGBCube/serenity
synced 2025-07-10 04:17:34 +00:00
Net: Let Socket have read/write wrappers around sendto/recvfrom
The situations in IPv4Socket and LocalSocket were mirrors of each other where one had implemented read/write as wrappers and the other had sendto/recvfrom as wrappers. Instead of this silliness, move read and write up to the Socket base. Then mark them final, so subclasses have no choice but to implement sendto and recvfrom.
This commit is contained in:
parent
9111d3626a
commit
6347e3aa51
6 changed files with 37 additions and 47 deletions
|
@ -163,26 +163,6 @@ bool LocalSocket::can_read(FileDescription& description) const
|
|||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::read(FileDescription& description, u8* buffer, ssize_t size)
|
||||
{
|
||||
auto role = description.socket_role();
|
||||
if (role == SocketRole::Accepted) {
|
||||
if (!description.is_blocking()) {
|
||||
if (m_for_server.is_empty())
|
||||
return -EAGAIN;
|
||||
}
|
||||
return m_for_server.read(buffer, size);
|
||||
}
|
||||
if (role == SocketRole::Connected) {
|
||||
if (!description.is_blocking()) {
|
||||
if (m_for_client.is_empty())
|
||||
return -EAGAIN;
|
||||
}
|
||||
return m_for_client.read(buffer, size);
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool LocalSocket::has_attached_peer(const FileDescription& description) const
|
||||
{
|
||||
if (description.socket_role() == SocketRole::Accepted)
|
||||
|
@ -192,17 +172,6 @@ bool LocalSocket::has_attached_peer(const FileDescription& description) const
|
|||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::write(FileDescription& description, const u8* data, ssize_t size)
|
||||
{
|
||||
if (!has_attached_peer(description))
|
||||
return -EPIPE;
|
||||
if (description.socket_role() == SocketRole::Accepted)
|
||||
return m_for_client.write(data, size);
|
||||
if (description.socket_role() == SocketRole::Connected)
|
||||
return m_for_server.write(data, size);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool LocalSocket::can_write(FileDescription& description) const
|
||||
{
|
||||
if (description.socket_role() == SocketRole::Accepted)
|
||||
|
@ -214,10 +183,31 @@ bool LocalSocket::can_write(FileDescription& description) const
|
|||
|
||||
ssize_t LocalSocket::sendto(FileDescription& description, const void* data, size_t data_size, int, const sockaddr*, socklen_t)
|
||||
{
|
||||
return write(description, (const u8*)data, data_size);
|
||||
if (!has_attached_peer(description))
|
||||
return -EPIPE;
|
||||
if (description.socket_role() == SocketRole::Accepted)
|
||||
return m_for_client.write((const u8*)data, data_size);
|
||||
if (description.socket_role() == SocketRole::Connected)
|
||||
return m_for_server.write((const u8*)data, data_size);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t buffer_size, int, sockaddr*, socklen_t*)
|
||||
{
|
||||
return read(description, (u8*)buffer, buffer_size);
|
||||
auto role = description.socket_role();
|
||||
if (role == SocketRole::Accepted) {
|
||||
if (!description.is_blocking()) {
|
||||
if (m_for_server.is_empty())
|
||||
return -EAGAIN;
|
||||
}
|
||||
return m_for_server.read((u8*)buffer, buffer_size);
|
||||
}
|
||||
if (role == SocketRole::Connected) {
|
||||
if (!description.is_blocking()) {
|
||||
if (m_for_client.is_empty())
|
||||
return -EAGAIN;
|
||||
}
|
||||
return m_for_client.read((u8*)buffer, buffer_size);
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue