forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
8e684f0
commit 0f0b00d
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
endpoint Simple { | ||
ComputeSum(i32 a, i32 b, i32 c) => (i32 sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |