summaryrefslogtreecommitdiff
path: root/Libraries/LibCore
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-07-13 19:42:03 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-13 22:57:24 +0200
commitffa8cb668faffd4c417cc7ff60215d732a088122 (patch)
treeb2b562056ba6bec73b7999335c2e9d9484cb4b2f /Libraries/LibCore
parent983245113afe1961376f5381ea4a77e4364b9d2d (diff)
downloadserenity-ffa8cb668faffd4c417cc7ff60215d732a088122.zip
AudioServer: Assorted infrastructure work
* Add a LibAudio, and move WAV file parsing there (via AWavFile and AWavLoader) * Add CLocalSocket, and CSocket::connect() variant for local address types. We make some small use of this in WindowServer (as that's where we modelled it from), but don't get too invasive as this PR is already quite large, and the WS I/O is a bit carefully done * Add an AClientConnection which will eventually be used to talk to AudioServer (and make use of it in Piano, though right now it really doesn't do anything except connect, using our new CLocalSocket...)
Diffstat (limited to 'Libraries/LibCore')
-rw-r--r--Libraries/LibCore/CEventLoop.h2
-rw-r--r--Libraries/LibCore/CLocalSocket.cpp19
-rw-r--r--Libraries/LibCore/CLocalSocket.h7
-rw-r--r--Libraries/LibCore/CSocket.cpp23
-rw-r--r--Libraries/LibCore/CSocket.h4
-rw-r--r--Libraries/LibCore/CSocketAddress.h11
-rw-r--r--Libraries/LibCore/Makefile1
7 files changed, 64 insertions, 3 deletions
diff --git a/Libraries/LibCore/CEventLoop.h b/Libraries/LibCore/CEventLoop.h
index 8cab0bbe99..7e3d5cb9e7 100644
--- a/Libraries/LibCore/CEventLoop.h
+++ b/Libraries/LibCore/CEventLoop.h
@@ -6,11 +6,11 @@
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <LibCore/CLock.h>
+#include <LibCore/CEvent.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
-class CEvent;
class CObject;
class CNotifier;
diff --git a/Libraries/LibCore/CLocalSocket.cpp b/Libraries/LibCore/CLocalSocket.cpp
new file mode 100644
index 0000000000..20837a066f
--- /dev/null
+++ b/Libraries/LibCore/CLocalSocket.cpp
@@ -0,0 +1,19 @@
+#include <LibCore/CLocalSocket.h>
+#include <sys/socket.h>
+
+CLocalSocket::CLocalSocket(CObject* parent)
+ : CSocket(CSocket::Type::Local, parent)
+{
+ int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (fd < 0) {
+ set_error(fd);
+ } else {
+ set_fd(fd);
+ set_mode(CIODevice::ReadWrite);
+ set_error(0);
+ }
+}
+
+CLocalSocket::~CLocalSocket()
+{
+}
diff --git a/Libraries/LibCore/CLocalSocket.h b/Libraries/LibCore/CLocalSocket.h
new file mode 100644
index 0000000000..cdb4eeba55
--- /dev/null
+++ b/Libraries/LibCore/CLocalSocket.h
@@ -0,0 +1,7 @@
+#include <LibCore/CSocket.h>
+
+class CLocalSocket final : public CSocket {
+public:
+ explicit CLocalSocket(CObject* parent = nullptr);
+ virtual ~CLocalSocket() override;
+};
diff --git a/Libraries/LibCore/CSocket.cpp b/Libraries/LibCore/CSocket.cpp
index 4479969c40..cbe2bd9dd4 100644
--- a/Libraries/LibCore/CSocket.cpp
+++ b/Libraries/LibCore/CSocket.cpp
@@ -35,6 +35,8 @@ bool CSocket::connect(const CSocketAddress& address, int port)
{
ASSERT(!is_connected());
ASSERT(address.type() == CSocketAddress::Type::IPv4);
+ dbgprintf("Connecting to %s...", address.to_string().characters());
+
ASSERT(port > 0 && port <= 65535);
struct sockaddr_in addr;
@@ -47,7 +49,6 @@ bool CSocket::connect(const CSocketAddress& address, int port)
m_destination_address = address;
m_destination_port = port;
- dbgprintf("Connecting to %s...", address.to_string().characters());
fflush(stdout);
int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr));
if (rc < 0) {
@@ -71,6 +72,26 @@ bool CSocket::connect(const CSocketAddress& address, int port)
return true;
}
+bool CSocket::connect(const CSocketAddress& address)
+{
+ ASSERT(!is_connected());
+ ASSERT(address.type() == CSocketAddress::Type::Local);
+ dbgprintf("Connecting to %s...", address.to_string().characters());
+
+ sockaddr_un saddr;
+ saddr.sun_family = AF_LOCAL;
+ strcpy(saddr.sun_path, address.to_string().characters());
+
+ int rc = ::connect(fd(), (const sockaddr*)&saddr, sizeof(saddr));
+ if (rc < 0) {
+ perror("connect");
+ return false;
+ }
+
+ m_connected = true;
+ return true;
+}
+
ByteBuffer CSocket::receive(int max_size)
{
auto buffer = read(max_size);
diff --git a/Libraries/LibCore/CSocket.h b/Libraries/LibCore/CSocket.h
index 587f80f290..8521bb02f9 100644
--- a/Libraries/LibCore/CSocket.h
+++ b/Libraries/LibCore/CSocket.h
@@ -10,12 +10,14 @@ public:
enum class Type {
Invalid,
TCP,
- UDP
+ UDP,
+ Local,
};
virtual ~CSocket() override;
bool connect(const String& hostname, int port);
bool connect(const CSocketAddress&, int port);
+ bool connect(const CSocketAddress&);
ByteBuffer receive(int max_size);
bool send(const ByteBuffer&);
diff --git a/Libraries/LibCore/CSocketAddress.h b/Libraries/LibCore/CSocketAddress.h
index 019a0869fb..b0333516bb 100644
--- a/Libraries/LibCore/CSocketAddress.h
+++ b/Libraries/LibCore/CSocketAddress.h
@@ -17,6 +17,14 @@ public:
{
}
+ static CSocketAddress local(const String& address)
+ {
+ CSocketAddress addr;
+ addr.m_type = Type::Local;
+ addr.m_local_address = address;
+ return addr;
+ }
+
Type type() const { return m_type; }
bool is_valid() const { return m_type != Type::Invalid; }
IPv4Address ipv4_address() const { return m_ipv4_address; }
@@ -26,6 +34,8 @@ public:
switch (m_type) {
case Type::IPv4:
return m_ipv4_address.to_string();
+ case Type::Local:
+ return m_local_address;
default:
return "[CSocketAddress]";
}
@@ -34,4 +44,5 @@ public:
private:
Type m_type { Type::Invalid };
IPv4Address m_ipv4_address;
+ String m_local_address;
};
diff --git a/Libraries/LibCore/Makefile b/Libraries/LibCore/Makefile
index e7b25671e4..2e119ee71f 100644
--- a/Libraries/LibCore/Makefile
+++ b/Libraries/LibCore/Makefile
@@ -5,6 +5,7 @@ OBJS = \
CIODevice.o \
CFile.o \
CSocket.o \
+ CLocalSocket.o \
CTCPSocket.o \
CElapsedTimer.o \
CNotifier.o \