static void tcpclient_receive_callback(void*y, t_iemnet_chunk*c) { t_tcpclient *x=(t_tcpclient*)y; if(c) { iemnet__addrout(x->x_statusout, x->x_addrout, x->x_addr, x->x_port); x->x_floatlist=iemnet__chunk2list(c, x->x_floatlist); // get's destroyed in the dtor iemnet__streamout(x->x_msgout, x->x_floatlist->argc, x->x_floatlist->argv, x->x_serialize); } else { // disconnected tcpclient_disconnect(x); } }
static void tcpclient_connect(t_tcpclient *x, t_symbol *hostname, t_floatarg fportno) { if(x->x_fd>=0)tcpclient_disconnect(x); /* we get hostname and port and pass them on to the child thread that establishes the connection */ x->x_hostname = hostname->s_name; x->x_port = fportno; x->x_connectstate = 0; /* start child thread */ if(pthread_create(&x->x_threadid, &x->x_threadattr, tcpclient_child_connect, x) < 0) error("%s: could not create new thread", objName); }
static void tcpclient_send(t_tcpclient *x, t_symbol *s, int argc, t_atom *argv) { int size=0; t_atom output_atom; t_iemnet_sender*sender=x->x_sender; t_iemnet_chunk*chunk=iemnet__chunk_create_list(argc, argv); if(sender && chunk) { size=iemnet__sender_send(sender, chunk); } iemnet__chunk_destroy(chunk); SETFLOAT(&output_atom, size); outlet_anything( x->x_statusout, gensym("sent"), 1, &output_atom); if(size<0) { tcpclient_disconnect(x); } }
static void tcpclient_send(t_tcpclient *x, t_symbol *s, int argc, t_atom *argv) { #define BYTE_BUF_LEN 65536 // arbitrary maximum similar to max IP packet size static char byte_buf[BYTE_BUF_LEN]; int i, j, d; unsigned char c; float f, e; char *bp; int length, sent; int result; static double lastwarntime; static double pleasewarn; double timebefore; double timeafter; int late; char fpath[FILENAME_MAX]; FILE *fptr; #ifdef DEBUG post("s: %s", s->s_name); post("argc: %d", argc); #endif for (i = j = 0; i < argc; ++i) { if (argv[i].a_type == A_FLOAT) { f = argv[i].a_w.w_float; d = (int)f; e = f - d; #ifdef DEBUG post("%s: argv[%d]: float:%f int:%d delta:%f", objName, i, f, d, e); #endif if (e != 0) { error("%s_send: item %d (%f) is not an integer", objName, i, f); return; } if ((d < 0) || (d > 255)) { error("%s: item %d (%f) is not between 0 and 255", objName, i, f); return; } c = (unsigned char)d; #ifdef DEBUG post("%s_send: argv[%d]: %d", objName, i, c); #endif byte_buf[j++] = c; } else if (argv[i].a_type == A_SYMBOL) { atom_string(&argv[i], fpath, FILENAME_MAX); #ifdef DEBUG post ("%s_send fname: %s", objName, fpath); #endif fptr = fopen(fpath, "rb"); if (fptr == NULL) { post("%s_send: unable to open \"%s\"", objName, fpath); return; } rewind(fptr); #ifdef DEBUG post("%s_send: d is %d", objName, d); #endif while ((d = fgetc(fptr)) != EOF) { byte_buf[j++] = (char)(d & 0x0FF); #ifdef DEBUG post("%s_send: byte_buf[%d] = %d", objName, j-1, byte_buf[j-1]); #endif if (j >= BYTE_BUF_LEN) { post ("%s_send: file too long, truncating at %lu", objName, BYTE_BUF_LEN); break; } } fclose(fptr); fptr = NULL; post("%s_send: read \"%s\" length %d byte%s", objName, fpath, j, ((d==1)?"":"s")); } else { error("%s_send: item %d is not a float or a file name", objName, i); return; } } length = j; if ((x->x_fd >= 0) && (length > 0)) { for (bp = byte_buf, sent = 0; sent < length;) { timebefore = sys_getrealtime(); result = send(x->x_fd, byte_buf, length-sent, 0); timeafter = sys_getrealtime(); late = (timeafter - timebefore > 0.005); if (late || pleasewarn) { if (timeafter > lastwarntime + 2) { post("%s_send blocked %d msec", objName, (int)(1000 * ((timeafter - timebefore) + pleasewarn))); pleasewarn = 0; lastwarntime = timeafter; } else if (late) pleasewarn += timeafter - timebefore; } if (result <= 0) { sys_sockerror("tcpclient_send"); tcpclient_disconnect(x); break; } else { sent += result; bp += result; } } } else error("%s: not connected", objName); }
static void tcpclient_free(t_tcpclient *x) { tcpclient_disconnect(x); if(x->x_clock)clock_free(x->x_clock);x->x_clock=NULL; if(x->x_floatlist)iemnet__floatlist_destroy(x->x_floatlist);x->x_floatlist=NULL; }