Open flykker opened 12 months ago
I victory my problem
And it's result code
import socket
import sys
import os
class epoll_data:
ptr: Ptr[byte]
fd: i32
u32: u32
u64: u64
def __repr__(self) -> str:
return f'[ptr={self.ptr},fd={self.fd},u32={self.u32},u64={self.u64}]'
@tuple
class epoll_event:
events: u32
data: epoll_data
def __new__(_events: u32, _data: epoll_data):
return epoll_event(_events, _data)
def __repr__(self) -> str:
return f'[events={self.events},data={self.data}]'
from C import epoll_create(i32) -> i32
from C import epoll_ctl(i32, i32, i32, Ptr[byte]) -> i32
from C import epoll_wait(i32, Ptr[byte], i32, i32) -> i32
EPOLLIN = u32(0x001)
EPOLLET = u32(int(1u) << 31)
EPOLL_CTL_ADD = i32(1)
EPOLL_CTL_DEL = i32(2)
EPOLL_CTL_MOD = i32(3)
host = '127.0.0.1' # Symbolic name meaning all available interfaces
port = 8001 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setnonblocking(s.__sock);
s.bind((host, port))
s.listen(1)
print(host, port)
wait_events = __array__[epoll_event](256)
ev = epoll_event(EPOLLIN)
ev.data.fd = s.__sock
print(ev)
epollfd = epoll_create(i32(256))
epoll_ctl(epollfd, EPOLL_CTL_ADD, s.__sock, __ptr__(ev).as_byte())
while True:
new_events = epoll_wait(epollfd, wait_events.ptr.as_byte(), i32(256), i32(-1))
print(int(new_events))
if int(new_events) == -1:
break
for i in range(int(new_events)):
print(i)
event = wait_events[i]
print(f'Event: {event}')
if event.data.fd == s.__sock:
sock_conn, sock_addr = s.accept()
socket.setnonblocking(sock_conn.__sock);
print('Connected by: ', sock_addr)
ev2 = epoll_event(EPOLLIN | EPOLLET)
ev2.data.fd = sock_conn.__sock
epoll_ctl(epollfd, EPOLL_CTL_ADD, sock_conn.__sock, __ptr__(ev2).as_byte())
else:
newsockfd = event.data.fd
data = socket.recv(newsockfd, 1024)
print("Client says: " + data)
if not data:
break
else:
#shutdown(newsockfd, SHUT_RDWR)
socket.sendall(newsockfd, "HTTP/1.1 200 OK\nServer: Z-Server\nContent-type: text/html\n\n<html><body>Z-Server</body></html>")
epoll_ctl(epollfd, EPOLL_CTL_DEL, newsockfd, Ptr[byte](0))
socket.close(newsockfd)
But when run and build sometime not working and
in it place code
event = wait_events[i]
print(f'Event: {event}')
Get sometimes Segmentation Fault .... Very very strange
Please help ! Codon is the Super !
codon run -release http_server.codon
All times Segmentation Fault
event = wait_events[i]
print(f'Event: {event}')
This is definitely a bug—thank you for the report!
Also, you should initialize ptr
here:
def __init__(self):
pass
Otherwise it will get garbage value and might cause segfault.
I develop epoll linux on codon
Don't work epoll_wait
When I get events[0] i get Segmention fault C
But in strace I see what epoll_wait
I see what set array [{events=EPOLLIN, data={u32=0, u64=3942918103876239360}}] epoll_event but in code events[0] is epmty
Please help !