tinypy: particularly debugged network support.
git-svn-id: svn://kolibrios.org@2110 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
7c0f7e203a
commit
fa03894a53
14
programs/develop/tinypy/examples/net/echosrv.py
Normal file
14
programs/develop/tinypy/examples/net/echosrv.py
Normal file
@ -0,0 +1,14 @@
|
||||
import socket
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.bind(('192.168.1.101', 5000))
|
||||
s.listen(1)
|
||||
conn, addr = s.accept()
|
||||
print("Connected by ", addr)
|
||||
while True:
|
||||
data = conn.recv(1024)
|
||||
if not data:
|
||||
break
|
||||
print(data)
|
||||
conn.send(data)
|
||||
conn.close()
|
17
programs/develop/tinypy/examples/net/nettest.py
Normal file
17
programs/develop/tinypy/examples/net/nettest.py
Normal file
@ -0,0 +1,17 @@
|
||||
import kolibri
|
||||
|
||||
socket = kolibri.socket # Alias for socket module
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.bind('10.0.2.15', 6000)
|
||||
if s.connect('192.168.1.101', 5000):
|
||||
print("cp2, socket is", s.socket)
|
||||
if not s.send("<html></html>"):
|
||||
print("Sending failed")
|
||||
else:
|
||||
data = s.recv(1024)
|
||||
print(data)
|
||||
s.close()
|
||||
else:
|
||||
print("Connection failed, socket is", s.socket)
|
||||
print("OK")
|
@ -6,11 +6,17 @@
|
||||
extern tp_obj tp_dict(TP);
|
||||
extern tp_obj tp_method(TP,tp_obj self,tp_obj v(TP));
|
||||
extern tp_obj tp_fnc(TP,tp_obj v(TP));
|
||||
extern tp_obj tp_get(TP,tp_obj self, tp_obj k);
|
||||
tp_obj tp_has(TP,tp_obj self, tp_obj k);
|
||||
#define _cdecl __attribute__((cdecl))
|
||||
extern int (* _cdecl con_printf)(const char* format,...);
|
||||
|
||||
#define PRECISION 0.000001
|
||||
|
||||
#define GET_SOCKET_DESCRIPTOR(_obj, _sock) do{ \
|
||||
if (!tp_has(tp, _obj, tp_string("socket"))) \
|
||||
if (fabs(tp_has(tp, _obj, tp_string("socket")).number.val) < PRECISION)\
|
||||
tp_raise(tp_None, "Socket not open", tp_None); \
|
||||
_sock = (__u32)tp_get(tp, _obj, tp_string("socket")).number.val;\
|
||||
_sock = (__u32)(tp_get(tp, _obj, tp_string("socket")).number.val + PRECISION);\
|
||||
} while(0)
|
||||
|
||||
/* Socket close method.
|
||||
@ -20,7 +26,7 @@ extern tp_obj tp_fnc(TP,tp_obj v(TP));
|
||||
*
|
||||
* Raises exception if socket was not opened. Otherwise returns True.
|
||||
*/
|
||||
tp_obj kolibri_close_socket(TP)
|
||||
static tp_obj kolibri_close_socket(TP)
|
||||
{
|
||||
tp_obj self = TP_TYPE(TP_DICT);
|
||||
__u32 socktype;
|
||||
@ -28,26 +34,12 @@ tp_obj kolibri_close_socket(TP)
|
||||
|
||||
GET_SOCKET_DESCRIPTOR(self, s);
|
||||
|
||||
con_printf("cp5\n");
|
||||
socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
||||
con_printf("cp6\n");
|
||||
if (!tp_has(tp, self, tp_string("socket")))
|
||||
{
|
||||
con_printf("cp7\n");
|
||||
tp_raise(tp_None, "Socket not open", tp_None);
|
||||
}
|
||||
s = (__u32)tp_get(tp, self, tp_string("socket")).number.val;
|
||||
con_printf("cp0, s=%d\n", s);
|
||||
GET_SOCKET_DESCRIPTOR(self, s);
|
||||
if (socktype == SOCK_STREAM)
|
||||
{
|
||||
con_printf("cp1\n");
|
||||
__menuet__close_TCP_socket(s);
|
||||
con_printf("cp2\n");
|
||||
}
|
||||
else if (socktype == SOCK_DGRAM)
|
||||
{
|
||||
__menuet__close_UDP_socket(s);
|
||||
}
|
||||
return tp_True;
|
||||
}
|
||||
|
||||
@ -59,23 +51,24 @@ tp_obj kolibri_close_socket(TP)
|
||||
* or:
|
||||
* s.send(data, 20) # Send just 20 bytes
|
||||
*/
|
||||
tp_obj kolibri_send(TP)
|
||||
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)
|
||||
__menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val);
|
||||
result = __menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val);
|
||||
else if (socktype == SOCK_DGRAM)
|
||||
__menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val);
|
||||
return tp_None;
|
||||
result = __menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val);
|
||||
return tp_number(!(result != 0));
|
||||
}
|
||||
|
||||
/* Socket recv method.
|
||||
@ -85,10 +78,9 @@ tp_obj kolibri_send(TP)
|
||||
* or:
|
||||
* s.recv(data, 20) # Send just 20 bytes
|
||||
*/
|
||||
tp_obj kolibri_recv(TP)
|
||||
static tp_obj kolibri_recv(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 s;
|
||||
__u8 c;
|
||||
@ -119,7 +111,7 @@ tp_obj kolibri_recv(TP)
|
||||
return tp_string_n(buf, bytes_read);
|
||||
}
|
||||
|
||||
void inet_pton(TP, const char *buf, int len, __u32 *addr)
|
||||
static void inet_pton(TP, const char *buf, int len, __u32 *addr)
|
||||
{
|
||||
char *p = (char *)buf;
|
||||
int i = 0;
|
||||
@ -127,35 +119,38 @@ void inet_pton(TP, const char *buf, int len, __u32 *addr)
|
||||
*addr = 0;
|
||||
while (*p && p < buf + len && i < 4)
|
||||
{
|
||||
con_printf("char %d", *p);
|
||||
if (*p == '.')
|
||||
if (*p == '.' || !*p)
|
||||
{
|
||||
if (val > 255)
|
||||
tp_raise(, "ValueError: number > 255 in IP address", tp_None);
|
||||
tp_raise(tp_None, "ValueError: number > 255 in IP address", tp_None);
|
||||
*addr += (val << ((i++) << 3));
|
||||
con_printf("num %d\n", val);
|
||||
val = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (*p < '0' || *p > '9')
|
||||
tp_raise(, "ValueError: bad char in IP address, digit expected", tp_None);
|
||||
tp_raise(tp_None, "ValueError: bad char in IP address, digit expected", tp_None);
|
||||
val = val * 10 + *p - '0';
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (!*p)
|
||||
{
|
||||
if (i == 3)
|
||||
*addr += (val << ((i++) << 3));
|
||||
else
|
||||
tp_raise(tp_None, "ValueError: bad IP address", tp_None);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Converter from string presentation to binary address. */
|
||||
tp_obj kolibri_inet_pton(TP)
|
||||
static tp_obj kolibri_inet_pton(TP)
|
||||
{
|
||||
tp_obj obj;
|
||||
__u32 addr;
|
||||
con_printf("A1\n");
|
||||
obj = TP_TYPE(TP_STRING);
|
||||
con_printf("A2\n");
|
||||
inet_pton(tp, (char *)obj.string.val, (int)obj.string.len, &addr);
|
||||
con_printf("A3\n");
|
||||
return tp_number(addr);
|
||||
}
|
||||
|
||||
@ -264,9 +259,9 @@ tp_obj kolibri_socket(TP)
|
||||
tp_obj sockfamily = TP_TYPE(TP_NUMBER);
|
||||
tp_obj socktype = TP_TYPE(TP_NUMBER);
|
||||
|
||||
if (abs(sockfamily.number.val - AF_INET) > 0.000001 ||
|
||||
(abs(socktype.number.val - SOCK_STREAM) > 0.000001 &&
|
||||
abs(socktype.number.val - SOCK_DGRAM) > 0.000001))
|
||||
if (fabs(sockfamily.number.val - AF_INET) > PRECISION ||
|
||||
(fabs(socktype.number.val - SOCK_STREAM) > PRECISION &&
|
||||
fabs(socktype.number.val - SOCK_DGRAM) > PRECISION))
|
||||
return tp_None;
|
||||
s = tp_dict(tp);
|
||||
tp_set(tp, s, tp_string("family"), sockfamily);
|
||||
@ -276,7 +271,7 @@ tp_obj kolibri_socket(TP)
|
||||
tp_set(tp, s, tp_string("send"), tp_method(tp, s, kolibri_send));
|
||||
tp_set(tp, s, tp_string("recv"), tp_method(tp, s, kolibri_recv));
|
||||
tp_set(tp, s, tp_string("close"), tp_method(tp, s, kolibri_close_socket));
|
||||
if (abs(socktype.number.val - SOCK_STREAM) < 0.000001)
|
||||
if (fabs(socktype.number.val - SOCK_STREAM) < PRECISION)
|
||||
tp_set(tp, s, tp_string("listen"), tp_method(tp, s, kolibri_listen));
|
||||
return s;
|
||||
}
|
||||
@ -288,7 +283,7 @@ tp_obj kolibri_socket_module(TP)
|
||||
tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET));
|
||||
tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(SOCK_STREAM));
|
||||
tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(SOCK_DGRAM));
|
||||
tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
|
||||
tp_set(tp, socket_mod, tp_string("inet_pton"), tp_fnc(tp, kolibri_inet_pton));
|
||||
tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
|
||||
return socket_mod;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user