void *tb_stream_connection(void *data) { int *retval = malloc(sizeof(int)); tb_session_t *session = (tb_session_t*)data; LOG_S_INFO(session, "started thread"); //Create the connection and time the connection time. tb_start_time(session->connect_t); if(connect(session->sock_d, session->addr_info->ai_addr, session->addr_info->ai_addrlen) == -1) { perror("Error: tb_stream_connection"); *retval = -1; return retval; } tb_finish_time(session->connect_t); LOG_S_INFO(session, "Connected"); session->status = SESSION_CONNECTED; int sz = session->pack_size, rc, rmbytes = 0; //Begin timing, and start transfer. tb_start_time(session->transfer_t); while(session->total_bytes < session->data_size) { rmbytes = session->data_size - session->total_bytes; if(rmbytes < session->pack_size) { sz = rmbytes; } if((rc = send(session->sock_d, session->data + session->total_bytes, sz, 0)) == -1) { PRT_ERR("Error: tb_stream_connection"); *retval = -1; return retval; } session->total_bytes += rc; } tb_finish_time(session->transfer_t); LOG_S_INFO(session, "Ended Connection"); fprintf(stdout, "Session %d: sent %lld bytes", session->id, session->total_bytes); session->status = SESSION_COMPLETE; tb_print_times(session); *retval = 0; return retval; }
static int custom_cb(void *data, Ecore_Fd_Handler * fd_handler) { int fd; struct message_data *msg; //------- PRT_ERR("[gandan] %s: socket data received\n", __FUNCTION__); struct sockaddr_un client_address; int client_sockfd; int client_len; if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) { PRT_TRACE_ERR ("ecore_main_fd_handler_active_get error , return\n"); return 1; } fd = ecore_main_fd_handler_fd_get(fd_handler); msg = malloc(sizeof(struct message_data)); //------- if (msg == NULL) { PRT_TRACE_ERR("%s : Not enough memory", __FUNCTION__); return 1; } client_len = sizeof(client_address); client_sockfd = accept(fd, (struct sockaddr *)&client_address, (socklen_t *)&client_len); if (client_sockfd == -1) { PRT_TRACE_ERR("socket accept error"); free(msg); return -1; } read_message(client_sockfd, msg); PRT_ERR("[gandan] received data : %d\n", msg->val); PRT_ERR("[gandan] %s: socket data receive end\n", __FUNCTION__); free(msg); return 1; }
TIESRENGINECOREAPI_API short set_volume_flag(gmhmm_type *gv) { short volume_flag; // short volume = (amplitude_max - amplitude_min) >> 1; unsigned short volume = (unsigned short)(((long)gv->amplitude_max - (long)gv->amplitude_min) >> 1); if (volume < gv->low_vol_limit) { PRT_ERR(printf("volume too low (%d)!\n", volume)); volume_flag = TIesrEngineVolumeLow; } else { if (volume > gv->high_vol_limit ) { PRT_ERR(printf("volume too high (%d)!\n", volume)); volume_flag = TIesrEngineVolumeHigh; } else { PRT_ERR(printf("volume (%d) OK.\n", volume)); volume_flag = TIesrEngineVolumeOK; } } return volume_flag; }
int tb_set_epoll(tb_listener_t *listener) { listener->epoll = tb_create_e_poll(listener->sock_d, 64, 0, listener); if(listener->epoll == NULL) { PRT_ERR("Error: tb_set_epoll: Cannot create epoll"); listener->f_abort(listener); } listener->epoll->f_event = listener->protocol->f_ep_event; listener->epoll->w_time = -1; return 0; }
static int custom_server_init(void) { int fd; struct sockaddr_un serveraddr; PRT_ERR("[gandan] custom server init!!\n"); PRT_ERR("[gandan] %s: custom_server_init\n", __FUNCTION__); if (access(SAMPLE_SOCKET_PATH, F_OK) == 0) unlink(SAMPLE_SOCKET_PATH); fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { PRT_ERR("%s: socket create failed\n", __FUNCTION__); return -1; } bzero(&serveraddr, sizeof(struct sockaddr_un)); serveraddr.sun_family = AF_UNIX; strncpy(serveraddr.sun_path, SAMPLE_SOCKET_PATH, sizeof(serveraddr.sun_path)); if (bind(fd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr)) < 0) { PRT_ERR("%s: socket bind failed\n", __FUNCTION__); close(fd); return -1; } if (chmod((SAMPLE_SOCKET_PATH), (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) /* 0777 */ PRT_ERR("failed to change the socket permission"); if (listen(fd, 5) < 0) { PRT_ERR("failed to listen"); close(fd); return -1; } PRT_ERR("[gandan] socket create & listen ok\n"); return fd; }
tb_listener_t *tb_create_listener(ENDPOINT_TYPE type, char *addr, char *port, PROTOCOL protocol, int bufsize) { tb_listener_t *listener = malloc(sizeof(tb_listener_t)); listener->prot_id = protocol; //Listener thread. listener->__l_thread = malloc(sizeof(pthread_t)); //Setup network parameters. listener->bind_port = strdup(port); listener->bind_address = strdup(addr); listener->e_type = type; listener->protocol = tb_create_protocol(protocol); listener->sock_d = -1; listener->options = NULL; listener->curr_session = NULL; //Set the file to upload to NULL listener->fp = NULL; listener->bufsize = bufsize; listener->total_tx_rx = 0; listener->file_size = 0; listener->filename = NULL; listener->data = NULL; listener->num_send = 1; //Set the listener to NOT be destroyed on exit. //This is set to allow for external destruction //of the listener. Can be changed. listener->d_exit = 1; //Threading stuff //Lock and condition for stat collection pthread_mutex_init(&listener->stat_lock, NULL); pthread_cond_init(&listener->stat_cond, NULL); pthread_mutex_init(&listener->status_lock, NULL); //Initialize stats. listener->stats = malloc(sizeof(tb_prot_stats_t)); memset(listener->stats, 0, sizeof(tb_prot_stats_t)); listener->stats->n_stats = NULL; listener->stats->protocol = protocol; listener->stats->stat_time = tb_create_time(CLOCK_MONOTONIC); tb_other_info *info = malloc(sizeof(tb_other_info)); info->sys_tid = -1; info->l_status = TB_CREATED; listener->stats->other_info = (void*)info; listener->stats->id = -1; listener->read = 0; listener->epoll = NULL; //Set defaults for monitor and print listener->monitor = 1; listener->print_stats = 1; //Set CPU and thread info. listener->num_proc = get_nprocs(); listener->cpu_affinity = 0; listener->main_cpu_aff = 0; listener->sys_tid = -1; //Set defaults for command and exit command. listener->command = TB_CONTINUE; listener->s_tx_end = TB_EXIT; //Set status of listener. listener->status = TB_CREATED; //#ifndef _TB_LIBRARY //Set logging info listener->log_enabled = 1; //Save file name depends on client or server if(listener->e_type == SERVER) { listener->log_info = tb_create_flog("log_server", DATE); } else { listener->log_info = tb_create_flog("log_client", DATE); } //#else // listener->log_info = NULL; // listener->log_enabled = 0; //#endif //Set protocol options listener->options = malloc(sizeof(tb_options_t)); listener->options->protocol = protocol; listener->options->l3_r_b_size = 0; listener->options->l3_s_b_size = 0; listener->options->l4_s_b_size = 0; listener->options->l4_r_b_size = 0; //Setup multiple connections listener->session_list = malloc(sizeof(tb_session_list_t)); listener->session_list->current_max_id = -1; listener->session_list->start = NULL; listener->session_list->end = NULL; listener->session_list->nac_lock = malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(listener->session_list->nac_lock, NULL); listener->session_list->num_active_conn = malloc(sizeof(int)); *listener->session_list->num_active_conn = 0; listener->session_list->num_sessions = 0; //Setup timing listener->transfer_time = tb_create_time(CLOCK_MONOTONIC); listener->connect_time = tb_create_time(CLOCK_MONOTONIC); struct sigaction s_act; s_act.sa_sigaction = &sig_action; if(sigaction(SIGINT, &s_act, NULL) == -1) { PRT_ERR("Cannot set SIGINT"); } return listener; }
tb_listener_t *tb_create_endpoint(tb_test_params_t *params) { assert(params->address != NULL); assert(params->port != NULL); assert(params->type <= 3 && params->type >= 0); assert(params->protocol >= TCP && params->protocol <= DCCP); if(params->file_name == NULL) { assert(params->d_size > 0); } else { assert(params->d_size <= 0); } tb_listener_t *listener = tb_create_listener( params->type, params->address, params->port, params->protocol, params->pack_size); //File size is in KB. listener->file_size = params->d_size * 1024; listener->filename = params->file_name; if(params->pack_size == 0) { PRT_INFO("Pack size is 0, setting default (1408)"); LOG(listener, "Setting default pack size: 1408", LOG_INFO); listener->bufsize = 1408; } else { PRT_I_D("Using pack size %d", params->pack_size); listener->bufsize = params->pack_size; } //Transfer protocol options listener->options->l3_r_b_size = params->l3_r_b_size; listener->options->l3_s_b_size = params->l3_w_b_size; listener->options->l4_s_b_size = params->l4_w_b_size; listener->options->l4_r_b_size = params->l4_r_b_size; listener->options->control = params->control; listener->print_stats = params->print_stats; //Setup and create log. if(params->log_enable) { PRT_I_S("Log enabled, creating log", params->log_path); listener->log_info = tb_create_flog(params->log_path, DATE); if(listener->log_info == NULL) { PRT_ERR("Could not create log info"); } else { PRT_I_S("Log created", listener->log_info->file_path); listener->log_enabled = 1; } } listener->d_exit = 0; return listener; }
int tb_stream_server(tb_listener_t *listener) { PRT_INFO("Listening"); tb_listen(listener); listener->status = TB_LISTENING; while(!(listener->command & TB_E_LOOP)){ PRT_INFO("Accepting"); LOG(listener, "Accepting", LOG_INFO); tb_session_t *session = tb_create_server_session(); session->sock_d = accept(listener->sock_d, (struct sockaddr*)session->addr_in, &session->addr_len); //Start timing. tb_start_time(listener->transfer_time); if(session->sock_d == -1) { PRT_ERR("Error: tb_stream_server: accept"); tb_abort(listener); } LOG_ADD(listener, "Connected address:", session->addr_in); session->data = malloc(listener->bufsize); session->data_size = listener->bufsize; listener->curr_session = session; listener->status = TB_CONNECTED; do { tb_recv_data(listener, session); session->total_bytes += session->last_trans; listener->total_tx_rx += session->last_trans; } while(session->last_trans != 0 && !(listener->command & TB_E_LOOP)); tb_finish_time(listener->transfer_time); //Lock while destroying session and closing connection pthread_mutex_trylock(&listener->stat_lock); PRT_INFO("Session closed\n"); listener->status = TB_DISCONNECTED; //Close and destroy current session close(session->sock_d); tb_destroy_session(session); listener->curr_session = NULL; PRT_INFO("Session destroyed"); //Close connection listener->protocol->f_close(session->sock_d); pthread_mutex_unlock(&listener->stat_lock); listener->command = listener->s_tx_end; } listener->protocol->f_close(listener->sock_d); listener->sock_d = -1; return listener->total_tx_rx; }
int tb_stream_m_client(tb_listener_t *listener) { const int num_conn = 4; int x = 0; for(; x < num_conn; x++) { tb_session_t *session = tb_create_session_full(listener->bind_address, listener->bind_port, listener->protocol->type, 0); if(session == NULL) { PRT_ERR("Error creating session"); tb_abort(listener); } session->sock_d = socket(session->addr_info->ai_family, session->addr_info->ai_socktype, session->addr_info->ai_protocol); if(session->sock_d == -1) { perror("Error: tb_stream_client: "); tb_abort(listener); } //Set options for the new socket. //tb_set_sock_opt(listener->options, session->sock_d); //Set session parameters. session->l_status = (int*)&listener->status; session->l_txrx = &listener->total_tx_rx; //Allocate the session the appropriate amount of data //for the number of connections session->data = listener->data + ((listener->file_size / num_conn) * x); session->data_size = listener->file_size / num_conn; session->stats->protocol = listener->protocol->protocol; session->n_session = NULL; session->pack_size = listener->bufsize; //Add session to current list. tb_session_add(listener->session_list, session); fprintf(stdout, BLUE "Session %d allocated %zu bytes" RESET, session->id, session->data_size); //Add logging info session->log_enabled = 1; session->log_info = listener->log_info; PRT_I_D("Creating thread for session %d", session->id); //Send the thread on its merry way. pthread_create(&session->s_thread, NULL, &tb_stream_connection, (void*)session); if(listener->stats->n_stats == NULL) { listener->stats->n_stats = session->stats; } } //Wait for the first session to be connected. while(listener->session_list->start->status == SESSION_CREATED); tb_start_time(listener->transfer_time); listener->status = TB_CONNECTED; int *retval; tb_session_t *curr_session = listener->session_list->start; //Wait for each session to complete, then close the connection. while(curr_session != NULL) { PRT_I_D("Joining with session %d",curr_session->id); pthread_join(curr_session->s_thread, (void**)&retval); pthread_mutex_lock(&curr_session->stat_lock); close(curr_session->sock_d); pthread_mutex_unlock(&curr_session->stat_lock); curr_session = curr_session->n_session; } //Stop timing and disconnect. tb_finish_time(listener->transfer_time); listener->status = TB_DISCONNECTED; return listener->total_tx_rx; }
/* * 模块加载方法 */ static ini __init hello_init(void){ int err = -1; dev_t dev = 0; struct device* temp = NULL; printk(KERN_ALERT"Initializing hello device.\n"); /* * 动态分配主设备号和从设备号 */ err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME); if(err < 0){ printk(KERN_ALERT"Failed to alloc char dev region.\n"); goto fail; } hello_major = MAJOR(dev); hello_minor = MINOR(dev); /* * 分配hello设备结构体变量 */ hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL); if(!hello_dev){ err = -ENOMEM; printk(KERN_ALERT"Failed to alloc hello_dev.\n"); goto unregister; } /* * 初始化设备 */ err = __hello_setup_dev(hello_dev); if(err){ printk(KERN_ALERT"Failed to setup dev:%d.\n", err); goto cleanup; } /* * 在/sys/class/目录下创建设备类别目录hello */ hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME); if(IS_ERR(hello_class)){ err = PTR_ERR(hello_class); printk(KERN_ALERT"Failed to create hello class.\n"); goto destroy_cdev; } /* * 在/dev/目录和/sys/class/hello目录下分别创建设备文件hello */ temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME); if(IS_ERR(temp)){ err = PRT_ERR(temp); printk(KERN_ALERT"Failed to create hello device.\n"); goto destroy_class; } /* * 在/sys/class/hello/hello目录下创建属性文件val */ err = device_create_file(temp, &dev_attr_val); if(err < 0){ printk(KERN_ALERT"Failed to create attribute val.\n"); goto destroy_device; } dev_set_drvdata(temp, hello_dev); /* * 创建/proc/hello文件 */ hello_create_proc(); printk(KERN_ALERT"Successed to initalize hello device.\n"); return 0; destroy_device: device_destroy(hello_class, dev); destroy_class: class_destroy(hello_class); destroy_cdev: cdev_del(&(hello_dev->dev)); cleanup: kfree(hello_dev); unregister: unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1); fail: return err; }