int main(int argc, char **argv) { int i, err; int nb_writers, nb_readers; pthread_t th; pthread_t thr[MAX_THREADS]; pthread_t thw[MAX_THREADS]; if(argc != 3) { printf("usage : %s <nb_writers> <nb_readers>\n",argv[0]); return EXIT_FAILURE; } srand(time(NULL)); nb_writers = atoi(argv[1]); nb_readers = atoi(argv[2]); chan_s = channel_create(sizeof(msg_t),NB_MSG,CHANNEL_PROCESS_NONBLOCK); chan_r = channel_create(sizeof(msg_t),NB_MSG,CHANNEL_PROCESS_NONBLOCK); // Create every threads for(i = 0; i < nb_writers; i++) { err = pthread_create(&thw[i],NULL,sendm,NULL); if(err != 0) perror("pthread_create"); } for(i = 0; i < nb_readers; i++) { pthread_create(&thr[i],NULL,receive,NULL); } pthread_create(&th,NULL,forward,NULL); printf("Working...\n"); // Wait for every threads for(i = 0; i < nb_writers; i++) { pthread_join(thw[i],NULL); } for(i = 0; i < nb_readers; i++) { pthread_join(thr[i],NULL); } pthread_join(th,NULL); channel_destroy(chan_s); channel_destroy(chan_r); printf("End of program\n"); return EXIT_SUCCESS; }
static int offer_session(struct respoke_session *session) { if (session->channel) { return 0; } if (!(session->channel = channel_create(session, AST_STATE_RING, respoke_session_get_exten(session), NULL, NULL))) { return -1; } switch (ast_pbx_start(session->channel)) { case AST_PBX_CALL_LIMIT: ast_log(LOG_WARNING, "PBX call limit reached\n"); case AST_PBX_FAILED: ast_log(LOG_WARNING, "Failed to start PBX\n"); ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION); ast_hangup(session->channel); return -1; case AST_PBX_SUCCESS: break; } ast_debug(3, "Started PBX on new RESPOKE channel %s\n", ast_channel_name(session->channel)); return 0; }
static void chttp2_init_client_micro_fullstack(grpc_end2end_test_fixture *f, grpc_channel_args *client_args) { micro_fullstack_fixture_data *ffd = f->fixture_data; grpc_connectivity_state conn_state; grpc_connected_subchannel *connected; char *ipv4_localaddr; gpr_asprintf(&ipv4_localaddr, "ipv4:%s", ffd->localaddr); ffd->master_channel = channel_create(ipv4_localaddr, client_args, &ffd->sniffed_subchannel); gpr_free(ipv4_localaddr); gpr_log(GPR_INFO, "MASTER CHANNEL %p ", ffd->master_channel); /* the following will block. That's ok for this test */ conn_state = grpc_channel_check_connectivity_state(ffd->master_channel, 1 /* try to connect */); GPR_ASSERT(conn_state == GRPC_CHANNEL_IDLE); /* here sniffed_subchannel should be ready to use */ GPR_ASSERT(conn_state == GRPC_CHANNEL_IDLE); GPR_ASSERT(ffd->sniffed_subchannel != NULL); connected = connect_subchannel(ffd->sniffed_subchannel); f->client = grpc_client_uchannel_create(ffd->sniffed_subchannel, client_args); grpc_client_uchannel_set_connected_subchannel(f->client, connected); gpr_log(GPR_INFO, "CHANNEL WRAPPING SUBCHANNEL: %p(%p)", f->client, ffd->sniffed_subchannel); GPR_ASSERT(f->client); }
/*========================================== * Reads channels *------------------------------------------*/ static bool channel_read_channeldb(char* str[], int columns, int current) {// <channel name>,<password>,<type>,<color>,<operator id> int type, color, op; struct channel_data *cd; type = atoi(str[2]); color = atoi(str[3]); op = atoi(str[4]); if( type < 0 || type > CHN_SERVER || (type < CHN_USER && server_channel[type]) ) { ShowWarning("channel_read_channeldb: Invalid channel type or type already in use (channel %s type %d).\n", str[0], type); return false; } if( (cd = (struct channel_data *)strdb_get(channel_db, str[0])) != NULL ) { ShowWarning("channel_read_channeldb: Channel %s already exists.\n", str[0]); return false; } if( !channel_create(NULL, str[0], str[1], type, color, op) ) { ShowWarning("channel_read_channeldb: Failed to create Channel %s.\n", str[0]); return false; } return true; }
/* * core_channel_open (response) * ----------------- * * Handles the response to a request to open a channel. * * This function takes the supplied channel identifier and creates a * channel list entry with it. * * req: TLV_TYPE_CHANNEL_ID -- The allocated channel identifier */ DWORD remote_response_core_channel_open(Remote *remote, Packet *packet) { DWORD res = ERROR_SUCCESS, channelId; Channel *newChannel; do { channelId = packet_get_tlv_value_uint(packet, TLV_TYPE_CHANNEL_ID); // DId the request fail? if (!channelId) { res = ERROR_NOT_ENOUGH_MEMORY; break; } // Create a local instance of the channel with the supplied identifier if (!(newChannel = channel_create(channelId, 0))) { res = ERROR_NOT_ENOUGH_MEMORY; break; } } while (0); return res; }
/* * core_channel_open * ----------------- * * Opens a channel with the remote endpoint. The response handler for this * request will establish the relationship on the other side. * * opt: TLV_TYPE_CHANNEL_TYPE * The channel type to allocate. If set, the function returns, allowing * a further up extension handler to allocate the channel. */ DWORD remote_request_core_channel_open(Remote *remote, Packet *packet) { Packet *response; DWORD res = ERROR_SUCCESS; Channel *newChannel; PCHAR channelType; DWORD flags = 0; do { dprintf( "[CHANNEL] Opening new channel for packet %p", packet ); // If the channel open request had a specific channel type if ((channelType = packet_get_tlv_value_string(packet, TLV_TYPE_CHANNEL_TYPE))) { res = ERROR_NOT_FOUND; break; } // Get any flags that were supplied flags = packet_get_tlv_value_uint(packet, TLV_TYPE_FLAGS); dprintf( "[CHANNEL] Opening %s %u", channelType, flags ); // Allocate a response response = packet_create_response(packet); // Did the response allocation fail? if ((!response) || (!(newChannel = channel_create(0, flags)))) { res = ERROR_NOT_ENOUGH_MEMORY; break; } dprintf( "[CHANNEL] Opened %s %u", channelType, flags ); // Get the channel class and set it newChannel->cls = packet_get_tlv_value_uint(packet, TLV_TYPE_CHANNEL_CLASS); dprintf( "[CHANNEL] Channel class for %s: %u", channelType, newChannel->cls ); // Add the new channel identifier to the response if ((res = packet_add_tlv_uint(response, TLV_TYPE_CHANNEL_ID, channel_get_id(newChannel))) != ERROR_SUCCESS) break; // Transmit the response dprintf( "[CHANNEL] Sending response for %s", channelType ); res = PACKET_TRANSMIT(remote, response, NULL); dprintf( "[CHANNEL] Done" ); } while (0); return res; }
/* * Network definition */ static mpegts_service_t * tsfile_network_create_service ( mpegts_mux_t *mm, uint16_t sid, uint16_t pmt_pid ) { pthread_mutex_lock(&tsfile_lock); mpegts_service_t *s = mpegts_service_create1(NULL, mm, sid, pmt_pid, NULL); pthread_mutex_unlock(&tsfile_lock); // TODO: HACK: REMOVE ME if (s) { channel_t *c = channel_create(NULL, NULL, NULL); service_mapper_link((service_t*)s, c); } return s; }
void midi_journal_add_control( journal_t *journal, uint32_t seq, midi_control_t *midi_control) { unsigned char channel = 0; unsigned char controller = 0; if( ! journal ) return; if( ! midi_control ) return; channel = midi_control->channel; if( channel > MAX_MIDI_CHANNELS ) return; controller = midi_control->controller_number; if( controller > (MAX_CHAPTER_C_CONTROLLERS - 1) ) return; // Set Journal Header A and S flags journal->header->bitfield |= ( JOURNAL_HEADER_A_FLAG | JOURNAL_HEADER_S_FLAG ); if( ! journal->channels[ channel ] ) { channel_t *channel_journal = channel_create(); if( ! channel_journal ) return; journal->channels[ channel ] = channel_journal; } if( ! journal->channels[ channel ]->chapter_c ) { journal->channels[ channel ]->chapter_c = chapter_c_create(); } // Set flag to show that chapter C is present journal->channels[ channel ]->header->bitfield |= CHAPTER_C; if( journal->channels[ channel ]->header->chan != ( channel + 1 ) ) { journal->channels[ channel ]->header->chan = ( channel + 1 ); journal->header->totchan +=1; } journal->header->seq = seq; journal->channels[ channel]->chapter_c->controller_log[ controller ].S = 1; journal->channels[ channel]->chapter_c->controller_log[ controller ].number = controller; journal->channels[ channel]->chapter_c->controller_log[ controller ].value = midi_control->controller_value; }
/* * Creates a stream-based channel with initialized operations. An example of * streaming channel is TCP. */ LINKAGE Channel *channel_create_stream(DWORD identifier, DWORD flags, StreamChannelOps *ops) { Channel *channel = channel_create(identifier, flags); if (channel) { channel->cls = CHANNEL_CLASS_STREAM; if (ops) memcpy(&channel->ops.stream, ops, sizeof(StreamChannelOps)); else memset(&channel->ops, 0, sizeof(channel->ops)); } return channel; }
/* * Creates a datagram-based channel with initialized operations. An example of * a datagram channel is UDP. */ LINKAGE Channel *channel_create_datagram(DWORD identifier, DWORD flags, DatagramChannelOps *ops) { Channel *channel = channel_create(identifier, flags); if (channel) { channel->cls = CHANNEL_CLASS_DATAGRAM; if (ops) memcpy(&channel->ops.datagram, ops, sizeof(DatagramChannelOps)); else memset(&channel->ops, 0, sizeof(channel->ops)); } return channel; }
/* * Creates a pool-based channel with initialized operations. An example of a * pool channel is one that operates on a file. */ LINKAGE Channel *channel_create_pool(DWORD identifier, DWORD flags, PoolChannelOps *ops) { Channel *channel = channel_create(identifier, flags); if (channel) { channel->cls = CHANNEL_CLASS_POOL; if (ops) memcpy(&channel->ops.pool, ops, sizeof(PoolChannelOps)); else memset(&channel->ops, 0, sizeof(channel->ops)); } return channel; }
void midi_journal_add_program( journal_t *journal, uint32_t seq, midi_program_t *midi_program) { unsigned char channel = 0; if( ! journal ) return; if( ! midi_program ) return; channel = midi_program->channel; if( channel > MAX_MIDI_CHANNELS ) return; // Set Journal Header A and S flags journal->header->bitfield |= ( JOURNAL_HEADER_A_FLAG | JOURNAL_HEADER_S_FLAG ); if( ! journal->channels[ channel ] ) { channel_t *channel_journal = channel_create(); if( ! channel_journal ) return; journal->channels[ channel ] = channel_journal; } if( ! journal->channels[ channel ]->chapter_p ) { journal->channels[ channel ]->chapter_p = chapter_p_create(); } // Set flag to show that chapter P is present journal->channels[ channel ]->header->bitfield |= CHAPTER_P; if( journal->channels[ channel ]->header->chan != ( channel + 1 ) ) { journal->channels[ channel ]->header->chan = ( channel + 1 ); journal->header->totchan +=1; } journal->header->seq = seq; journal->channels[ channel]->chapter_p->S = 1; journal->channels[ channel]->chapter_p->B = 0; journal->channels[ channel]->chapter_p->program = midi_program->program; journal->channels[ channel]->chapter_p->X =0; journal->channels[ channel]->chapter_p->bank_msb = 0; journal->channels[ channel]->chapter_p->bank_lsb = 0; }
/* create a new channel */ static int luaproc_create_channel( lua_State *L ) { const char *chname = luaL_checkstring( L, 1 ); channel *chan = channel_locked_get( chname ); if (chan != NULL) { /* does channel exist? */ /* unlock the channel mutex locked by channel_locked_get */ luaproc_unlock_channel( chan ); /* return an error to lua */ lua_pushnil( L ); lua_pushfstring( L, "channel '%s' already exists", chname ); return 2; } else { /* create channel */ channel_create( chname ); lua_pushboolean( L, TRUE ); return 1; } }
static int api_channel_create ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp ) { htsmsg_t *conf; channel_t *ch; if (!(conf = htsmsg_get_map(args, "conf"))) return EINVAL; pthread_mutex_lock(&global_lock); ch = channel_create(NULL, conf, NULL); if (ch) channel_save(ch); pthread_mutex_unlock(&global_lock); return 0; }
/** * Allocates the proper settings depending on the channel type * @param name: Channel name, can't be null * @param pass: Channel password, can be null * @param chantype: Channel type * @param owner: Owner ID * @return NULL on failure or Channel on success */ struct Channel* channel_create_simple(char *name, char *pass, enum Channel_Type chantype, unsigned int owner) { struct Channel tmp_chan; memset(&tmp_chan, 0, sizeof(struct Channel)); switch (chantype) { case CHAN_TYPE_ALLY: memcpy(&tmp_chan, &channel_config.ally_tmpl, sizeof(channel_config.ally_tmpl)); tmp_chan.gid = (int)owner; break; case CHAN_TYPE_MAP: memcpy(&tmp_chan, &channel_config.map_tmpl, sizeof(channel_config.map_tmpl)); tmp_chan.m = (uint16)owner; break; case CHAN_TYPE_PUBLIC: safestrncpy(tmp_chan.name, name+1, sizeof(tmp_chan.name)); if (pass) safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass)); else tmp_chan.pass[0] = '\0'; safestrncpy(tmp_chan.alias, name, sizeof(tmp_chan.name)); tmp_chan.type = CHAN_TYPE_PUBLIC; tmp_chan.opt = CHAN_OPT_BASE; tmp_chan.msg_delay = 1000; tmp_chan.color = channel_getColor("Default"); break; case CHAN_TYPE_PRIVATE: safestrncpy(tmp_chan.name, name+1, sizeof(tmp_chan.name)); if (pass) safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass)); else tmp_chan.pass[0] = '\0'; safestrncpy(tmp_chan.alias, name, sizeof(tmp_chan.name)); tmp_chan.type = CHAN_TYPE_PRIVATE; tmp_chan.opt = channel_config.private_channel.opt; tmp_chan.msg_delay = channel_config.private_channel.delay; tmp_chan.color = channel_config.private_channel.color; tmp_chan.char_id = owner; break; default: return NULL; } return channel_create(&tmp_chan); }
/* * Completion routine for opening a TCP channel */ DWORD portfwd_open_tcp_channel_complete(Remote *remote, Packet *packet, LPVOID context, LPCSTR method, DWORD res) { PortForwardClientContext *pcctx = (PortForwardClientContext *)context; if (res == ERROR_SUCCESS) { DWORD channelId = packet_get_tlv_value_uint(packet, TLV_TYPE_CHANNEL_ID); // Create a channel from the response if (channelId) pcctx->channel = channel_create(channelId); // Override the default dio handler if (pcctx->channel) { channel_set_type(pcctx->channel, "network_tcp"); channel_set_local_io_handler(pcctx->channel, pcctx, portfwd_client_dio); } // Create a waitable event for this client connection // and insert it into the scheduler's waitable list if ((pcctx->notify = WSACreateEvent())) { WSAEventSelect(pcctx->clientFd, pcctx->notify, FD_READ|FD_CLOSE); scheduler_insert_waitable(pcctx->notify, pcctx, (WaitableNotifyRoutine)portfwd_local_client_notify); } } else { console_generic_response_output(remote, packet, "NETWORK", "open_tcp_channel"); portfwd_destroy_client(pcctx); } return ERROR_SUCCESS; }
static void _accept_handler(aeEventLoop *el, fd_t fd, void *privdata, int mask) { acceptor_t *acceptor = (acceptor_t*)privdata; fd_t accepted = _INVALIDFD; unsigned int ip = 0; unsigned short port = 0; channel_t *channel = NULL; if(0!=aesocaccept(acceptor->fd, &accepted, &ip, &port)){ return; } channel = channel_create(accepted, ip, port, 0); if(NULL==channel){ aesocclose(accepted); return; } _notify_channel(acceptor, channel); }
/* * Allocate a new channel that is associated with a local file on disk * and send the new channel identifier response to the requestor */ DWORD request_fs_file_open(Remote *remote, Packet *packet) { Packet *response = NULL; PCHAR filePath, modeString, channelType; DWORD res = ERROR_SUCCESS; Channel *newChannel; FileContext *ctx; DWORD mode; do { // If this is not an FS channel open, ignore it. if ((!(channelType = packet_get_tlv_value_string(packet, TLV_TYPE_CHANNEL_TYPE))) || (strcmp(channelType, "fs"))) { res = ERROR_NOT_FOUND; break; } // Allocate a response response = packet_create_response(packet); // Check the response allocation & allocate a un-connected // channel if ((!response) || (!(newChannel = channel_create(0))) || (!(ctx = (FileContext *)malloc(sizeof(FileContext))))) { res = ERROR_NOT_ENOUGH_MEMORY; break; } // Get the file path and the mode filePath = packet_get_tlv_value_string(packet, TLV_TYPE_FS_PATH); mode = packet_get_tlv_value_uint(packet, TLV_TYPE_FS_MODE); // Determine the actual mode switch (mode) { case FILE_MODE_READ: modeString = "rb"; break; case FILE_MODE_WRITE: modeString = "wb"; break; case FILE_MODE_READWRITE: modeString = "rwb"; break; } // Invalid file? if ((!filePath) || (!(ctx->fd = fopen(filePath, modeString)))) { res = (filePath) ? ERROR_FILE_NOT_FOUND : GetLastError(); break; } // Set the direct I/O handler for this channel to the file // channel direct I/O handler. channel_set_local_io_handler(newChannel, ctx, (DirectIoHandler)file_channel_dio_handler); // Add the channel identifier to the response packet_add_tlv_uint(response, TLV_TYPE_CHANNEL_ID, channel_get_id(newChannel)); } while (0); // Transmit the packet if it's valid if (response) { packet_add_tlv_uint(response, TLV_TYPE_RESULT, res); packet_transmit(remote, response, NULL); } // Clean up on failure if (res != ERROR_SUCCESS) { if (newChannel) channel_destroy(newChannel); if (ctx) free(ctx); } return res; }
void midi_journal_add_note( journal_t *journal, uint32_t seq, midi_note_t *midi_note) { uint16_t note_slot = 0; chapter_n_note_t *new_note = NULL; unsigned char channel = 0; if( ! journal ) return; if( ! midi_note ) return; channel = midi_note->channel; if( channel > MAX_MIDI_CHANNELS ) return; // Set Journal Header A and S flags journal->header->bitfield |= ( JOURNAL_HEADER_A_FLAG | JOURNAL_HEADER_S_FLAG ); if( ! journal->channels[ channel ] ) { channel_t *channel_journal = channel_create(); if( ! channel_journal ) return; journal->channels[ channel ] = channel_journal; } if( ! journal->channels[ channel ]->chapter_n ) { journal->channels[ channel ]->chapter_n = chapter_n_create(); } journal->channels[ channel ]->header->bitfield |= CHAPTER_N; journal->channels[ channel ]->chapter_n->header->B = 1; if( journal->channels[ channel ]->header->chan != ( channel + 1 ) ) { journal->channels[ channel ]->header->chan = ( channel + 1 ); journal->header->totchan +=1; } journal->header->seq = seq; // Need to update NOTE OFF bits if the command is NOTE OFF if( midi_note->command == MIDI_COMMAND_NOTE_OFF ) { uint8_t offset, shift; // Which element offset = (midi_note->note) / 8; shift = ( (midi_note->note) - ( offset * 8 )) - 1; // Set low and high values; journal->channels[ channel ]->chapter_n->header->high = MAX( offset , journal->channels[ channel ]->chapter_n->header->high ); journal->channels[ channel ]->chapter_n->header->low = MIN( offset , journal->channels[ channel ]->chapter_n->header->low ); journal->channels[ channel ]->chapter_n->offbits[offset] |= ( 1 << shift ); return; } if( journal->channels[ channel ]->chapter_n->num_notes == MAX_CHAPTER_N_NOTES ) return; new_note = chapter_n_note_create(); if(! new_note ) return; new_note->num = midi_note->note; new_note->velocity = midi_note->velocity; note_slot = journal->channels[ channel ]->chapter_n->num_notes; journal->channels[ channel ]->chapter_n->notes[note_slot] = new_note; journal->channels[ channel ]->chapter_n->num_notes++; }