mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 16:25:08 +00:00

The goal here is to generate most of this code from IPC protocol descriptions, but for now I've spelled them all out to get started. Each message gets a wrapper class in the ASAPI_Client or ASAPI_Server namespace. They are convertible to and from the old message structs. The real hotness happens when you want to make a synchronous request to the other side: auto response = send_sync<ASAPI_Client::GetMainMixVolume>(); Each request class knows his corresponding response class, so in the above example, "response" will be an ASAPI_Server::DidGetMainMixVolume object, and we can get the volume like so: int volume = response.volume(); For posting messages that don't expect a response, you can still use post_message() since the message classes are convertible: post_message(ASAPI_Server::DidGetMainMixVolume(volume)); It's not perfect yet, but I already really like it. :^)
80 lines
2.4 KiB
C++
80 lines
2.4 KiB
C++
#include "ASClientConnection.h"
|
|
#include "ASMixer.h"
|
|
|
|
#include <LibAudio/ABuffer.h>
|
|
#include <LibAudio/ASAPI.h>
|
|
#include <LibCore/CEventLoop.h>
|
|
#include <SharedBuffer.h>
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <sys/uio.h>
|
|
#include <unistd.h>
|
|
|
|
ASClientConnection::ASClientConnection(CLocalSocket& client_socket, int client_id, ASMixer& mixer)
|
|
: Connection(client_socket, client_id)
|
|
, m_mixer(mixer)
|
|
{
|
|
}
|
|
|
|
ASClientConnection::~ASClientConnection()
|
|
{
|
|
}
|
|
|
|
void ASClientConnection::send_greeting()
|
|
{
|
|
post_message(ASAPI_Server::Greeting(getpid(), client_id()));
|
|
}
|
|
|
|
bool ASClientConnection::handle_message(const ASAPI_ClientMessage& message, const ByteBuffer&&)
|
|
{
|
|
switch (message.type) {
|
|
case ASAPI_ClientMessage::Type::Greeting:
|
|
set_client_pid(message.greeting.client_pid);
|
|
break;
|
|
case ASAPI_ClientMessage::Type::EnqueueBuffer: {
|
|
auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(message.play_buffer.buffer_id);
|
|
if (!shared_buffer) {
|
|
did_misbehave();
|
|
return false;
|
|
}
|
|
|
|
ASAPI_ServerMessage reply;
|
|
reply.type = ASAPI_ServerMessage::Type::EnqueueBufferResponse;
|
|
reply.playing_buffer.buffer_id = message.play_buffer.buffer_id;
|
|
|
|
if (!m_queue)
|
|
m_queue = m_mixer.create_queue(*this);
|
|
|
|
if (m_queue->is_full()) {
|
|
post_message(ASAPI_Server::EnqueueBufferResponse(false, message.play_buffer.buffer_id));
|
|
break;
|
|
}
|
|
m_queue->enqueue(ABuffer::create_with_shared_buffer(*shared_buffer));
|
|
post_message(ASAPI_Server::EnqueueBufferResponse(true, message.play_buffer.buffer_id));
|
|
break;
|
|
}
|
|
case ASAPI_ClientMessage::Type::GetMainMixVolume: {
|
|
post_message(ASAPI_Server::DidGetMainMixVolume(m_mixer.main_volume()));
|
|
break;
|
|
}
|
|
case ASAPI_ClientMessage::Type::SetMainMixVolume: {
|
|
m_mixer.set_main_volume(message.value);
|
|
post_message(ASAPI_Server::DidSetMainMixVolume());
|
|
break;
|
|
}
|
|
case ASAPI_ClientMessage::Type::Invalid:
|
|
default:
|
|
dbgprintf("ASClientConnection: Unexpected message ID %d\n", int(message.type));
|
|
did_misbehave();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ASClientConnection::did_finish_playing_buffer(Badge<ASMixer>, int buffer_id)
|
|
{
|
|
post_message(ASAPI_Server::FinishedPlayingBuffer(buffer_id));
|
|
}
|