Lagom: Add a simple IPC client and server for host-side testing
Add LibIPC to the Lagom build, and also implement two little test apps so we can run IPC tests on other systems.
This commit is contained in:
parent
8e684f0959
commit
0f0b00dc1f
Notes:
sideshowbarker
2024-07-19 12:54:48 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0f0b00dc1f5
4 changed files with 93 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
|||
cmake_minimum_required (VERSION 3.0)
|
||||
project (Lagom)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror -std=c++17 -fPIC")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror -std=c++17 -fPIC -g")
|
||||
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconsumed")
|
||||
|
@ -11,8 +11,9 @@ endif()
|
|||
|
||||
file(GLOB AK_SOURCES "../AK/*.cpp")
|
||||
file(GLOB LIBCORE_SOURCES "../Libraries/LibCore/*.cpp")
|
||||
file(GLOB LIBIPC_SOURCES "../Libraries/LibIPC/*.cpp")
|
||||
|
||||
set(SOURCES ${AK_SOURCES} ${LIBCORE_SOURCES})
|
||||
set(SOURCES ${AK_SOURCES} ${LIBCORE_SOURCES} ${LIBIPC_SOURCES})
|
||||
|
||||
include_directories (../)
|
||||
include_directories (../Libraries/)
|
||||
|
@ -21,3 +22,11 @@ add_library(lagom ${SOURCES})
|
|||
add_executable(TestApp TestApp.cpp)
|
||||
target_link_libraries(TestApp lagom)
|
||||
target_link_libraries(TestApp stdc++)
|
||||
|
||||
add_executable(SimpleIPCClient SimpleIPCClient.cpp)
|
||||
target_link_libraries(SimpleIPCClient lagom)
|
||||
target_link_libraries(SimpleIPCClient stdc++)
|
||||
|
||||
add_executable(SimpleIPCServer SimpleIPCServer.cpp)
|
||||
target_link_libraries(SimpleIPCServer lagom)
|
||||
target_link_libraries(SimpleIPCServer stdc++)
|
||||
|
|
3
Lagom/SimpleIPC.ipc
Normal file
3
Lagom/SimpleIPC.ipc
Normal file
|
@ -0,0 +1,3 @@
|
|||
endpoint Simple {
|
||||
ComputeSum(i32 a, i32 b, i32 c) => (i32 sum)
|
||||
}
|
39
Lagom/SimpleIPCClient.cpp
Normal file
39
Lagom/SimpleIPCClient.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CTimer.h>
|
||||
#include <LibCore/CoreIPCClient.h>
|
||||
#include <stdio.h>
|
||||
#include "SimpleEndpoint.h"
|
||||
|
||||
class SimpleIPCClient : public IPC::Client::ConnectionNG<SimpleEndpoint> {
|
||||
C_OBJECT(SimpleIPCClient)
|
||||
public:
|
||||
SimpleIPCClient()
|
||||
: ConnectionNG("/tmp/simple-ipc")
|
||||
{}
|
||||
|
||||
virtual void handshake() override {}
|
||||
|
||||
i32 compute_sum(i32 a, i32 b, i32 c)
|
||||
{
|
||||
return send_sync<Simple::ComputeSum>(a, b, c)->sum();
|
||||
}
|
||||
};
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
CEventLoop event_loop;
|
||||
|
||||
SimpleIPCClient client;
|
||||
|
||||
CTimer timer(100, [&] {
|
||||
i32 sum = client.compute_sum(1, 2, 3);
|
||||
dbg() << "Sum: " << sum;
|
||||
});
|
||||
|
||||
CTimer kill_timer(5000, [&] {
|
||||
dbg() << "Timer fired, good-bye! :^)";
|
||||
event_loop.quit(0);
|
||||
});
|
||||
|
||||
return event_loop.exec();
|
||||
}
|
40
Lagom/SimpleIPCServer.cpp
Normal file
40
Lagom/SimpleIPCServer.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CTimer.h>
|
||||
#include <LibCore/CoreIPCServer.h>
|
||||
#include <LibCore/CLocalServer.h>
|
||||
#include <stdio.h>
|
||||
#include "SimpleEndpoint.h"
|
||||
|
||||
class SimpleIPCServer final :
|
||||
public IPC::Server::ConnectionNG<SimpleEndpoint>,
|
||||
public SimpleEndpoint {
|
||||
|
||||
C_OBJECT(SimpleIPCServer)
|
||||
public:
|
||||
SimpleIPCServer(CLocalSocket& socket, int client_id)
|
||||
: ConnectionNG(*this, socket, client_id)
|
||||
{
|
||||
}
|
||||
|
||||
virtual OwnPtr<Simple::ComputeSumResponse> handle(const Simple::ComputeSum& message)
|
||||
{
|
||||
return make<Simple::ComputeSumResponse>(message.a() + message.b() + message.c());
|
||||
}
|
||||
};
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
CEventLoop event_loop;
|
||||
|
||||
unlink("/tmp/simple-ipc");
|
||||
CLocalServer server_sock;
|
||||
server_sock.listen("/tmp/simple-ipc");
|
||||
server_sock.on_ready_to_accept = [&] {
|
||||
auto* client_socket = server_sock.accept();
|
||||
ASSERT(client_socket);
|
||||
static int next_client_id = 0;
|
||||
IPC::Server::new_connection_ng_for_client<SimpleIPCServer>(*client_socket, ++next_client_id);
|
||||
};
|
||||
|
||||
return event_loop.exec();
|
||||
}
|
Loading…
Add table
Reference in a new issue