mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:17:36 +00:00
Kernel: Make Socket inherit from File.
This commit is contained in:
parent
03da7046bd
commit
2470fdcd9b
18 changed files with 81 additions and 73 deletions
|
@ -44,6 +44,7 @@ public:
|
|||
|
||||
protected:
|
||||
IPv4Socket(int type, int protocol);
|
||||
virtual const char* class_name() const override { return "IPv4Socket"; }
|
||||
|
||||
int allocate_source_port_if_needed();
|
||||
|
||||
|
|
|
@ -113,6 +113,8 @@ void LocalSocket::attach(FileDescriptor& descriptor)
|
|||
case SocketRole::Connecting:
|
||||
++m_connecting_fds_open;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,6 +133,8 @@ void LocalSocket::detach(FileDescriptor& descriptor)
|
|||
ASSERT(m_connecting_fds_open);
|
||||
--m_connecting_fds_open;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <Kernel/FileSystem/FileDescriptor.h>
|
||||
#include <Kernel/Net/Socket.h>
|
||||
#include <Kernel/Net/LocalSocket.h>
|
||||
#include <Kernel/Net/IPv4Socket.h>
|
||||
|
@ -119,3 +120,22 @@ void Socket::load_send_deadline()
|
|||
m_send_deadline.tv_sec += (m_send_timeout.tv_usec / 1000000) * 1;
|
||||
m_send_deadline.tv_usec %= 1000000;
|
||||
}
|
||||
|
||||
static const char* to_string(SocketRole role)
|
||||
{
|
||||
switch (role) {
|
||||
case SocketRole::Listener:
|
||||
return "Listener";
|
||||
case SocketRole::Accepted:
|
||||
return "Accepted";
|
||||
case SocketRole::Connected:
|
||||
return "Connected";
|
||||
default:
|
||||
return "None";
|
||||
}
|
||||
}
|
||||
|
||||
String Socket::absolute_path(FileDescriptor& descriptor) const
|
||||
{
|
||||
return String::format("socket:%x (role: %s)", this, to_string(descriptor.socket_role()));
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <AK/RetainPtr.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <Kernel/File.h>
|
||||
#include <Kernel/UnixTypes.h>
|
||||
#include <Kernel/KResult.h>
|
||||
|
||||
|
@ -13,10 +14,10 @@ enum class ShouldBlock { No = 0, Yes = 1 };
|
|||
|
||||
class FileDescriptor;
|
||||
|
||||
class Socket : public Retainable<Socket> {
|
||||
class Socket : public File {
|
||||
public:
|
||||
static KResultOr<Retained<Socket>> create(int domain, int type, int protocol);
|
||||
virtual ~Socket();
|
||||
virtual ~Socket() override;
|
||||
|
||||
int domain() const { return m_domain; }
|
||||
int type() const { return m_type; }
|
||||
|
@ -34,10 +35,6 @@ public:
|
|||
virtual bool is_ipv4() const { return false; }
|
||||
virtual void attach(FileDescriptor&) = 0;
|
||||
virtual void detach(FileDescriptor&) = 0;
|
||||
virtual bool can_read(FileDescriptor&) const = 0;
|
||||
virtual ssize_t read(FileDescriptor&, byte*, ssize_t) = 0;
|
||||
virtual ssize_t write(FileDescriptor&, const byte*, ssize_t) = 0;
|
||||
virtual bool can_write(FileDescriptor&) const = 0;
|
||||
virtual ssize_t sendto(FileDescriptor&, const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
|
||||
virtual ssize_t recvfrom(FileDescriptor&, void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
|
||||
|
||||
|
@ -53,6 +50,8 @@ public:
|
|||
|
||||
Lock& lock() { return m_lock; }
|
||||
|
||||
virtual String absolute_path(FileDescriptor&) const override;
|
||||
|
||||
protected:
|
||||
Socket(int domain, int type, int protocol);
|
||||
|
||||
|
@ -61,7 +60,11 @@ protected:
|
|||
void load_receive_deadline();
|
||||
void load_send_deadline();
|
||||
|
||||
virtual const char* class_name() const override { return "Socket"; }
|
||||
|
||||
private:
|
||||
virtual bool is_socket() const final { return true; }
|
||||
|
||||
Lock m_lock { "Socket" };
|
||||
pid_t m_origin_pid { 0 };
|
||||
int m_domain { 0 };
|
||||
|
|
|
@ -29,6 +29,7 @@ public:
|
|||
|
||||
private:
|
||||
explicit TCPSocket(int protocol);
|
||||
virtual const char* class_name() const override { return "TCPSocket"; }
|
||||
|
||||
NetworkOrdered<word> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, word payload_size);
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ public:
|
|||
|
||||
private:
|
||||
explicit UDPSocket(int protocol);
|
||||
virtual const char* class_name() const override { return "UDPSocket"; }
|
||||
|
||||
virtual int protocol_receive(const ByteBuffer&, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length) override;
|
||||
virtual int protocol_send(const void*, int) override;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue