implement client C binding
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
#include "MyServiceClient_c.h"
|
||||
#include "rpc/rpc_client_c.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
// Создание FIFO — часть пользовательского IPC‑кода.
|
||||
mkfifo("/tmp/fifo_to_server", 0666);
|
||||
mkfifo("/tmp/fifo_to_client", 0666);
|
||||
|
||||
// Создание RPC клиента
|
||||
RpcClient* client = rpc_client_create("/tmp/fifo_to_client", "/tmp/fifo_to_server");
|
||||
if (!client) {
|
||||
fprintf(stderr, "Failed to create RPC client\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Создание маршаллеров для двух объектов
|
||||
// По договорённости на сервере:
|
||||
// ObjectId = 0 -> первый MyService
|
||||
// ObjectId = 1 -> второй MyService
|
||||
IpcMarshallerHandle* m1 = rpc_client_create_marshaller(client, 0);
|
||||
IpcMarshallerHandle* m2 = rpc_client_create_marshaller(client, 1);
|
||||
|
||||
if (!m1 || !m2) {
|
||||
fprintf(stderr, "Failed to create marshallers\n");
|
||||
rpc_client_destroy(client);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Создание клиентов для MyService
|
||||
MyServiceClient* obj1 = myservice_client_create(m1);
|
||||
MyServiceClient* obj2 = myservice_client_create(m2);
|
||||
|
||||
if (!obj1 || !obj2) {
|
||||
fprintf(stderr, "Failed to create service clients\n");
|
||||
rpc_marshaller_destroy(m1);
|
||||
rpc_marshaller_destroy(m2);
|
||||
rpc_client_destroy(client);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// obj1 увеличивает свой счётчик.
|
||||
int result = myservice_add(obj1, 7, 9);
|
||||
if (result == -1) {
|
||||
fprintf(stderr, "Error calling myservice_add on obj1\n");
|
||||
}
|
||||
|
||||
// obj2 увеличивает счётчик на текущее значение счётчика obj1.
|
||||
int counter1 = myservice_get(obj1);
|
||||
if (counter1 == -1) {
|
||||
fprintf(stderr, "Error calling myservice_get on obj1\n");
|
||||
} else {
|
||||
result = myservice_add(obj2, 0, counter1);
|
||||
if (result == -1) {
|
||||
fprintf(stderr, "Error calling myservice_add on obj2\n");
|
||||
}
|
||||
}
|
||||
|
||||
int c1 = myservice_get(obj1);
|
||||
int c2 = myservice_get(obj2);
|
||||
|
||||
if (c1 == -1 || c2 == -1) {
|
||||
fprintf(stderr, "Error getting final counters\n");
|
||||
} else {
|
||||
printf("OBJ1: %d OBJ2: %d\n", c1, c2);
|
||||
}
|
||||
|
||||
// Освобождение ресурсов
|
||||
myservice_client_destroy(obj1);
|
||||
myservice_client_destroy(obj2);
|
||||
rpc_marshaller_destroy(m1);
|
||||
rpc_marshaller_destroy(m2);
|
||||
rpc_client_destroy(client);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "rpc/rpc_client_c.h"
|
||||
#include "rpc/rpc_client_c_impl.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
extern "C" {
|
||||
|
||||
RpcClient* rpc_client_create(const char* read_pipe, const char* write_pipe) {
|
||||
try {
|
||||
IpcPipeChannel* channel = new IpcPipeChannel(read_pipe, write_pipe);
|
||||
// Проверяем, что канал открылся успешно
|
||||
// (IpcPipeChannel может вывести ошибку, но не бросает исключение)
|
||||
// Для простоты считаем, что если конструктор завершился, всё ОК
|
||||
return new RpcClient(channel);
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void rpc_client_destroy(RpcClient* client) {
|
||||
if (client) {
|
||||
delete client;
|
||||
}
|
||||
}
|
||||
|
||||
IpcMarshallerHandle* rpc_client_create_marshaller(RpcClient* client, int object_id) {
|
||||
if (!client || !client->channel) {
|
||||
return nullptr;
|
||||
}
|
||||
try {
|
||||
IpcMarshaller* marshaller = new IpcMarshaller(*client->channel, object_id);
|
||||
return new IpcMarshallerHandle(marshaller);
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void rpc_marshaller_destroy(IpcMarshallerHandle* marshaller) {
|
||||
if (marshaller) {
|
||||
delete marshaller;
|
||||
}
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Reference in New Issue
Block a user