void erl_thread_task void ptr int sfd int port CNODE_PORT if Config er

 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
void *erl_thread_task(void *ptr)
{
int sfd;
int port = CNODE_PORT;
if (Config.erlang_port > 0) port = Config.erlang_port;
int loop = 1, got;
ErlConnect conn;
ErlMessage emsg;
unsigned char buf[BUFSIZE];
app_trace(TRACE_INFO, "erlang: start thread %d", getpid());
while(1) {
loop = 1;
sfd = create_inet_sock("*", port);
erl_init(NULL, 0);
erl_connect_init(Config.cnode_number, "secret", 0);
erl_publish(port);
app_trace(TRACE_INFO, "erlang: port %d published", port);
erl_fd = erl_accept(sfd, &conn);
app_trace(TRACE_INFO, "Erlang node: Accepted.");
while (loop) {
got = erl_receive_msg(erl_fd, &buf[0], BUFSIZE, &emsg);
if (got == ERL_TICK) {
//app_trace(TRACE_INFO, "Erlang node: Tick!");
}
else
if (got == ERL_ERROR) {
app_trace(TRACE_INFO, "Erlang node: error");
loop = 0; // exit while loop
}
else {
if (emsg.type == ERL_REG_SEND) {
if (ERL_TUPLE_SIZE(emsg.msg) != 3){
app_trace(TRACE_INFO, "erlang: bad main tuple size!");
}
else{
// store destination
to_erlang_node = erl_copy_term(emsg.from);
erl_proc(erl_fd, buf, &emsg);
}
}
}
// free msg memory
erl_free_term(emsg.msg);
erl_free_term(emsg.to);
erl_free_term(emsg.from);
// +++ send init port states +++
if (to_erlang_node && need_send_states){
//send_all_port_states();
send_all_init_states();
need_send_states = 0;
}
} // while(loop)
// close descriptors
close(erl_fd);
close(sfd);
erl_fd = -1;
to_erlang_node = NULL;
} // while(1)
}