int main () { int delay = 0; int times_5 = 5; int times_7 = 7; pthread_t t_s; /* successor */ pthread_t t_t5; /* times 7 */ pthread_t t_t7; /* times 5 */ pthread_t t_m; /* merge */ pthread_t t_c; /* consumer */ stream_t s_suc; stream_t s_times5; stream_t s_times7; stream_t s_merge; stream_t s_cons; init_stream(&s_suc , (void*)&delay); init_stream(&s_times5 , (void*)×_5); init_stream(&s_times7 , (void*)×_7); init_stream(&s_merge , NULL); init_stream(&s_cons , (void*)&delay); stream_connect(&s_cons , &s_merge); stream_connect(&s_merge , &s_times5); stream_connect(&s_merge , &s_times7); stream_connect(&s_times5 , &s_suc); stream_connect(&s_times7 , &s_suc); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_create(&t_s , &attr , successor , (void*)&s_suc); pthread_create(&t_t5 , &attr , times , (void*)&s_times5); pthread_create(&t_t7 , &attr , times , (void*)&s_times7); pthread_create(&t_m , &attr , merge , (void*)&s_merge); pthread_create(&t_c , &attr , consumer , (void*)&s_cons); pthread_join(t_c, NULL); pthread_cancel(t_m); pthread_cancel(t_t5); pthread_cancel(t_t7); pthread_cancel(t_s); return 0; }
int main(int argc, char **argv) { char *transport; char *endpoint; char *path; int server_sock; int client_fd; if (argc < 3 || (endpoint = split_at(transport = argv[1], ':')) == 0 || *endpoint == 0 || *transport == 0) msg_fatal("usage: %s transport:endpoint file...", argv[0]); if (strcmp(transport, "stream") == 0) { server_sock = stream_connect(endpoint, BLOCKING, 0); } else { msg_fatal("invalid transport name: %s", transport); } if (server_sock < 0) msg_fatal("connect %s:%s: %m", transport, endpoint); argv += 2; while ((path = *argv++) != 0) { if ((client_fd = open(path, O_RDONLY, 0)) < 0) msg_fatal("open %s: %m", path); msg_info("path=%s client_fd=%d", path, client_fd); if (stream_send_fd(server_sock, client_fd) < 0) msg_fatal("send file descriptor: %m"); if (close(client_fd) != 0) msg_fatal("close(%d): %m", client_fd); } exit(0); }
int jabber_connect(){ stream=stream_connect(server,port,1,jabber_event_cb); g_assert(stream!=NULL); jabber_source=g_source_new(&jabber_source_funcs,sizeof(GSource)); g_source_attach(jabber_source,g_main_loop_get_context(main_loop)); return 0; }
/* * If passed one argument, this is identical to Kgio::Socket.connect. * If passed two or three arguments, it uses its superclass method: * * Socket.new(domain, socktype [, protocol ]) */ static VALUE kgio_new(int argc, VALUE *argv, VALUE klass) { if (argc == 1) /* backwards compat, the only way for kgio <= 2.7.4 */ return stream_connect(klass, argv[0], 1); return rb_call_super(argc, argv); }
static void ssl_wait(struct stream *stream, enum stream_wait_type wait) { struct ssl_stream *sslv = ssl_stream_cast(stream); switch (wait) { case STREAM_CONNECT: if (stream_connect(stream) != EAGAIN) { poll_immediate_wake(); } else { switch (sslv->state) { case STATE_TCP_CONNECTING: poll_fd_wait(sslv->fd, POLLOUT); break; case STATE_SSL_CONNECTING: /* ssl_connect() called SSL_accept() or SSL_connect(), which * set up the status that we test here. */ poll_fd_wait(sslv->fd, want_to_poll_events(SSL_want(sslv->ssl))); break; default: OVS_NOT_REACHED(); } } break; case STREAM_RECV: if (sslv->rx_want != SSL_NOTHING) { poll_fd_wait(sslv->fd, want_to_poll_events(sslv->rx_want)); } else { poll_immediate_wake(); } break; case STREAM_SEND: if (!sslv->txbuf) { /* We have room in our tx queue. */ poll_immediate_wake(); } else { /* stream_run_wait() will do the right thing; don't bother with * redundancy. */ } break; default: OVS_NOT_REACHED(); } }
/* Model output function */ void client_output(int_T tid) { /* local block i/o variables */ t_stream_ptr rtb_StreamFlush_o1; /* S-Function (stream_connect_block): '<Root>/Stream Connect' */ /* S-Function Block: client/Stream Connect (stream_connect_block) */ { t_error result; result = stream_connect("tcpip://localhost:18000", true, client_P.StreamConnect_SendBufferSize, client_P.StreamConnect_ReceiveBufferSize, &client_DWork.StreamConnect_Stream); if (result == 0) { static const t_short endian_test = 0x0201; result = stream_set_swap_bytes(client_DWork.StreamConnect_Stream, *(t_byte *)&endian_test != client_P.StreamConnect_Endian); } } /* Sin: '<Root>/Sine Wave' */ client_B.SineWave = sin(client_P.SineWave_Freq * client_M->Timing.t[0] + client_P.SineWave_Phase) * client_P.SineWave_Amp + client_P.SineWave_Bias; /* S-Function (stream_send_block): '<Root>/Stream Send' incorporates: * S-Function (stream_connect_block): '<Root>/Stream Connect' */ { stream_send_unit_array(*((t_stream_ptr)&client_DWork.StreamConnect_Stream), &client_B.SineWave, sizeof(real_T), 1); } /* S-Function (stream_flush_block): '<Root>/Stream Flush' incorporates: * S-Function (stream_send_block): '<Root>/Stream Send' * S-Function (stream_connect_block): '<Root>/Stream Connect' */ /* S-Function Block: client/Stream Flush (stream_flush_block) */ { t_error result; result = stream_flush(*((t_stream_ptr)((t_stream_ptr)& client_DWork.StreamConnect_Stream))); rtb_StreamFlush_o1 = ((t_stream_ptr)((t_stream_ptr)& client_DWork.StreamConnect_Stream)); client_B.StreamFlush_o2 = (int32_T) result; } UNUSED_PARAMETER(tid); }
int main() { struct stream* p_stream; const char* stream_name = "tcp:127.0.0.1:1234"; char pnum[4]; uint8_t dscp = 1; int actualsend = 0; int index = 0; while(1) { if(stream_open(stream_name,&p_stream,dscp)) { printf("stream open failure!\n"); } else { printf("stream open sucessed!\n"); if(stream_connect(p_stream)) { printf("stream connect failure!\n"); } else { printf("stream connect successed!\n"); sprintf(pnum,"%d",index); actualsend = stream_send(p_stream,pnum,sizeof(pnum)); index++; if(actualsend < 0) printf("stream send failure!\n"); else if(actualsend == 0) printf("stream send 0 bytes!\n"); else printf("stream actual send %d bytes,Data:%s\n",actualsend,pnum); } } stream_close(p_stream); sleep(1); } return 0; }
static int play_flac(lua_State *lstate) { FLAC_FEED *feed; char buf[BLOCKSIZE]; int port, sock; char *mark; const char *host, *mount; lua_pushstring(lstate, "host"); lua_gettable(lstate, -2); lua_pushstring(lstate, "port"); lua_gettable(lstate, -3); lua_pushstring(lstate, "mount"); lua_gettable(lstate, -4); mount = lua_tostring(lstate, -1); port = lua_tointeger(lstate, -2); host = lua_tostring(lstate, -3); sock = stream_connect(host, port, mount, buf, &mark); lua_pop(lstate, 3); if (sock == 0) { lua_pop(lstate, 1); return 0; } lua_pushstring(lstate, "intern"); lua_gettable(lstate, -2); feed = (FLAC_FEED *)lua_touserdata(lstate, -1); lua_pop(lstate, 1); feed->base.sock = sock; pthread_mutex_init(&(feed->base.thread_lock), NULL); pthread_cond_init(&(feed->base.data_ready), NULL); feed->decoder = FLAC__stream_decoder_new(); FLAC__stream_decoder_set_md5_checking(feed->decoder, false); FLAC__stream_decoder_init_ogg_stream(feed->decoder, flac_read, NULL, //seek_callback, NULL, // tell_callback, NULL, //length_callback, NULL, // eof_callback, flac_write, flac_metadata, flac_error, (void *)feed); lua_pop(lstate, 1); pthread_create(&(feed->base.thread_id), NULL, flac_thread, feed); return 0; }
void jsonrpc_session_run(struct jsonrpc_session *s) { if (s->pstream) { struct stream *stream; int error; error = pstream_accept(s->pstream, &stream); if (!error) { if (s->rpc || s->stream) { VLOG_INFO_RL(&rl, "%s: new connection replacing active connection", reconnect_get_name(s->reconnect)); jsonrpc_session_disconnect(s); } reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(stream); } else if (error != EAGAIN) { reconnect_listen_error(s->reconnect, time_msec(), error); pstream_close(s->pstream); s->pstream = NULL; } } if (s->rpc) { size_t backlog; int error; backlog = jsonrpc_get_backlog(s->rpc); jsonrpc_run(s->rpc); if (jsonrpc_get_backlog(s->rpc) < backlog) { /* Data previously caught in a queue was successfully sent (or * there's an error, which we'll catch below.) * * We don't count data that is successfully sent immediately as * activity, because there's a lot of queuing downstream from us, * which means that we can push a lot of data into a connection * that has stalled and won't ever recover. */ reconnect_activity(s->reconnect, time_msec()); } error = jsonrpc_get_status(s->rpc); if (error) { reconnect_disconnected(s->reconnect, time_msec(), error); jsonrpc_session_disconnect(s); s->last_error = error; } } else if (s->stream) { int error; stream_run(s->stream); error = stream_connect(s->stream); if (!error) { reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(s->stream); s->stream = NULL; } else if (error != EAGAIN) { reconnect_connect_failed(s->reconnect, time_msec(), error); stream_close(s->stream); s->stream = NULL; s->last_error = error; } } switch (reconnect_run(s->reconnect, time_msec())) { case RECONNECT_CONNECT: jsonrpc_session_connect(s); break; case RECONNECT_DISCONNECT: reconnect_disconnected(s->reconnect, time_msec(), 0); jsonrpc_session_disconnect(s); break; case RECONNECT_PROBE: if (s->rpc) { struct json *params; struct jsonrpc_msg *request; params = json_array_create_empty(); request = jsonrpc_create_request("echo", params, NULL); json_destroy(request->id); request->id = json_string_create("echo"); jsonrpc_send(s->rpc, request); } break; } }
/* call-seq: * * addr = Socket.pack_sockaddr_in(80, 'example.com') * Kgio::Socket.start(addr) -> socket * * addr = Socket.pack_sockaddr_un("/path/to/unix/socket") * Kgio::Socket.start(addr) -> socket * * Creates a generic Kgio::Socket object and initiates a * non-blocking connection. The caller should select/poll * on the socket for writability before attempting to write * or optimistically attempt a write and handle :wait_writable * or Errno::EAGAIN. */ static VALUE kgio_start(VALUE klass, VALUE addr) { return stream_connect(klass, addr, 0); }
int main(void) { printf("01\t--------------------------------------------\n"); printf("02\t1 successor, 2 non threaded consumers\n"); printf("03\tConsumer 2 disconnects, consumer 1 continues\n"); printf("04\tgetting token, consumer 2 reconnects\n"); printf("03\t--------------------------------------------\n"); int con_delay = 0; int suc_delay = 0; pthread_t s1; stream_t suc1; stream_t cons1; stream_t cons2; init_stream(&suc1, &suc_delay); init_stream(&cons1, NULL); init_stream(&cons2, NULL); stream_connect(&cons1, &suc1); stream_connect(&cons2, &suc1); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_create(&s1, &attr, suc, (void*)&suc1); printf("Both consumers connected\n"); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); printf("Disconnecting consumer 2\n"); sleep(con_delay); stream_disconnect(&cons2, &suc1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf(" Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); printf("Reconnecting consumer 2\n"); sleep(con_delay); stream_connect(&cons2, &suc1); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); printf("Disconnecting consumer 1\n"); sleep(con_delay); stream_disconnect(&cons1, &suc1); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); sleep(con_delay); printf("Consumer 2 got: %d\n", *(int*)consume_single((void*)&cons2)); //print_buffers(&cons2); printf("Reconnecting consumer 1\n"); sleep(con_delay); stream_connect(&cons1, &suc1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("Consumer 1 got: %d\n", *(int*)consume_single((void*)&cons1)); //print_buffers(&cons1); sleep(con_delay); printf("done\n"); pthread_cancel(s1); kill_stream(&suc1); return 0; }
void jsonrpc_session_run(struct jsonrpc_session *s) { if (s->pstream) { struct stream *stream; int error; error = pstream_accept(s->pstream, &stream); if (!error) { if (s->rpc || s->stream) { VLOG_INFO_RL(&rl, "%s: new connection replacing active connection", reconnect_get_name(s->reconnect)); jsonrpc_session_disconnect(s); } reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(stream); } else if (error != EAGAIN) { reconnect_listen_error(s->reconnect, time_msec(), error); pstream_close(s->pstream); s->pstream = NULL; } } if (s->rpc) { int error; jsonrpc_run(s->rpc); error = jsonrpc_get_status(s->rpc); if (error) { reconnect_disconnected(s->reconnect, time_msec(), error); jsonrpc_session_disconnect(s); } } else if (s->stream) { int error; stream_run(s->stream); error = stream_connect(s->stream); if (!error) { reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(s->stream); s->stream = NULL; } else if (error != EAGAIN) { reconnect_connect_failed(s->reconnect, time_msec(), error); stream_close(s->stream); s->stream = NULL; } } switch (reconnect_run(s->reconnect, time_msec())) { case RECONNECT_CONNECT: jsonrpc_session_connect(s); break; case RECONNECT_DISCONNECT: reconnect_disconnected(s->reconnect, time_msec(), 0); jsonrpc_session_disconnect(s); break; case RECONNECT_PROBE: if (s->rpc) { struct json *params; struct jsonrpc_msg *request; params = json_array_create_empty(); request = jsonrpc_create_request("echo", params, NULL); json_destroy(request->id); request->id = json_string_create("echo"); jsonrpc_send(s->rpc, request); } break; } }
static int play_vorbis(lua_State *lstate) { char buf[BLOCKSIZE]; char *pt, *oggbuf, **comm, *mark; int n, sock, sr_err, port; const char *host, *mount; VORBIS_FEED *feed; lua_pushstring(lstate, "host"); lua_gettable(lstate, -2); lua_pushstring(lstate, "port"); lua_gettable(lstate, -3); lua_pushstring(lstate, "mount"); lua_gettable(lstate, -4); mount = lua_tostring(lstate, -1); port = lua_tointeger(lstate, -2); host = lua_tostring(lstate, -3); sock = stream_connect(host, port, mount, buf, &mark); lua_pop(lstate, 3); if (sock == 0) { lua_pop(lstate, 1); return 0; } lua_pushstring(lstate, "intern"); lua_gettable(lstate, -2); feed = (VORBIS_FEED *)lua_touserdata(lstate, -1); lua_pop(lstate, 1); feed->base.sock = sock; pthread_mutex_init(&(feed->base.thread_lock), NULL); pthread_cond_init(&(feed->base.data_ready), NULL); ogg_sync_init(&(feed->oy)); oggbuf = ogg_sync_buffer(&(feed->oy), BLOCKSIZE); n = BLOCKSIZE - (mark - buf); memcpy(oggbuf, mark, n); read_sock(feed->base.sock, oggbuf + n, BLOCKSIZE - n); ogg_sync_wrote(&(feed->oy), BLOCKSIZE); if ((n = ogg_sync_pageout(&(feed->oy), &(feed->og))) != 1) { logmsg("out of data: %d\n", n); free_vorbis((FEED *)feed); lua_pop(lstate, 1); return 0; } ogg_stream_init(&(feed->os), ogg_page_serialno(&(feed->og))); vorbis_info_init(&(feed->vi)); vorbis_comment_init(&(feed->vc)); if (ogg_stream_pagein(&(feed->os), &(feed->og)) < 1) { logmsg("error reading first ogg page\n"); //free_feed(feed); //return 0; } if (ogg_stream_packetout(&(feed->os), &(feed->op)) != 1) { logmsg("error reading first header packet\n"); free_vorbis((FEED *)feed); lua_pop(lstate, 1); return 0; } if (vorbis_synthesis_headerin(&(feed->vi), &(feed->vc), &(feed->op)) < 0) { logmsg("stream is not vorbis\n"); free_vorbis((FEED *)feed); lua_pop(lstate, 1); return 0; } vorbis_headers(feed); add_table(lstate, "info"); add_table(lstate, "comments"); comm = feed->vc.user_comments; while (*comm) { if ((pt = index(*comm, '=')) != NULL) { *pt++ = '\0'; set_string(lstate, *comm, pt); } ++comm; } lua_pop(lstate, 1); // comments feed->base.channels = feed->vi.channels; set_integer(lstate, "channels", feed->base.channels); set_integer(lstate, "srate", feed->vi.rate); lua_pop(lstate, 1); // info feed->base.cbuf = new_ringbuf(feed->vi.rate, feed->base.channels, BUFSECS, 0.333, 0.667); if (jack_sr != feed->vi.rate) { feed->base.converter = src_new(SRC_SINC_MEDIUM_QUALITY, feed->base.channels, &sr_err); feed->base.src_data_in = (float *)malloc(SRC_DATA_FRAMES * feed->base.channels * sizeof(float)); feed->base.src_data.data_in = feed->base.src_data_in; feed->base.src_data_remain = 0; feed->base.src_data.src_ratio = jack_sr / (double)feed->vi.rate; feed->base.src_data_out = (float *)malloc( (int)ceil(SRC_DATA_FRAMES * feed->base.channels * sizeof(float) * feed->base.src_data.src_ratio)); } feed->base.init = 1; lua_pop(lstate, 1); pthread_create(&(feed->base.thread_id), NULL, vorbis_thread, feed); return 0; }