summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-14 16:01:08 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-14 16:01:45 +0100
commiteb1c721ef343c065d871499c248fb5b754276293 (patch)
treead2426addcc9d2c535b68ab953dbc83e11acef5a
parentb20a7aca612b705e47fc15d1b1c9b864c6acda61 (diff)
downloadserenity-eb1c721ef343c065d871499c248fb5b754276293.zip
Kernel: Actually send things between the socket endpoints.
-rw-r--r--Kernel/FileDescriptor.cpp8
-rw-r--r--Kernel/FileDescriptor.h4
-rw-r--r--Kernel/LocalSocket.cpp32
-rw-r--r--Kernel/LocalSocket.h5
-rw-r--r--Kernel/Socket.h7
5 files changed, 53 insertions, 3 deletions
diff --git a/Kernel/FileDescriptor.cpp b/Kernel/FileDescriptor.cpp
index 06c6f34991..f5533fc4c0 100644
--- a/Kernel/FileDescriptor.cpp
+++ b/Kernel/FileDescriptor.cpp
@@ -167,6 +167,8 @@ ssize_t FileDescriptor::read(Process& process, byte* buffer, size_t count)
// FIXME: What should happen to m_currentOffset?
return m_device->read(process, buffer, count);
}
+ if (m_socket)
+ return m_socket->read(m_socket_role, buffer, count);
ASSERT(inode());
ssize_t nread = inode()->read_bytes(m_current_offset, count, buffer, this);
m_current_offset += nread;
@@ -183,6 +185,8 @@ ssize_t FileDescriptor::write(Process& process, const byte* data, size_t size)
// FIXME: What should happen to m_currentOffset?
return m_device->write(process, data, size);
}
+ if (m_socket)
+ return m_socket->write(m_socket_role, data, size);
ASSERT(m_inode);
ssize_t nwritten = m_inode->write_bytes(m_current_offset, size, data, this);
m_current_offset += nwritten;
@@ -197,6 +201,8 @@ bool FileDescriptor::can_write(Process& process)
}
if (m_device)
return m_device->can_write(process);
+ if (m_socket)
+ return m_socket->can_write(m_socket_role);
return true;
}
@@ -208,6 +214,8 @@ bool FileDescriptor::can_read(Process& process)
}
if (m_device)
return m_device->can_read(process);
+ if (m_socket)
+ return m_socket->can_read(m_socket_role);
return true;
}
diff --git a/Kernel/FileDescriptor.h b/Kernel/FileDescriptor.h
index f89f96f325..0dc15af07d 100644
--- a/Kernel/FileDescriptor.h
+++ b/Kernel/FileDescriptor.h
@@ -7,13 +7,11 @@
#include <AK/CircularQueue.h>
#include <AK/Retainable.h>
#include <AK/Badge.h>
+#include <Kernel/Socket.h>
class TTY;
class MasterPTY;
class Process;
-class Socket;
-
-enum class SocketRole { None, Accepted, Connected };
class FileDescriptor : public Retainable<FileDescriptor> {
public:
diff --git a/Kernel/LocalSocket.cpp b/Kernel/LocalSocket.cpp
index 2fb0f3de1c..055ef40fd5 100644
--- a/Kernel/LocalSocket.cpp
+++ b/Kernel/LocalSocket.cpp
@@ -94,3 +94,35 @@ RetainPtr<Socket> LocalSocket::connect(const sockaddr* address, socklen_t addres
m_connected = true;
return m_peer;
}
+
+bool LocalSocket::can_read(SocketRole role) const
+{
+ if (role == SocketRole::Accepted)
+ return !m_for_server.is_empty();
+ else
+ return !m_for_client.is_empty();
+}
+
+ssize_t LocalSocket::read(SocketRole role, byte* buffer, size_t size)
+{
+ if (role == SocketRole::Accepted)
+ return m_for_server.read(buffer, size);
+ else
+ return m_for_client.read(buffer, size);
+}
+
+ssize_t LocalSocket::write(SocketRole role, const byte* data, size_t size)
+{
+ if (role == SocketRole::Accepted)
+ return m_for_client.write(data, size);
+ else
+ return m_for_server.write(data, size);
+}
+
+bool LocalSocket::can_write(SocketRole role) const
+{
+ if (role == SocketRole::Accepted)
+ return !m_for_client.is_empty();
+ else
+ return !m_for_server.is_empty();
+}
diff --git a/Kernel/LocalSocket.h b/Kernel/LocalSocket.h
index 7ec70b9741..76e58c92ad 100644
--- a/Kernel/LocalSocket.h
+++ b/Kernel/LocalSocket.h
@@ -14,6 +14,11 @@ public:
virtual RetainPtr<Socket> connect(const sockaddr*, socklen_t, int& error) override;
virtual bool get_address(sockaddr*, socklen_t*) override;
+ virtual bool can_read(SocketRole) const override;
+ virtual ssize_t read(SocketRole, byte*, size_t) override;
+ virtual ssize_t write(SocketRole, const byte*, size_t) override;
+ virtual bool can_write(SocketRole) const override;
+
private:
explicit LocalSocket(int type);
virtual bool is_local() const override { return true; }
diff --git a/Kernel/Socket.h b/Kernel/Socket.h
index 2afd6a53f2..56e91a64b5 100644
--- a/Kernel/Socket.h
+++ b/Kernel/Socket.h
@@ -7,6 +7,8 @@
#include <AK/Vector.h>
#include <Kernel/UnixTypes.h>
+enum class SocketRole { None, Accepted, Connected };
+
class Socket : public Retainable<Socket> {
public:
static RetainPtr<Socket> create(int domain, int type, int protocol, int& error);
@@ -27,6 +29,11 @@ public:
virtual bool get_address(sockaddr*, socklen_t*) = 0;
virtual bool is_local() const { return false; }
+ virtual bool can_read(SocketRole) const = 0;
+ virtual ssize_t read(SocketRole, byte*, size_t) = 0;
+ virtual ssize_t write(SocketRole, const byte*, size_t) = 0;
+ virtual bool can_write(SocketRole) const = 0;
+
protected:
Socket(int domain, int type, int protocol);