static void new_connection(void *ud, zn_State *S) { zn_BufferPoolNode *data = zn_getbuffer(&pool); zn_recvonheader(&data->recv, on_header, data); zn_recvonpacket(&data->recv, on_client_packet, data); data->tcp = zn_newtcp(S); if (zn_connect(data->tcp, addr, port, on_connect, data) != ZN_OK) zn_post(S, new_connection, NULL); }
inline bool TcpSocket::doConnect(std::string const& remoteIP, unsigned short remotePort, OnConnectHandler&& h) { if (zn_connect(tcp, remoteIP.c_str(), remotePort, connect_cb, this) == ZN_OK) { connectHandler = std::move(h); return true; } return false; }
static zn_Time on_client(void *ud, zn_Timer *timer, zn_Time elapsed) { zn_BufferPoolNode *node = zn_getbuffer(&pool); zn_Tcp *tcp = zn_newtcp(S); zn_recvonheader(&node->recv, on_header, node); zn_recvonpacket(&node->recv, on_packet, node); node->user_data = 0; node->tcp = tcp; zn_connect(tcp, addr, port, on_connect, node); return 1; }
int main(int argc, const char **argv) { if (argc == 2 && strcmp(argv[1], "-h") == 0) { printf("usage: %s [(client/server) [ip [port]]]\n", argv[0]); exit(0); } if (argc > 1) { if (strcmp(argv[1], "client") == 0) is_client = 1; } if (argc > 2) { strncpy(addr, argv[2], ZN_MAX_ADDRLEN-1); } if (argc > 3) { unsigned p = atoi(argv[3]); if (p != 0) port = p; } zn_initialize(); if ((S = zn_newstate()) == NULL) return 2; if (is_client) { zn_Tcp *tcp; if ((tcp = zn_newtcp(S)) == NULL) return 2; zn_connect(tcp, addr, port, on_connect, NULL); printf("connecting to %s:%d ...\n", addr, port); } else { zn_Accept *accept; if ((accept = zn_newaccept(S)) == NULL) return 2; zn_listen(accept, addr, port); zn_accept(accept, on_accept, NULL); printf("listening at %s:%d ...\n", addr, port); } zn_starttimer(zn_newtimer(S, on_summary, NULL), 1000); register_interrupted(); return zn_run(S, ZN_RUN_LOOP); }
static void on_error(void *ud, zn_State *S) { zn_Tcp *tcp = zn_newtcp(S); if (zn_connect(tcp, addr, port, on_connect, &client) != ZN_OK) zn_post(S, on_error, NULL); }