-
Notifications
You must be signed in to change notification settings - Fork 12
/
channel.xs
96 lines (85 loc) · 2.63 KB
/
channel.xs
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
Grpc::XS::Channel
new(const char *class, const char* target, ... )
PREINIT:
ChannelCTX* ctx = (ChannelCTX *)malloc( sizeof(ChannelCTX) );
ctx->wrapped = NULL;
CODE:
if ( items > 2 && ( items - 2 ) % 2 ) {
croak("Expecting a hash as input to constructor");
}
Grpc__XS__ChannelCredentials creds = NULL;
// channel, args_hash
// hash->{credentials} - credentials object (optional)
int i;
HV *hash = newHV();
if (items>2) {
for (i = 2; i < items; i += 2 ) {
SV *key = ST(i);
if (!strcmp(SvPV_nolen(key), "credentials")) {
if (!sv_isobject(ST(i+1)) ||
!sv_derived_from(ST(i+1),"Grpc::XS::ChannelCredentials")) {
croak("credentials is not a credentials object");
} else {
IV tmp = SvIV((SV*)SvRV(ST(i+1)));
creds = INT2PTR(Grpc__XS__ChannelCredentials,tmp);
}
} else {
SV *value = newSVsv(ST(i+1));
hv_store_ent(hash,key,value,0);
}
}
}
grpc_channel_args args;
perl_grpc_read_args_array(hash, &args);
if (creds == NULL) {
ctx->wrapped = grpc_insecure_channel_create(target, &args, NULL);
} else {
gpr_log(GPR_DEBUG, "Initialized secure channel");
ctx->wrapped =
grpc_secure_channel_create(creds->wrapped, target, &args, NULL);
}
free(args.args);
RETVAL = ctx;
OUTPUT: RETVAL
const char*
getTarget(Grpc::XS::Channel self)
CODE:
RETVAL = grpc_channel_get_target(self->wrapped);
OUTPUT: RETVAL
long
getConnectivityState(Grpc::XS::Channel self, ... )
CODE:
int try_to_connect = 0;
if ( items > 1 ) {
if (items > 2 || !SvIOK(ST(1))) {
croak("Invalid param getConnectivityState");
}
try_to_connect = SvUV(ST(1));
}
RETVAL = grpc_channel_check_connectivity_state(self->wrapped, try_to_connect);
OUTPUT: RETVAL
int
watchConnectivityState(Grpc::XS::Channel self, long last_state, Grpc::XS::Timeval deadline)
CODE:
grpc_channel_watch_connectivity_state(
self->wrapped, (grpc_connectivity_state)last_state,
deadline->wrapped, completion_queue, NULL);
grpc_event event = grpc_completion_queue_pluck(
completion_queue, NULL,
gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
RETVAL = event.success;
OUTPUT: RETVAL
void
close(Grpc::XS::Channel self)
CODE:
if (self->wrapped != NULL) {
grpc_channel_destroy(self->wrapped);
self->wrapped = NULL;
}
void
DESTROY(Grpc::XS::Channel self)
CODE:
if (self->wrapped != NULL) {
grpc_channel_destroy(self->wrapped);
}
Safefree(self);