static err_t net_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) { lnet_userdata *ud = (lnet_userdata*)arg; if (!ud || ud->type != TYPE_TCP_SERVER || !ud->pcb) return ERR_ABRT; if (ud->self_ref == LUA_NOREF || ud->server.cb_accept_ref == LUA_NOREF) return ERR_ABRT; lua_State *L = lua_getstate(); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->server.cb_accept_ref); lnet_userdata *nud = net_create(L, TYPE_TCP_CLIENT); lua_pushvalue(L, 2); nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); nud->tcp_pcb = newpcb; tcp_arg(nud->tcp_pcb, nud); tcp_err(nud->tcp_pcb, net_err_cb); tcp_recv(nud->tcp_pcb, net_tcp_recv_cb); tcp_sent(nud->tcp_pcb, net_sent_cb); nud->tcp_pcb->so_options |= SOF_KEEPALIVE; nud->tcp_pcb->keep_idle = ud->server.timeout * 1000; nud->tcp_pcb->keep_cnt = 1; tcp_accepted(ud->tcp_pcb); lua_call(L, 1, 0); return net_connected_cb(nud, nud->tcp_pcb, ERR_OK); }
// Lua: net.createServer(type, timeout) int net_createServer( lua_State *L ) { int type, timeout; type = luaL_optlong(L, 1, TYPE_TCP); timeout = luaL_optlong(L, 2, 30); if (type == TYPE_UDP) { platform_print_deprecation_note("net.createServer with net.UDP type", "in next version"); return net_createUDPSocket( L ); } if (type != TYPE_TCP) return luaL_error(L, "invalid type"); lnet_userdata *u = net_create(L, TYPE_TCP_SERVER); u->server.timeout = timeout; return 1; }
int main(int argc, char *argv[]) { srand(time(0)); log_init("httpsrv.log", 1); log_set_level(LOG_LEVEL_DEBUG); DEBUG("httpsrv has started."); signal(SIGINT, sig_handler); alarm_init(); if(net_create(&net, "8080")) return -1; connmgr_init(net); while(1) net_process(net, -1); connmgr_close(); net_free(&net); alarm_close(); log_close(); return 0; }
// Lua: net.createConnection(type, secure) int net_createConnection( lua_State *L ) { int type, secure; type = luaL_optlong(L, 1, TYPE_TCP); secure = luaL_optlong(L, 2, 0); if (type == TYPE_UDP) { platform_print_deprecation_note("net.createConnection with net.UDP type", "in next version"); return net_createUDPSocket( L ); } if (type != TYPE_TCP) return luaL_error(L, "invalid type"); if (secure) { platform_print_deprecation_note("net.createConnection with secure flag", "in next version"); #ifdef TLS_MODULE_PRESENT return tls_socket_create( L ); #else return luaL_error(L, "secure connections not enabled"); #endif } net_create(L, TYPE_TCP_CLIENT); return 1; }
/* :<acct> NET <name> <address> [<nick> [<user> [:<gecos>]]] */ static int db_net(int parc, const char *parv[], void *priv) { struct a_network *net; int line = (int)priv; if (parc < 3 || parc > 6) { a_log(LERROR, "Bad NET line on %d", line); return 0; } net = net_create(parv[1]); if (net == NULL) { a_log(LERROR, "Failed to allocate network!"); return 0; } mowgli_strput(&net->nick, parv[0]); parse_server(net, parv[2]); if (parc > 3) mowgli_strput(&net->nick, parv[3]); if (parc > 4) mowgli_strput(&net->ident, parv[4]); if (parc > 5) mowgli_strput(&net->gecos, parv[5]); if (net_add_to_account(net, parv[0]) < 0) { a_log(LERROR, "Failed to add network %s to account %s", net->name, parv[0]); goto fail; } net_start_connect(net); return 0; fail: net_destroy(net); return 0; }
int main(int argc, char** argv) { struct net_service* ns; pthread_t ts[2]; struct net_event events[64]; struct buff_pool* pool; int i; int ret; void* status; unsigned int e; struct net_config cfg; int count; param_type parm; char bf[1024]; srand((unsigned)time(NULL)); pool = buff_pool_create(1024, 64); if(!pool) { printf("buff_pool_create faild\n"); exit(-1); } ns = net_create(1024 ); if(!ns) { printf("open net_service faild\n"); exit(-1); } for(i = 0; i < (int)(sizeof(ts)/sizeof(ts[0])); ++i) { pthread_create(ts + i, 0, thread_run, ns); } count = 0; while(1) { ret = net_queue(ns, events, sizeof(events)/sizeof(events[0])); if(ret < 0) { break; } if(ret == 0) { net_service_sleep(10); ++count; if(!net_connect(ns, "127.0.0.1", 9522)) { // printf("connect error\n"); fflush(stdout); } if(count % 100 == 0) { printf("-------------------------------- %d %d %d\n", net_socket_size(ns), msg_send_count, msg_recv_count); fflush(stdout); } continue; } for(i = 0; i < ret; ++i) { e = events[i].events; if(e & Eve_Error) { printf("socket error %d %d\n", events[i].nd, e); fflush(stdout); net_socket_close(ns, events[i].nd, 0); continue; } if(e & Eve_Connect) { cfg.enByte = enByte16; cfg.read_buff_cnt = 8; cfg.write_buff_cnt = 8; cfg.pool = pool; if(net_socket_cfg(ns, events[i].nd, &cfg) < 0) { net_socket_close(ns, events[i].nd, 0); } else { // add a connect parm = 1; net_socket_ctl(ns, events[i].nd , &parm); sprintf(bf, " s %d do some read write op %d %d\n", net_socket_size(ns), rand(), rand()); if(net_socket_write(ns, events[i].nd, bf, strlen(bf) + 1) > 0) { ++msg_send_count; } } } if(e & Eve_Read) { parm = net_socket_ctl(ns, events[i].nd, 0); if((rand() % 20480) < ((int)parm / 256) ) { net_socket_close(ns, events[i].nd, 1); } else { ++parm; net_socket_ctl(ns, events[i].nd, &parm); handle_client_read(ns, events[i].nd); } } } } for(i = 0; i < (int)(sizeof(ts)/sizeof(ts[0])); ++i) { pthread_join(ts[i], &status); } net_close(ns); buff_pool_release(pool); return 0; }
// Lua: s = net.createConnection(type, function(conn)) static int net_createConnection( lua_State* L ) { const char *mt = "net.socket"; return net_create(L, mt); }
// Lua: s = net.createServer(type, function(server)) static int net_createServer( lua_State* L ) { const char *mt = "net.server"; return net_create(L, mt); }
// Lua: net.createUDPSocket() int net_createUDPSocket( lua_State *L ) { net_create(L, TYPE_UDP_SOCKET); return 1; }
int main(int argc, char **argv) { int listen_fd; sp_session *session; static sp_session_config config; static sp_session_callbacks callbacks = { .logged_in = &sess_callback_logged_in, .logged_out = &sess_callback_logged_out, .metadata_updated = &sess_callback_metadata_updated, .connection_error = NULL, .message_to_user = &sess_callback_message_to_user, .notify_main_thread = &sess_callback_notify, .music_delivery = &player_callback_frame_delivery, .play_token_lost = &player_callback_playtoken_lost, .log_message = &sess_callback_log_message, .end_of_track = &player_callback_end_of_track, .streaming_error = NULL, .userinfo_updated = NULL, .start_playback = &player_callback_start_playback, .stop_playback = &player_callback_stop_playback, .get_audio_buffer_stats = &player_callback_get_audio_buffer_stats, // libspotify 10 .offline_status_updated = &sess_callback_offline_status_updated, .offline_error = &sess_callback_offline_error, // libspotify 11 .credentials_blob_updated = sess_callback_credentials_blob_updated, // libspotify 12 .connectionstate_updated = &sess_callback_connectionstate_updated, .scrobble_error = NULL, .private_session_mode_changed = NULL, }; thread_main = pthread_self(); /* Setup logging to stderr */ openlog(LIBSPOTIFY_USERAGENT, LOG_PERROR, LOG_USER); /** * Filter logging with one of these * setlogmask(LOG_UPTO(LOG_WARNING)); setlogmask(LOG_UPTO(LOG_NOTICE)); setlogmask(LOG_UPTO(LOG_DEBUG)); */ setlogmask(LOG_UPTO(LOG_INFO)); config.api_version = SPOTIFY_API_VERSION; config.cache_location = LIBSPOTIFY_CACHE_DIR; config.settings_location = LIBSPOTIFY_CACHE_DIR; config.application_key = g_appkey; config.application_key_size = sizeof(g_appkey); config.user_agent = LIBSPOTIFY_USERAGENT; config.callbacks = &callbacks; config.userdata = app_create(); config.compress_playlists = 1; config.dont_save_metadata_for_playlists = 0; config.initially_unload_playlists = 0; config.device_id = NULL; syslog(LOG_DEBUG, "MAIN: Initializing libspotify"); if(sp_session_create(&config, &session) != SP_ERROR_OK) { syslog(LOG_ERR, "MAIN: Unable to initialize libspotify"); app_release(); return -1; } app_set_session(session); if(argc == 4) { sp_link *link = sp_link_create_from_string(argv[3]); app_set_link(link); } /* This program will be run on mobile internet connections */ sp_session_set_connection_type(session, SP_CONNECTION_TYPE_MOBILE_ROAMING); sp_session_set_connection_rules(session, SP_CONNECTION_RULE_NETWORK|SP_CONNECTION_RULE_NETWORK_IF_ROAMING|SP_CONNECTION_RULE_ALLOW_SYNC_OVER_MOBILE); sp_session_preferred_offline_bitrate(session, SP_BITRATE_160k, 0); if(argc < 2) { char username[256]; if(sp_session_remembered_user(session, username, sizeof(username)) > 0) syslog(LOG_DEBUG, "MAIN: Attempting to login using stored credentials for user '%s'", username); if(sp_session_relogin(session) == SP_ERROR_NO_CREDENTIALS) { syslog(LOG_ERR, "MAIN: No credentials stored. Please run: %s <username> <password>", argv[0]); app_release(); return -1; } } else { syslog(LOG_DEBUG, "MAIN: Attempting to login using command line credentials"); sp_session_login(session, argv[1], argc == 3? argv[2]: NULL, 1, get_auth_blob()); } if((listen_fd = net_create(CTRL_TCP_PORT)) < 0) { syslog(LOG_ERR, "MAIN: Failed to initialize external network"); app_release(); return -1; } mainloop(session, listen_fd); syslog(LOG_INFO, "MAIN: Outside main event loop, good bye!"); net_release(listen_fd); app_release(); return 0; }
struct net_t *net_create_from_config(struct config_t *config, char *name) { int routing_type = 0; struct net_t *net; char *section; char section_str[MAX_STRING_SIZE]; int def_input_buffer_size; int def_output_buffer_size; int def_bandwidth; /* Create network */ net = net_create(name); /* Main section */ snprintf(section_str, sizeof section_str, "Network.%s", name); for (section = config_section_first(config); section; section = config_section_next(config)) { if (strcasecmp(section, section_str)) continue; net->def_input_buffer_size = config_read_int(config, section, "DefaultInputBufferSize", 0); net->def_output_buffer_size = config_read_int(config, section, "DefaultOutputBufferSize", 0); def_bandwidth = config_read_int(config, section, "DefaultBandwidth", 0); if (!net->def_input_buffer_size) fatal("%s:%s: DefaultInputBufferSize: invalid/missing value.\n%s", net->name, section, net_err_config); if (!net->def_output_buffer_size) fatal("%s:%s: DefaultOutputBufferSize: invalid/missing value.\n%s", net->name, section, net_err_config); if (!def_bandwidth) fatal("%s:%s: DefaultBandwidth: invalid/missing value.\n%s", net->name, section, net_err_config); def_output_buffer_size = net->def_output_buffer_size; def_input_buffer_size = net->def_input_buffer_size; } /* Nodes */ for (section = config_section_first(config); section; section = config_section_next(config)) { char *delim = "."; char *token; char *node_name; char *node_type; int input_buffer_size; int output_buffer_size; int bandwidth; int lanes; /* BUS lanes */ /* First token must be 'Network' */ snprintf(section_str, sizeof section_str, "%s", section); token = strtok(section_str, delim); if (!token || strcasecmp(token, "Network")) continue; /* Second token must be the name of the network */ token = strtok(NULL, delim); if (!token || strcasecmp(token, name)) continue; /* Third token must be 'Node' */ token = strtok(NULL, delim); if (!token || strcasecmp(token, "Node")) continue; /* Get name */ node_name = strtok(NULL, delim); token = strtok(NULL, delim); if (!node_name || token) fatal("%s:%s: wrong format for node.\n%s", net->name, section, net_err_config); /* Get properties */ node_type = config_read_string(config, section, "Type", ""); input_buffer_size = config_read_int(config, section, "InputBufferSize", def_input_buffer_size); output_buffer_size = config_read_int(config, section, "OutputBufferSize", def_output_buffer_size); bandwidth = config_read_int(config, section, "BandWidth", def_bandwidth); lanes = config_read_int(config, section, "Lanes", 1); /* Create node */ if (!strcasecmp(node_type, "EndNode")) net_add_end_node(net, input_buffer_size, output_buffer_size, node_name, NULL); else if (!strcasecmp(node_type, "Switch")) net_add_switch(net, input_buffer_size, output_buffer_size, bandwidth, node_name); else if (!strcasecmp(node_type, "Bus")) { /* Right now we ignore the size of buffers. But we * can set it as the value for bus ports, making the * connecting switches asymmetric. */ if (input_buffer_size != def_input_buffer_size || output_buffer_size != def_output_buffer_size) fatal("%s:%s: BUS does not contain input/output buffers. " "Size values will be ignored \n", net->name, section); /* If the number of lanes is smaller than 1 produce * an error */ if (lanes < 1) fatal("%s:%s: BUS cannot have less than 1 number of lanes \n%s", net->name, section, net_err_config); net_add_bus(net, bandwidth, node_name, lanes); } else fatal("%s:%s: Type: invalid/missing value.\n%s", net->name, section, net_err_config); } /* Links */ for (section = config_section_first(config); section; section = config_section_next(config)) { char *delim = "."; char *token; char *link_name; char *link_type; char *src_node_name; char *dst_node_name; int bandwidth; int v_channel_count; int src_buffer_size; int dst_buffer_size; struct net_node_t *src_node; struct net_node_t *dst_node; /* First token must be 'Network' */ snprintf(section_str, sizeof section_str, "%s", section); token = strtok(section_str, delim); if (!token || strcasecmp(token, "Network")) continue; /* Second token must be the name of the network */ token = strtok(NULL, delim); if (!token || strcasecmp(token, name)) continue; /* Third token must be 'Link' */ token = strtok(NULL, delim); if (!token || strcasecmp(token, "Link")) continue; /* Fourth token must name of the link */ link_name = strtok(NULL, delim); token = strtok(NULL, delim); if (!link_name || token) fatal("%s: %s: bad format for link.\n%s", name, section, net_err_config); /* Fields */ link_type = config_read_string(config, section, "Type", "Unidirectional"); bandwidth = config_read_int(config, section, "Bandwidth", def_bandwidth); src_node_name = config_read_string(config, section, "Source", ""); dst_node_name = config_read_string(config, section, "Dest", ""); v_channel_count = config_read_int(config, section, "VC", 1); src_buffer_size = config_read_int(config, section, "SourceBufferSize", 0); dst_buffer_size = config_read_int(config, section, "DestBufferSize", 0); /* Nodes */ src_node = net_get_node_by_name(net, src_node_name); dst_node = net_get_node_by_name(net, dst_node_name); if (!src_node) fatal("%s: %s: %s: source node does not exist.\n%s", name, section, src_node_name, net_err_config); if (!dst_node) fatal("%s: %s: %s: destination node does not exist.\n%s", name, section, dst_node_name, net_err_config); /* If it is a link connection */ if (src_node->kind != net_node_bus && dst_node->kind != net_node_bus) { int link_src_bsize; int link_dst_bsize; if (v_channel_count >= 1) { if (!strcasecmp(link_type, "Unidirectional")) { link_src_bsize = (src_buffer_size)? src_buffer_size : src_node->output_buffer_size; link_dst_bsize = (dst_buffer_size) ?dst_buffer_size : dst_node->input_buffer_size; net_add_link(net, src_node, dst_node, bandwidth, link_src_bsize, link_dst_bsize, v_channel_count); } else if (!strcasecmp(link_type, "Bidirectional")) { net_add_bidirectional_link(net, src_node, dst_node, bandwidth, src_buffer_size, dst_buffer_size, v_channel_count); } } else fatal("%s: %s: Unacceptable number of virtual channels \n %s", name, section, net_err_config); } /* If is is a Bus Connection */ else { if (v_channel_count > 1) fatal("%s: %s: BUS can not have virtual channels. \n %s", name, section, net_err_config); if (!strcasecmp(link_type, "Unidirectional")) { if ((src_node->kind == net_node_bus && src_buffer_size) || (dst_node->kind == net_node_bus && dst_buffer_size)) { fatal ("%s: %s: Source/Destination BUS cannot have buffer. \n %s " ,name, section, net_err_config); } net_add_bus_port(net, src_node, dst_node, src_buffer_size, dst_buffer_size); } else if (!strcasecmp(link_type, "Bidirectional")) { net_add_bidirectional_bus_port(net, src_node, dst_node, src_buffer_size, dst_buffer_size); } } } /* initializing the routing table */ net_routing_table_initiate(net->routing_table); /* Routes */ for (section = config_section_first(config); section; section = config_section_next(config)) { char *delim = "."; char *token; char *token_endl; /* First token must be 'Network' */ snprintf(section_str, sizeof section_str, "%s", section); token = strtok(section_str, delim); if (!token || strcasecmp(token, "Network")) continue; /* Second token must be the name of the network */ token = strtok(NULL, delim); if (!token || strcasecmp(token, name)) continue; /* Third token must be 'Routes' */ token = strtok(NULL, delim); if (!token || strcasecmp(token, "Routes")) continue; token_endl = strtok(NULL, delim); if (token_endl) fatal("%s: %s: bad format for route.\n%s", name, section, net_err_config); /* Routes */ routing_type = 1; net_config_route_create(net, config, section); config_check(config); } /* Commands */ for (section = config_section_first(config); section; section = config_section_next(config)) { char *delim = "."; char *token; char *token_endl; /* First token must be 'Network' */ snprintf(section_str, sizeof section_str, "%s", section); token = strtok(section_str, delim); if (!token || strcasecmp(token, "Network")) continue; /* Second token must be the name of the network */ token = strtok(NULL, delim); if (!token || strcasecmp(token, name)) continue; /* Third token must be 'Commands' */ token = strtok(NULL, delim); if (!token || strcasecmp(token, "Commands")) continue; token_endl = strtok(NULL, delim); if (token_endl) fatal("%s: %s: bad format for Commands section.\n%s", name, section, net_err_config); /* Commands */ net_config_command_create(net, config, section); config_check(config); } /* If there is no route section, Floyd-Warshall calculates the * shortest path for all the nodes in the network */ if (routing_type == 0) net_routing_table_floyd_warshall(net->routing_table); /* Return */ return net; }