-
Notifications
You must be signed in to change notification settings - Fork 85
/
searpc-demo-client.c
212 lines (174 loc) · 5.57 KB
/
searpc-demo-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <glib-object.h>
#include <searpc.h>
#include "searpc-demo-packet.h"
#include "test-object.h"
#define BUFLEN 256
#define MAGIC_STRING "ABCD"
#ifdef WIN32
#include <inttypes.h>
#include <winsock2.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
static char *transport_callback(void *arg, const char *fcall_str,
size_t fcall_len, size_t *ret_len)
{
int fd, ret;
char buf[BUFLEN];
packet *pac, *pac_ret;
fd = (int)(long) arg;
pac = (packet *)buf;
/* construct the packet */
pac->length = htons((uint16_t)fcall_len);
memcpy(pac->data, fcall_str, fcall_len);
/* send the packet */
if ( writen (fd, buf, PACKET_HEADER_LENGTH + fcall_len) == -1) {
fprintf (stderr, "write failed: %s\n", strerror(errno));
exit(-1);
}
/* read the returned packet */
pac_ret = read_packet(fd, buf);
if (pac_ret == NULL) {
fprintf(stderr, "read packet failed: %s\n", strerror(errno));
exit(-1);
}
*ret_len = ntohs(pac_ret->length);
return g_strndup(pac_ret->data, *ret_len);
}
/*The function is copied from searpc-server.c for convience*/
void
searpc_set_objlist_to_ret_object (json_t *object, GList *ret)
{
GList *ptr;
if (ret == NULL)
json_object_set_new (object, "ret", json_null ());
else {
json_t *array = json_array ();
for (ptr = ret; ptr; ptr = ptr->next)
json_array_append_new (array, json_gobject_serialize (ptr->data));
json_object_set_new (object, "ret", array);
for (ptr = ret; ptr; ptr = ptr->next)
g_object_unref (ptr->data);
g_list_free (ret);
}
}
int
connection_init(int *sockfd, struct sockaddr_in *servaddr)
{
int ret;
ret = *sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (ret < 0) {
fprintf(stderr, "socket failed: %s\n", strerror(errno));
return -1;
}
int on = 1;
if (setsockopt (*sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) < 0) {
fprintf (stderr, "setsockopt of SO_REUSEADDR error: %s\n", strerror(errno));
return -1;
}
ret = connect(*sockfd, (struct sockaddr *)servaddr, sizeof(*servaddr));
if (ret < 0) {
fprintf(stderr, "connect failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
void
rpc_string_test(int sockfd, struct sockaddr_in *servaddr, SearpcClient *rpc_client, GError *error)
{
int ret;
char str[16] = "hello searpc";
/* call the client-side funcion */
ret = searpc_client_call__int(rpc_client, "searpc_strlen", &error,
1, "string", str);
if (error != NULL) {
fprintf(stderr, "error: %s\n", error->message);
exit(-1);
} else
printf("the length of string %s is %d.\n", str, ret);
if (ret == strlen(str))
printf("String test succeed.\n");
else printf("String test fail.\n");
close(sockfd);
}
void
rpc_glist_test(int sockfd, struct sockaddr_in *servaddr, SearpcClient *rpc_client, GError *error)
{
int count = 4, len = 11;
char str[16] = "A rpc test.";
GList *ans=searpc_client_call__objlist(rpc_client, "searpc_objlisttest",
TEST_OBJECT_TYPE, &error, 3,
"int", count,
"int", len,
"string", str);
json_t *object=json_object();
searpc_set_objlist_to_ret_object (object,ans);
if (error != NULL) {
fprintf(stderr, "error: %s\n", error->message);
exit(-1);
}
else printf("%s\n", json_dumps (object, JSON_INDENT(2)));
json_t *array = json_object_get (object, "ret");
if (json_array_size(array) != count) {
printf("Glisttest fail.\n");
return;
}
int i;
for (i = 0; i != count; ++i) {
json_t *member = json_array_get(array, i);
if (json_integer_value (json_object_get (member, "len"))!=len) {
printf("Glisttest fail.\n");
return;
}
if (strcmp (json_string_value (json_object_get (member, "str")), str)) {
printf("Glisttest fail.\n");
return;
}
if ((json_is_false (json_object_get (member, "equal"))) == (strlen(str)==len)) {
printf("Glisttest fail.\n");
return;
}
}
json_decref(object);
printf("Glisttest succeed.\n");
close(sockfd);
}
int
main(int argc, char *argv[])
{
int sockfd;
char *ret_str;
struct sockaddr_in servaddr;
SearpcClient *rpc_client;
GError *error = NULL;
#if !GLIB_CHECK_VERSION(2, 36, 0)
g_type_init();
#endif
#ifdef WIN32
WSADATA wsadata;
WSAStartup(0x0101, &wsadata);
#endif
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(12345);
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
if (connection_init(&sockfd, &servaddr)<0) exit(-1);
/* create an rpc_client and supply the transport function. */
rpc_client = searpc_client_new();
rpc_client->send = transport_callback;
rpc_client->arg = (void *)(long)sockfd;
rpc_string_test(sockfd, &servaddr, rpc_client, error);
if (connection_init(&sockfd, &servaddr)<0) exit(-1);
rpc_client->arg = (void *)(long)sockfd;
rpc_glist_test(sockfd, &servaddr, rpc_client, error);
return 0;
}