mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
add socket.setblocking method
This commit is contained in:
parent
24dd3c8e5c
commit
8a21ba98d1
@ -1,5 +1,5 @@
|
|||||||
#include "_socket_socket.h"
|
|
||||||
#include "PikaPlatform_socket.h"
|
#include "PikaPlatform_socket.h"
|
||||||
|
#include "_socket_socket.h"
|
||||||
|
|
||||||
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 0)
|
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 0)
|
||||||
#error "This library requires PikaScript version 1.12.0 or higher"
|
#error "This library requires PikaScript version 1.12.0 or higher"
|
||||||
@ -17,6 +17,7 @@ void _socket_socket__init(PikaObj* self) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
obj_setInt(self, "sockfd", sockfd);
|
obj_setInt(self, "sockfd", sockfd);
|
||||||
|
obj_setInt(self, "blocking", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _socket_socket__close(PikaObj* self) {
|
void _socket_socket__close(PikaObj* self) {
|
||||||
@ -71,9 +72,11 @@ Arg* _socket_socket__recv(PikaObj* self, int num) {
|
|||||||
data_recv = arg_getBytes(res);
|
data_recv = arg_getBytes(res);
|
||||||
ret = __platform_recv(sockfd, data_recv, num, 0);
|
ret = __platform_recv(sockfd, data_recv, num, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
if (obj_getInt(self, "blocking")) {
|
||||||
__platform_printf("recv error\n");
|
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||||
return NULL;
|
__platform_printf("recv error\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -91,6 +94,14 @@ void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
|||||||
server_addr.sin_addr.s_addr = inet_addr(host);
|
server_addr.sin_addr.s_addr = inet_addr(host);
|
||||||
__platform_connect(sockfd, (struct sockaddr*)&server_addr,
|
__platform_connect(sockfd, (struct sockaddr*)&server_addr,
|
||||||
sizeof(server_addr));
|
sizeof(server_addr));
|
||||||
|
if (obj_getInt(self, "blocking") == 0) {
|
||||||
|
int flags = fcntl(sockfd, F_GETFL);
|
||||||
|
if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||||
|
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||||
|
__platform_printf("Unable to set socket non blocking\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||||
@ -114,3 +125,7 @@ char* _socket__gethostname(PikaObj* self) {
|
|||||||
__platform_gethostname(hostname_buff, 128);
|
__platform_gethostname(hostname_buff, 128);
|
||||||
return obj_cacheStr(self, hostname);
|
return obj_cacheStr(self, hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _socket_socket__setblocking(PikaObj* self, int sta) {
|
||||||
|
obj_setInt(self, "blocking", sta);
|
||||||
|
}
|
@ -10,6 +10,7 @@ class socket:
|
|||||||
def _connect(host: str, port: int): ...
|
def _connect(host: str, port: int): ...
|
||||||
def _recv(num: int) -> bytes: ...
|
def _recv(num: int) -> bytes: ...
|
||||||
def _init(): ...
|
def _init(): ...
|
||||||
|
def _setblocking(sta: int): ...
|
||||||
|
|
||||||
|
|
||||||
def _gethostname() -> str: ...
|
def _gethostname() -> str: ...
|
||||||
|
@ -50,5 +50,8 @@ class socket(_socket.socket):
|
|||||||
def recv(self, num):
|
def recv(self, num):
|
||||||
return self._recv(num)
|
return self._recv(num)
|
||||||
|
|
||||||
|
def setblocking(self, sta):
|
||||||
|
return self._setblocking(sta)
|
||||||
|
|
||||||
def gethostname():
|
def gethostname():
|
||||||
return _socket._gethostname()
|
return _socket._gethostname()
|
||||||
|
@ -10,6 +10,7 @@ class socket:
|
|||||||
def _connect(host: str, port: int): ...
|
def _connect(host: str, port: int): ...
|
||||||
def _recv(num: int) -> bytes: ...
|
def _recv(num: int) -> bytes: ...
|
||||||
def _init(): ...
|
def _init(): ...
|
||||||
|
def _setblocking(sta: int): ...
|
||||||
|
|
||||||
|
|
||||||
def _gethostname() -> str: ...
|
def _gethostname() -> str: ...
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include "_socket_socket.h"
|
|
||||||
#include "PikaPlatform_socket.h"
|
#include "PikaPlatform_socket.h"
|
||||||
|
#include "_socket_socket.h"
|
||||||
|
|
||||||
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 0)
|
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 0)
|
||||||
#error "This library requires PikaScript version 1.12.0 or higher"
|
#error "This library requires PikaScript version 1.12.0 or higher"
|
||||||
@ -17,6 +17,7 @@ void _socket_socket__init(PikaObj* self) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
obj_setInt(self, "sockfd", sockfd);
|
obj_setInt(self, "sockfd", sockfd);
|
||||||
|
obj_setInt(self, "blocking", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _socket_socket__close(PikaObj* self) {
|
void _socket_socket__close(PikaObj* self) {
|
||||||
@ -71,9 +72,11 @@ Arg* _socket_socket__recv(PikaObj* self, int num) {
|
|||||||
data_recv = arg_getBytes(res);
|
data_recv = arg_getBytes(res);
|
||||||
ret = __platform_recv(sockfd, data_recv, num, 0);
|
ret = __platform_recv(sockfd, data_recv, num, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
if (obj_getInt(self, "blocking")) {
|
||||||
__platform_printf("recv error\n");
|
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||||
return NULL;
|
__platform_printf("recv error\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -91,6 +94,14 @@ void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
|||||||
server_addr.sin_addr.s_addr = inet_addr(host);
|
server_addr.sin_addr.s_addr = inet_addr(host);
|
||||||
__platform_connect(sockfd, (struct sockaddr*)&server_addr,
|
__platform_connect(sockfd, (struct sockaddr*)&server_addr,
|
||||||
sizeof(server_addr));
|
sizeof(server_addr));
|
||||||
|
if (obj_getInt(self, "blocking") == 0) {
|
||||||
|
int flags = fcntl(sockfd, F_GETFL);
|
||||||
|
if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||||
|
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||||
|
__platform_printf("Unable to set socket non blocking\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||||
@ -114,3 +125,7 @@ char* _socket__gethostname(PikaObj* self) {
|
|||||||
__platform_gethostname(hostname_buff, 128);
|
__platform_gethostname(hostname_buff, 128);
|
||||||
return obj_cacheStr(self, hostname);
|
return obj_cacheStr(self, hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _socket_socket__setblocking(PikaObj* self, int sta) {
|
||||||
|
obj_setInt(self, "blocking", sta);
|
||||||
|
}
|
@ -50,5 +50,8 @@ class socket(_socket.socket):
|
|||||||
def recv(self, num):
|
def recv(self, num):
|
||||||
return self._recv(num)
|
return self._recv(num)
|
||||||
|
|
||||||
|
def setblocking(self, sta):
|
||||||
|
return self._setblocking(sta)
|
||||||
|
|
||||||
def gethostname():
|
def gethostname():
|
||||||
return _socket._gethostname()
|
return _socket._gethostname()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user