mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
add __platform for socket, add override
This commit is contained in:
parent
a00ade6334
commit
ecaf7ed0e9
4
examples/socket/gethostname.py
Normal file
4
examples/socket/gethostname.py
Normal file
@ -0,0 +1,4 @@
|
||||
import socket
|
||||
|
||||
hostname = socket.gethostname()
|
||||
print(hostname)
|
@ -1,3 +1,4 @@
|
||||
#include "_socket.h"
|
||||
#include "_socket_socket.h"
|
||||
#ifdef __linux__
|
||||
#include <arpa/inet.h>
|
||||
@ -6,14 +7,94 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
PIKA_WEAK int __platform_socket(int __domain, int __type, int __protocol) {
|
||||
#ifdef __linux__
|
||||
return socket(__domain, __type, __protocol);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_bind(int __fd,
|
||||
const struct sockaddr* __addr,
|
||||
socklen_t __addr_len) {
|
||||
#ifdef __linux__
|
||||
return bind(__fd, __addr, __addr_len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_listen(int __fd, int __n) {
|
||||
#ifdef __linux__
|
||||
return listen(__fd, __n);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_accept(int __fd,
|
||||
struct sockaddr* __addr,
|
||||
socklen_t* __addr_len) {
|
||||
#ifdef __linux__
|
||||
return accept(__fd, __addr, __addr_len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_connect(int __fd,
|
||||
const struct sockaddr* __addr,
|
||||
socklen_t __addr_len) {
|
||||
#ifdef __linux__
|
||||
return connect(__fd, __addr, __addr_len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_send(int __fd,
|
||||
const void* __buf,
|
||||
size_t __n,
|
||||
int __flags) {
|
||||
#ifdef __linux__
|
||||
return send(__fd, __buf, __n, __flags);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_recv(int __fd, void* __buf, size_t __n, int __flags) {
|
||||
#ifdef __linux__
|
||||
return recv(__fd, __buf, __n, __flags);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_close(int __fd) {
|
||||
#ifdef __linux__
|
||||
return close(__fd);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
/* gethostname */
|
||||
PIKA_WEAK int __platform_gethostname(char* __name, size_t __len) {
|
||||
#ifdef __linux__
|
||||
return gethostname(__name, __len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
void _socket_socket__init(PikaObj* self) {
|
||||
int family = obj_getInt(self, "family");
|
||||
int type = obj_getInt(self, "type");
|
||||
int protocol = obj_getInt(self, "protocol");
|
||||
int sockfd = 0;
|
||||
#ifdef __linux__
|
||||
sockfd = socket(family, type, protocol);
|
||||
#endif
|
||||
sockfd = __platform_socket(family, type, protocol);
|
||||
if (sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("socket error\n");
|
||||
@ -24,9 +105,7 @@ void _socket_socket__init(PikaObj* self) {
|
||||
|
||||
void _socket_socket__close(PikaObj* self) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
close(sockfd);
|
||||
#endif
|
||||
__platform_close(sockfd);
|
||||
}
|
||||
|
||||
void _socket_socket__send(PikaObj* self, Arg* data) {
|
||||
@ -44,9 +123,7 @@ void _socket_socket__send(PikaObj* self, Arg* data) {
|
||||
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
int ret = 0;
|
||||
#ifdef __linux__
|
||||
ret = send(sockfd, data_send, len, 0);
|
||||
#endif
|
||||
ret = __platform_send(sockfd, data_send, len, 0);
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("send error\n");
|
||||
@ -59,10 +136,8 @@ void _socket_socket__accept(PikaObj* self) {
|
||||
int client_sockfd = 0;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addr_len = sizeof(client_addr);
|
||||
#ifdef __linux__
|
||||
client_sockfd =
|
||||
accept(sockfd, (struct sockaddr*)&client_addr, &client_addr_len);
|
||||
#endif
|
||||
client_sockfd = __platform_accept(sockfd, (struct sockaddr*)&client_addr,
|
||||
&client_addr_len);
|
||||
if (client_sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("accept error\n");
|
||||
@ -77,11 +152,9 @@ char* _socket_socket__recv(PikaObj* self, int num) {
|
||||
int ret = 0;
|
||||
char* data = NULL;
|
||||
uint8_t* data_recv = NULL;
|
||||
#ifdef __linux__
|
||||
obj_setBytes(self, "_recv_data", NULL, num);
|
||||
data_recv = obj_getBytes(self, "_recv_data");
|
||||
ret = recv(sockfd, data_recv, num, 0);
|
||||
#endif
|
||||
ret = __platform_recv(sockfd, data_recv, num, 0);
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("recv error\n");
|
||||
@ -93,9 +166,7 @@ char* _socket_socket__recv(PikaObj* self, int num) {
|
||||
|
||||
void _socket_socket__listen(PikaObj* self, int num) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
listen(sockfd, num);
|
||||
#endif
|
||||
__platform_listen(sockfd, num);
|
||||
}
|
||||
|
||||
void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
||||
@ -104,9 +175,8 @@ void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
server_addr.sin_addr.s_addr = inet_addr(host);
|
||||
#ifdef __linux__
|
||||
connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||||
#endif
|
||||
__platform_connect(sockfd, (struct sockaddr*)&server_addr,
|
||||
sizeof(server_addr));
|
||||
}
|
||||
|
||||
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||
@ -115,16 +185,13 @@ void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
server_addr.sin_addr.s_addr = inet_addr(host);
|
||||
#ifdef __linux__
|
||||
bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||||
#endif
|
||||
__platform_bind(sockfd, (struct sockaddr*)&server_addr,
|
||||
sizeof(server_addr));
|
||||
}
|
||||
|
||||
char* _socket_socket__gethostname(PikaObj* self) {
|
||||
char* _socket__gethostname(PikaObj* self) {
|
||||
char hostname_buff[128] = {0};
|
||||
char* hostname = (char*)hostname_buff;
|
||||
#ifdef __linux__
|
||||
gethostname(hostname_buff, 128);
|
||||
#endif
|
||||
return hostname;
|
||||
__platform_gethostname(hostname_buff, 128);
|
||||
return obj_cacheStr(self, hostname);
|
||||
}
|
@ -2,7 +2,6 @@ from PikaObj import *
|
||||
|
||||
|
||||
class socket:
|
||||
def _gethostname() -> str: ...
|
||||
def _bind(host: str, port: int): ...
|
||||
def _listen(num: int): ...
|
||||
def _accept(): ...
|
||||
@ -11,3 +10,6 @@ class socket:
|
||||
def _connect(host: str, port: int): ...
|
||||
def _recv(num: int) -> str: ...
|
||||
def _init(): ...
|
||||
|
||||
|
||||
def _gethostname() -> str: ...
|
||||
|
@ -4,6 +4,7 @@ AF_INET = 2
|
||||
SOCK_STREAM = 1
|
||||
|
||||
|
||||
|
||||
class socket(_socket.socket):
|
||||
family = AF_INET
|
||||
type = SOCK_STREAM
|
||||
@ -21,9 +22,6 @@ class socket(_socket.socket):
|
||||
self.proto = vars[2]
|
||||
self._init()
|
||||
|
||||
def gethostname(self):
|
||||
return self._gethostname()
|
||||
|
||||
def bind(self, host_port):
|
||||
host = host_port[0]
|
||||
port = host_port[1]
|
||||
@ -51,3 +49,6 @@ class socket(_socket.socket):
|
||||
|
||||
def recv(self, num):
|
||||
return self._recv(num)
|
||||
|
||||
def gethostname():
|
||||
return _socket._gethostname()
|
||||
|
4
port/linux/.vscode/settings.json
vendored
4
port/linux/.vscode/settings.json
vendored
@ -1,6 +1,7 @@
|
||||
{
|
||||
"C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, IndentWidth: 4}",
|
||||
"files.associations": {
|
||||
"*.rs": "rust",
|
||||
"pikastdlib_sysobj.h": "c",
|
||||
"pikastddata_list.h": "c",
|
||||
"dataargs.h": "c",
|
||||
@ -10,6 +11,7 @@
|
||||
"fstream": "c",
|
||||
"system_error": "c",
|
||||
"unistd.h": "c",
|
||||
"types.h": "c"
|
||||
"types.h": "c",
|
||||
"_socket.h": "c"
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ from PikaObj import *
|
||||
|
||||
|
||||
class socket:
|
||||
def _gethostname() -> str: ...
|
||||
def _bind(host: str, port: int): ...
|
||||
def _listen(num: int): ...
|
||||
def _accept(): ...
|
||||
@ -11,3 +10,6 @@ class socket:
|
||||
def _connect(host: str, port: int): ...
|
||||
def _recv(num: int) -> str: ...
|
||||
def _init(): ...
|
||||
|
||||
|
||||
def _gethostname() -> str: ...
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include "_socket.h"
|
||||
#include "_socket_socket.h"
|
||||
#ifdef __linux__
|
||||
#include <arpa/inet.h>
|
||||
@ -6,14 +7,94 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
PIKA_WEAK int __platform_socket(int __domain, int __type, int __protocol) {
|
||||
#ifdef __linux__
|
||||
return socket(__domain, __type, __protocol);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_bind(int __fd,
|
||||
const struct sockaddr* __addr,
|
||||
socklen_t __addr_len) {
|
||||
#ifdef __linux__
|
||||
return bind(__fd, __addr, __addr_len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_listen(int __fd, int __n) {
|
||||
#ifdef __linux__
|
||||
return listen(__fd, __n);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_accept(int __fd,
|
||||
struct sockaddr* __addr,
|
||||
socklen_t* __addr_len) {
|
||||
#ifdef __linux__
|
||||
return accept(__fd, __addr, __addr_len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_connect(int __fd,
|
||||
const struct sockaddr* __addr,
|
||||
socklen_t __addr_len) {
|
||||
#ifdef __linux__
|
||||
return connect(__fd, __addr, __addr_len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_send(int __fd,
|
||||
const void* __buf,
|
||||
size_t __n,
|
||||
int __flags) {
|
||||
#ifdef __linux__
|
||||
return send(__fd, __buf, __n, __flags);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_recv(int __fd, void* __buf, size_t __n, int __flags) {
|
||||
#ifdef __linux__
|
||||
return recv(__fd, __buf, __n, __flags);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_close(int __fd) {
|
||||
#ifdef __linux__
|
||||
return close(__fd);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
/* gethostname */
|
||||
PIKA_WEAK int __platform_gethostname(char* __name, size_t __len) {
|
||||
#ifdef __linux__
|
||||
return gethostname(__name, __len);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
#endif
|
||||
}
|
||||
|
||||
void _socket_socket__init(PikaObj* self) {
|
||||
int family = obj_getInt(self, "family");
|
||||
int type = obj_getInt(self, "type");
|
||||
int protocol = obj_getInt(self, "protocol");
|
||||
int sockfd = 0;
|
||||
#ifdef __linux__
|
||||
sockfd = socket(family, type, protocol);
|
||||
#endif
|
||||
sockfd = __platform_socket(family, type, protocol);
|
||||
if (sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("socket error\n");
|
||||
@ -24,9 +105,7 @@ void _socket_socket__init(PikaObj* self) {
|
||||
|
||||
void _socket_socket__close(PikaObj* self) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
close(sockfd);
|
||||
#endif
|
||||
__platform_close(sockfd);
|
||||
}
|
||||
|
||||
void _socket_socket__send(PikaObj* self, Arg* data) {
|
||||
@ -44,9 +123,7 @@ void _socket_socket__send(PikaObj* self, Arg* data) {
|
||||
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
int ret = 0;
|
||||
#ifdef __linux__
|
||||
ret = send(sockfd, data_send, len, 0);
|
||||
#endif
|
||||
ret = __platform_send(sockfd, data_send, len, 0);
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("send error\n");
|
||||
@ -59,10 +136,8 @@ void _socket_socket__accept(PikaObj* self) {
|
||||
int client_sockfd = 0;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addr_len = sizeof(client_addr);
|
||||
#ifdef __linux__
|
||||
client_sockfd =
|
||||
accept(sockfd, (struct sockaddr*)&client_addr, &client_addr_len);
|
||||
#endif
|
||||
client_sockfd = __platform_accept(sockfd, (struct sockaddr*)&client_addr,
|
||||
&client_addr_len);
|
||||
if (client_sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("accept error\n");
|
||||
@ -77,11 +152,9 @@ char* _socket_socket__recv(PikaObj* self, int num) {
|
||||
int ret = 0;
|
||||
char* data = NULL;
|
||||
uint8_t* data_recv = NULL;
|
||||
#ifdef __linux__
|
||||
obj_setBytes(self, "_recv_data", NULL, num);
|
||||
data_recv = obj_getBytes(self, "_recv_data");
|
||||
ret = recv(sockfd, data_recv, num, 0);
|
||||
#endif
|
||||
ret = __platform_recv(sockfd, data_recv, num, 0);
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("recv error\n");
|
||||
@ -93,9 +166,7 @@ char* _socket_socket__recv(PikaObj* self, int num) {
|
||||
|
||||
void _socket_socket__listen(PikaObj* self, int num) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
listen(sockfd, num);
|
||||
#endif
|
||||
__platform_listen(sockfd, num);
|
||||
}
|
||||
|
||||
void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
||||
@ -104,9 +175,8 @@ void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
server_addr.sin_addr.s_addr = inet_addr(host);
|
||||
#ifdef __linux__
|
||||
connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||||
#endif
|
||||
__platform_connect(sockfd, (struct sockaddr*)&server_addr,
|
||||
sizeof(server_addr));
|
||||
}
|
||||
|
||||
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||
@ -115,16 +185,13 @@ void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
server_addr.sin_addr.s_addr = inet_addr(host);
|
||||
#ifdef __linux__
|
||||
bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||||
#endif
|
||||
__platform_bind(sockfd, (struct sockaddr*)&server_addr,
|
||||
sizeof(server_addr));
|
||||
}
|
||||
|
||||
char* _socket_socket__gethostname(PikaObj* self) {
|
||||
char* _socket__gethostname(PikaObj* self) {
|
||||
char hostname_buff[128] = {0};
|
||||
char* hostname = (char*)hostname_buff;
|
||||
#ifdef __linux__
|
||||
gethostname(hostname_buff, 128);
|
||||
#endif
|
||||
return hostname;
|
||||
__platform_gethostname(hostname_buff, 128);
|
||||
return obj_cacheStr(self, hostname);
|
||||
}
|
@ -4,6 +4,7 @@ AF_INET = 2
|
||||
SOCK_STREAM = 1
|
||||
|
||||
|
||||
|
||||
class socket(_socket.socket):
|
||||
family = AF_INET
|
||||
type = SOCK_STREAM
|
||||
@ -21,9 +22,6 @@ class socket(_socket.socket):
|
||||
self.proto = vars[2]
|
||||
self._init()
|
||||
|
||||
def gethostname(self):
|
||||
return self._gethostname()
|
||||
|
||||
def bind(self, host_port):
|
||||
host = host_port[0]
|
||||
port = host_port[1]
|
||||
@ -51,3 +49,6 @@ class socket(_socket.socket):
|
||||
|
||||
def recv(self, num):
|
||||
return self._recv(num)
|
||||
|
||||
def gethostname():
|
||||
return _socket._gethostname()
|
||||
|
@ -180,4 +180,21 @@ TEST(unittest, test2) {
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
TEST(socket, gethostname) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
pikaVM_runSingleFile(pikaMain, "test/python/socket/gethostname.py");
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_EQ(ARG_TYPE_STRING, args_getType(pikaMain->list, "hostname"));
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
4
port/linux/test/python/socket/gethostname.py
Normal file
4
port/linux/test/python/socket/gethostname.py
Normal file
@ -0,0 +1,4 @@
|
||||
import socket
|
||||
|
||||
hostname = socket.gethostname()
|
||||
print(hostname)
|
Loading…
x
Reference in New Issue
Block a user