📄 alc_session.c
字号:
/* $Author: peltotas $ $Date: 2006/03/16 11:05:00 $ $Revision: 1.61 $ *//* * MAD-ALCLIB: Implementation of ALC/LCT protocols, Compact No-Code FEC, * Simple XOR FEC, Reed-Solomon FEC, and RLC Congestion Control protocol. * Copyright (c) 2003-2006 TUT - Tampere University of Technology * main authors/contacts: jani.peltotalo@tut.fi and sami.peltotalo@tut.fi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include "inc.h"/**** Set global variables ****/ struct alc_session *alc_session_list[MAX_ALC_SESSIONS];int nb_alc_session = 0;/* * This function creates and initializes new session. * * Params: alc_arguments_t *a: ALC specific commandline arguments. * * Return: int: Identifier for created session in success, -1 otherwise * */int open_alc_session(alc_arguments_t *a) {#ifndef WIN32 pthread_attr_t attr;#endif alc_session_t *s; int i; int retval; char fullpath[MAX_PATH]; if(!lib_init) { alc_init(); /* alc session list initialization */ memset(alc_session_list, 0, MAX_ALC_SESSIONS * sizeof(alc_session_t*)); } if(nb_alc_session >= MAX_ALC_SESSIONS) { /* Could not create new alc session */ printf("Could not create new alc session: too many sessions!\n"); return -1; } if (!(s = (alc_session_t*)calloc(1, sizeof(alc_session_t)))) { printf("Could not alloc memory for alc session!\n"); return -1; } s->mode = a->mode; s->tsi = a->tsi; s->state = SActive; s->addr_family = a->addr_family; s->addr_type = a->addr_type; s->cc_id = a->cc_id; if(s->cc_id == RLC) { retval = init_mad_rlc(s); if(retval < 0) { return retval; } } s->rx_memory_mode = a->rx_memory_mode; s->verbosity = a->verbosity; s->starttime = a->start_time; s->stoptime = a->stop_time; s->simul_losses = a->simul_losses; s->loss_ratio1 = a->loss_ratio1; s->loss_ratio2 = a->loss_ratio2; if(s->mode == SENDER) { memcpy(s->base_dir, a->base_dir, strlen(a->base_dir)); s->nb_channel = 0; s->max_channel = a->nb_channel; s->def_fec_ratio = a->fec_ratio; s->sent_bytes = 0; s->obj_sent_bytes = 0; s->last_print_tx_percent = 0; s->a_flag = 0; s->fdt_instance_id = 0; /*uplimit 16777215;*/ s->def_ttl = a->ttl; s->def_fec_enc_id = a->fec_enc_id; s->def_fec_inst_id = a->fec_inst_id; s->def_max_sblen = a->max_sb_len; s->def_eslen = a->es_len; s->use_fec_oti_ext_hdr = a->use_fec_oti_ext_hdr; s->def_tx_rate = a->tx_rate; s->tx_queue_begin = NULL; s->tx_queue_end = NULL; s->tx_queue_size = 0; s->first_unit_in_loop = true; s->nb_ready_channel = 0; s->nb_sending_channel = 0; s->half_word = a->half_word; s->encode_content = a->encode_content; s->optimize_tx_rate = a->optimize_tx_rate; s->calculate_session_size = a->calculate_session_size; } if(s->mode == RECEIVER) {#ifdef SSM s->ssm = a->use_ssm;#endif#ifdef WIN32 memset(fullpath, 0, MAX_PATH); if(_fullpath(fullpath, a->base_dir, MAX_PATH) != NULL) { memcpy(s->base_dir, fullpath, strlen(fullpath)); } else { memcpy(s->base_dir, a->base_dir, strlen(a->base_dir)); }#else memset(fullpath, 0, MAX_PATH); if(a->base_dir[0] != '/') { if(getcwd(fullpath, MAX_PATH) != NULL) { memcpy(s->base_dir, fullpath, strlen(fullpath)); strcat(s->base_dir, "/"); strcat(s->base_dir, a->base_dir); } else { memcpy(s->base_dir, a->base_dir, strlen(a->base_dir)); } } else { memcpy(s->base_dir, a->base_dir, strlen(a->base_dir)); }#endif s->nb_channel = 0; s->max_channel = a->nb_channel; s->obj_list = NULL; s->fdt_list = NULL; s->wanted_obj_list = NULL; s->rx_fdt_instance_list = NULL; s->accept_expired_fdt_inst = a->accept_expired_fdt_inst; memset(s->src_addr, 0, 40); if(a->src_addr != NULL) { memcpy(s->src_addr, a->src_addr, strlen(a->src_addr)); } /* Create receiving thread */#ifdef WIN32 if(_beginthread((void*)rx_thread, 0, (void*)s) < 0) { perror("open_alc_session: _beginthread"); return -1; }#else pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (pthread_create(&s->rx_thread_id, &attr, rx_thread, (void*)s) != 0) { perror("open_alc_session: pthread_create"); return -1; }#endif } for(i = 0; i < MAX_ALC_SESSIONS; i++) { if(alc_session_list[i] == NULL) { s->s_id = i; alc_session_list[s->s_id] = s; break; } } nb_alc_session++; return s->s_id;}/* * This function closes session. * * Params: int s_id: Session identifier * * Return: void * */void close_alc_session(int s_id) { int i, ret_val; wanted_obj_t *next_want; wanted_obj_t *want; rx_fdt_instance_t *next_instance; rx_fdt_instance_t *instance; tx_queue_t *next_pkt; tx_queue_t *pkt; trans_obj_t *to; alc_session_t *s = alc_session_list[s_id]; s->state = SClosed;#ifdef WIN32 Sleep(1000);#else sleep(1);#endif for(i = 0; i < s->max_channel; i++) { if(s->ch_list[i] != NULL) { ret_val = close_alc_channel(s->ch_list[i], s); } } /* Closing, free all uncompleted objects, uncompleted fdt instances and wanted obj list */ to = s->obj_list; while(to != NULL) { free_object(to, s, 1); to = s->obj_list; } to = s->fdt_list; while(to != NULL) { free_object(to, s, 0); to = s->fdt_list; } want = s->wanted_obj_list; while(want != NULL) { next_want = want->next; free(want); want = next_want; } instance = s->rx_fdt_instance_list; while(instance != NULL) { next_instance = instance->next; free(instance); instance = next_instance; } if(s->cc_id == RLC) { close_mad_rlc(s); } pkt = s->tx_queue_begin; while(pkt != NULL) { next_pkt = pkt->next; free(pkt->data); free(pkt); pkt = next_pkt; } free(s); alc_session_list[s_id] = NULL; nb_alc_session--;}/* * This function returns session from session_list. * * Params: int s_id: Session identifier * * Return: alc_session_t*: Pointer to session, NULL if session does not exist. * */alc_session_t* get_alc_session(int s_id) { return alc_session_list[s_id]; }/* * This function returns state of session. * * Params: int s_id: Session identifier * * Return: int: State of the session, -1 if session does not exist anymore. * */int get_session_state(int s_id) { if(alc_session_list[s_id] == NULL) { return -1; } return alc_session_list[s_id]->state;}/* * This function sets state of session. * * Params: int s_id: Session identifier, * enum alc_session_states state: New session state * * Return: void * */void set_session_state(int s_id, enum alc_session_states state) { alc_session_list[s_id]->state = state;}/* * This function sets state for all sessions. * * Params: enum alc_session_states state: New session state * * Return: void * */void set_session_state2(enum alc_session_states state) { int i; for(i = 0; i < MAX_ALC_SESSIONS; i++) { if(alc_session_list[i] != NULL) { alc_session_list[i]->state = state; } }}/* * This function returns state of A flag in session. * * Params: int s_id: Session identifier * * Return: int: State of A flag (0 = not set, 1 = set) * */int get_session_a_flag(int s_id) { return alc_session_list[s_id]->a_flag;}/* * This function sets state of A flag in session. * * Params: int s_id: Session identifier * * Return: void * */void set_session_a_flag(int s_id) { alc_session_list[s_id]->a_flag = 1;}/* * This function returns session's FDT Instance ID. * * Params: int s_id: Session identifier * * Return: unsigned int: Session's FDT Instance ID * */unsigned int get_fdt_instance_id(int s_id) { return alc_session_list[s_id]->fdt_instance_id;}/* * This function sets session's FDT Instance ID. * * Params: int s_id: Session identifier, * unsigned int: FDT Instance ID to be set * * Return: void * */void set_fdt_instance_id(int s_id, unsigned int instance_id) { alc_session_list[s_id]->fdt_instance_id = (instance_id & 0x00FFFFFF);}/* * This function returns number of bytes sent in the session. * * Params: int s_id: Session identifier * * Return: ULONGLONG/unsigned long long: Number of bytes sent in session since latest zeroing * */ #ifdef WIN32ULONGLONG get_session_sent_bytes(int s_id) {#elseunsigned long long get_session_sent_bytes(int s_id) {#endif return alc_session_list[s_id]->sent_bytes;} /* * This function returns number of bytes sent for the object. * * Params: int s_id: Session identifier * * Return: ULONGLONG/unsigned long long: Number of bytes sent in session since latest zeroing * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -