/* Socket recv method. * * data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>" * s.recv(data) * or: * s.recv(data, 20) # Send just 20 bytes */ static tp_obj kolibri_recv(TP) { tp_obj self = TP_TYPE(TP_DICT); __u32 datalen = TP_DEFAULT(tp_False).number.val; __u32 s; __u8 c; __u8 *buf, *p; __u32 buf_size; __u32 bytes_read = 0; int i; GET_SOCKET_DESCRIPTOR(self, s); if (datalen) buf_size = datalen; else buf_size = 2048; if (!(buf = malloc(datalen))) tp_raise(tp_None, "Cannot allocate buffer for received data", tp_None); p = buf; while (__menuet__read_socket(s, &c) && bytes_read < buf_size) { *p++ = c; bytes_read++; if (bytes_read >= buf_size && !datalen) { buf_size += 1024; buf = realloc(buf, buf_size); } } return tp_string_n(buf, bytes_read); }
tp_obj tp_range(TP) { int a = TP_NUM(); int b = TP_NUM(); int c = TP_DEFAULT(tp_number(1)).number.val; tp_obj r = tp_list(tp); int i; for (i=a; i!=b; i+=c) { _tp_list_append(tp,r.list.val,tp_number(i)); } return r; }
tp_obj tp_float(TP) { tp_obj v = TP_OBJ(); int ord = TP_DEFAULT(tp_number(0)).number.val; int type = v.type; if (type == TP_NUMBER) { return v; } if (type == TP_STRING) { if (strchr(STR(v),'.')) { return tp_number(atof(STR(v))); } return(tp_number(strtol(STR(v),0,ord))); } tp_raise(None,"tp_float(%s)",STR(v)); }
tp_obj tp_float(TP) { tp_obj v = TP_OBJ(); int ord = TP_DEFAULT(tp_number(0)).number.val; int type = v.type; if (type == TP_NUMBER) { return v; } if (type == TP_STRING && v.string.len < 32) { char s[32]; memset(s,0,v.string.len+1); memcpy(s,v.string.val,v.string.len); if (strchr(s,'.')) { return tp_number(atof(s)); } return(tp_number(strtol(s,0,ord))); } tp_raise(tp_None,tp_string("(tp_float) TypeError: ?")); }
tp_obj tp_range(TP) { int a,b,c,i; tp_obj r = tp_list(tp); switch (tp->params.list.val->len) { case 1: a = 0; b = TP_NUM(); c = 1; break; case 2: case 3: a = TP_NUM(); b = TP_NUM(); c = TP_DEFAULT(tp_number(1)).number.val; break; default: return r; } if (c != 0) { for (i=a; (c>0) ? i<b : i>b; i+=c) { _tp_list_append(tp,r.list.val,tp_number(i)); } } return r; }
/* Socket send method. * * Example: * data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>" * s.send(data) * or: * s.send(data, 20) # Send just 20 bytes */ static tp_obj kolibri_send(TP) { tp_obj self = TP_TYPE(TP_DICT); tp_obj data_obj = TP_TYPE(TP_STRING); __u32 datalen = TP_DEFAULT(tp_False).number.val; __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val; __u32 s; int result; GET_SOCKET_DESCRIPTOR(self, s); if (datalen < 0 || datalen > data_obj.string.len) datalen = data_obj.string.len; if (socktype == SOCK_STREAM) result = __menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val); else if (socktype == SOCK_DGRAM) result = __menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val); return tp_number(!(result != 0)); }