mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
create socket.py
support socket on linux
This commit is contained in:
parent
b26b4ddffa
commit
2a4115722e
13
package/socket/_socket.pyi
Normal file
13
package/socket/_socket.pyi
Normal file
@ -0,0 +1,13 @@
|
||||
from PikaObj import *
|
||||
|
||||
|
||||
class socket:
|
||||
def _gethostname() -> str: ...
|
||||
def _bind(host: str, port: int): ...
|
||||
def _listen(num: int): ...
|
||||
def _accept(): ...
|
||||
def _send(data: any): ...
|
||||
def _close(): ...
|
||||
def _connect(host: str, port: int): ...
|
||||
def _recv(num: int) -> str: ...
|
||||
def _init(): ...
|
130
package/socket/_socket_socket.c
Normal file
130
package/socket/_socket_socket.c
Normal file
@ -0,0 +1,130 @@
|
||||
#include "_socket_socket.h"
|
||||
#ifdef __linux__
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#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
|
||||
if (sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("socket error\n");
|
||||
return;
|
||||
}
|
||||
obj_setInt(self, "sockfd", sockfd);
|
||||
}
|
||||
|
||||
void _socket_socket__close(PikaObj* self) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
close(sockfd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _socket_socket__send(PikaObj* self, Arg* data) {
|
||||
uint8_t* data_send = NULL;
|
||||
int len = 0;
|
||||
if (arg_getType(data) == ARG_TYPE_STRING) {
|
||||
data_send = (uint8_t*)arg_getStr(data);
|
||||
len = strGetSize((char*)data_send);
|
||||
}
|
||||
|
||||
if (arg_getType(data) == ARG_TYPE_BYTES) {
|
||||
data_send = (uint8_t*)arg_getBytes(data);
|
||||
len = arg_getBytesSize(data);
|
||||
}
|
||||
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
int ret = 0;
|
||||
#ifdef __linux__
|
||||
ret = send(sockfd, data_send, len, 0);
|
||||
#endif
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("send error\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _socket_socket__accept(PikaObj* self) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
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
|
||||
if (client_sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("accept error\n");
|
||||
return;
|
||||
}
|
||||
obj_setInt(self, "client_sockfd", client_sockfd);
|
||||
obj_setStr(self, "client_addr", inet_ntoa(client_addr.sin_addr));
|
||||
}
|
||||
|
||||
char* _socket_socket__recv(PikaObj* self, int num) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
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
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("recv error\n");
|
||||
return NULL;
|
||||
}
|
||||
data = (char*)data_recv;
|
||||
return data;
|
||||
}
|
||||
|
||||
void _socket_socket__listen(PikaObj* self, int num) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
listen(sockfd, num);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
struct sockaddr_in server_addr;
|
||||
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
|
||||
}
|
||||
|
||||
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
struct sockaddr_in server_addr;
|
||||
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
|
||||
}
|
||||
|
||||
char* _socket_socket__gethostname(PikaObj* self) {
|
||||
char hostname_buff[128] = {0};
|
||||
char* hostname = (char*)hostname_buff;
|
||||
#ifdef __linux__
|
||||
gethostname(hostname_buff, 128);
|
||||
#endif
|
||||
return hostname;
|
||||
}
|
53
package/socket/socket.py
Normal file
53
package/socket/socket.py
Normal file
@ -0,0 +1,53 @@
|
||||
import _socket
|
||||
|
||||
AF_INET = 2
|
||||
SOCK_STREAM = 1
|
||||
|
||||
|
||||
class socket(_socket.socket):
|
||||
family = AF_INET
|
||||
type = SOCK_STREAM
|
||||
sockfd = 0
|
||||
client_sockfd = 0
|
||||
client_addr = ''
|
||||
proto = 0
|
||||
|
||||
def __init__(self, *vars):
|
||||
if len(vars) > 0:
|
||||
self.family = vars[0]
|
||||
if len(vars) > 1:
|
||||
self.type = vars[1]
|
||||
if len(vars) > 2:
|
||||
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]
|
||||
return self._bind(host, port)
|
||||
|
||||
def listen(self, num):
|
||||
return self._listen(num)
|
||||
|
||||
def accept(self):
|
||||
self._accept()
|
||||
client = socket()
|
||||
client.sockfd = self.client_sockfd
|
||||
return (client, self.client_addr)
|
||||
|
||||
def send(self, data):
|
||||
return self._send(data)
|
||||
|
||||
def close(self):
|
||||
self._close()
|
||||
|
||||
def connect(self, host_port):
|
||||
host = host_port[0]
|
||||
port = host_port[1]
|
||||
return self._connect(host, port)
|
||||
|
||||
def recv(self, num):
|
||||
return self._recv(num)
|
8
port/linux/.vscode/settings.json
vendored
8
port/linux/.vscode/settings.json
vendored
@ -4,6 +4,12 @@
|
||||
"pikastdlib_sysobj.h": "c",
|
||||
"pikastddata_list.h": "c",
|
||||
"dataargs.h": "c",
|
||||
"dataarg.h": "c"
|
||||
"dataarg.h": "c",
|
||||
"_socket_socket.h": "c",
|
||||
"*.tcc": "c",
|
||||
"fstream": "c",
|
||||
"system_error": "c",
|
||||
"unistd.h": "c",
|
||||
"types.h": "c"
|
||||
}
|
||||
}
|
13
port/linux/package/pikascript/_socket.pyi
Normal file
13
port/linux/package/pikascript/_socket.pyi
Normal file
@ -0,0 +1,13 @@
|
||||
from PikaObj import *
|
||||
|
||||
|
||||
class socket:
|
||||
def _gethostname() -> str: ...
|
||||
def _bind(host: str, port: int): ...
|
||||
def _listen(num: int): ...
|
||||
def _accept(): ...
|
||||
def _send(data: any): ...
|
||||
def _close(): ...
|
||||
def _connect(host: str, port: int): ...
|
||||
def _recv(num: int) -> str: ...
|
||||
def _init(): ...
|
@ -16,6 +16,7 @@ import PikaDebug
|
||||
import PikaCV
|
||||
import binascii
|
||||
import unittest
|
||||
import socket
|
||||
|
||||
mem = PikaStdLib.MemChecker()
|
||||
print('hello pikascript!')
|
||||
|
@ -0,0 +1,130 @@
|
||||
#include "_socket_socket.h"
|
||||
#ifdef __linux__
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#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
|
||||
if (sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("socket error\n");
|
||||
return;
|
||||
}
|
||||
obj_setInt(self, "sockfd", sockfd);
|
||||
}
|
||||
|
||||
void _socket_socket__close(PikaObj* self) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
close(sockfd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _socket_socket__send(PikaObj* self, Arg* data) {
|
||||
uint8_t* data_send = NULL;
|
||||
int len = 0;
|
||||
if (arg_getType(data) == ARG_TYPE_STRING) {
|
||||
data_send = (uint8_t*)arg_getStr(data);
|
||||
len = strGetSize((char*)data_send);
|
||||
}
|
||||
|
||||
if (arg_getType(data) == ARG_TYPE_BYTES) {
|
||||
data_send = (uint8_t*)arg_getBytes(data);
|
||||
len = arg_getBytesSize(data);
|
||||
}
|
||||
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
int ret = 0;
|
||||
#ifdef __linux__
|
||||
ret = send(sockfd, data_send, len, 0);
|
||||
#endif
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("send error\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _socket_socket__accept(PikaObj* self) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
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
|
||||
if (client_sockfd < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("accept error\n");
|
||||
return;
|
||||
}
|
||||
obj_setInt(self, "client_sockfd", client_sockfd);
|
||||
obj_setStr(self, "client_addr", inet_ntoa(client_addr.sin_addr));
|
||||
}
|
||||
|
||||
char* _socket_socket__recv(PikaObj* self, int num) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
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
|
||||
if (ret < 0) {
|
||||
obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
|
||||
__platform_printf("recv error\n");
|
||||
return NULL;
|
||||
}
|
||||
data = (char*)data_recv;
|
||||
return data;
|
||||
}
|
||||
|
||||
void _socket_socket__listen(PikaObj* self, int num) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
#ifdef __linux__
|
||||
listen(sockfd, num);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _socket_socket__connect(PikaObj* self, char* host, int port) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
struct sockaddr_in server_addr;
|
||||
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
|
||||
}
|
||||
|
||||
void _socket_socket__bind(PikaObj* self, char* host, int port) {
|
||||
int sockfd = obj_getInt(self, "sockfd");
|
||||
struct sockaddr_in server_addr;
|
||||
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
|
||||
}
|
||||
|
||||
char* _socket_socket__gethostname(PikaObj* self) {
|
||||
char hostname_buff[128] = {0};
|
||||
char* hostname = (char*)hostname_buff;
|
||||
#ifdef __linux__
|
||||
gethostname(hostname_buff, 128);
|
||||
#endif
|
||||
return hostname;
|
||||
}
|
53
port/linux/package/pikascript/socket.py
Normal file
53
port/linux/package/pikascript/socket.py
Normal file
@ -0,0 +1,53 @@
|
||||
import _socket
|
||||
|
||||
AF_INET = 2
|
||||
SOCK_STREAM = 1
|
||||
|
||||
|
||||
class socket(_socket.socket):
|
||||
family = AF_INET
|
||||
type = SOCK_STREAM
|
||||
sockfd = 0
|
||||
client_sockfd = 0
|
||||
client_addr = ''
|
||||
proto = 0
|
||||
|
||||
def __init__(self, *vars):
|
||||
if len(vars) > 0:
|
||||
self.family = vars[0]
|
||||
if len(vars) > 1:
|
||||
self.type = vars[1]
|
||||
if len(vars) > 2:
|
||||
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]
|
||||
return self._bind(host, port)
|
||||
|
||||
def listen(self, num):
|
||||
return self._listen(num)
|
||||
|
||||
def accept(self):
|
||||
self._accept()
|
||||
client = socket()
|
||||
client.sockfd = self.client_sockfd
|
||||
return (client, self.client_addr)
|
||||
|
||||
def send(self, data):
|
||||
return self._send(data)
|
||||
|
||||
def close(self):
|
||||
self._close()
|
||||
|
||||
def connect(self, host_port):
|
||||
host = host_port[0]
|
||||
port = host_port[1]
|
||||
return self._connect(host, port)
|
||||
|
||||
def recv(self, num):
|
||||
return self._recv(num)
|
Loading…
x
Reference in New Issue
Block a user