-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] llgo/c/hyper-related c lib #523
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/goplus/llgo/c" | ||
"github.com/goplus/llgo/c/net" | ||
) | ||
|
||
func main() { | ||
var hints net.AddrInfo | ||
hints.AiFamily = net.AF_UNSPEC | ||
hints.AiSockType = net.SOCK_STREAM | ||
|
||
host := "httpbin.org" | ||
port := "80" | ||
|
||
var result *net.AddrInfo | ||
c.Printf(c.Str("%d\n"), net.Getaddrinfo(c.Str(host), c.Str(port), &hints, &result)) | ||
|
||
c.Printf(c.Str("%d\n"), net.Freeaddrinfo(result)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/goplus/llgo/c" | ||
"github.com/goplus/llgo/c/os" | ||
"github.com/goplus/llgo/c/sys" | ||
"github.com/goplus/llgo/c/syscall" | ||
"unsafe" | ||
) | ||
|
||
func main() { | ||
Check failure on line 11 in _demo/select/select.go GitHub Actions / test (macos-latest, 18)
|
||
var readFds syscall.FdSet | ||
|
||
sys.FD_ZERO(&readFds) | ||
|
||
sys.FD_SET(0, &readFds) | ||
|
||
var tv sys.TimeVal | ||
tv.TvSec = 5 | ||
tv.TvUSec = 0 | ||
|
||
c.Printf(c.Str("Waiting for input on stdin...\n")) | ||
ret := sys.Select(1, &readFds, nil, nil, &tv) | ||
if ret == -1 { | ||
c.Perror(c.Str("select error")) | ||
c.Exit(1) | ||
} else if ret == 0 { | ||
c.Printf(c.Str("Timeout occurred! No data after 5 seconds.\n")) | ||
} else { | ||
if sys.FD_ISSET(0, &readFds) != 0 { | ||
var buffer [100]c.Char | ||
n := os.Read(0, c.Pointer(&buffer[:][0]), unsafe.Sizeof(buffer)-1) | ||
if n == -1 { | ||
c.Perror(c.Str("read error")) | ||
c.Exit(1) | ||
} else if n == 0 { | ||
c.Printf(c.Str("End of file\n")) | ||
} else { | ||
buffer[n] = c.Char(0) | ||
c.Printf(c.Str("Read %ld bytes: %s\n"), n, &buffer[0]) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/goplus/llgo/c" | ||
"github.com/goplus/llgo/c/net" | ||
"github.com/goplus/llgo/c/os" | ||
"github.com/goplus/llgo/c/sys" | ||
"github.com/goplus/llgo/c/syscall" | ||
"unsafe" | ||
) | ||
|
||
const ( | ||
SERVER_IP = "110.242.68.66" // Get the IP address by ping baidu.com | ||
SERVER_PORT = 80 | ||
BUFFER_SIZE = 4096 * 1024 | ||
) | ||
|
||
func main() { | ||
Check failure on line 18 in _demo/select/select2.go GitHub Actions / test (macos-latest, 18)
|
||
var server net.SockaddrIn | ||
|
||
sendBuf := c.Str("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n") | ||
var recvBuf [BUFFER_SIZE]c.Char | ||
var bytes_sent, bytes_received c.Int | ||
|
||
// create net | ||
sock := net.Socket(net.AF_INET, net.SOCK_STREAM, 0) | ||
if sock < 0 { | ||
c.Perror(c.Str("Socket creation failed")) | ||
return | ||
} | ||
|
||
// set server addr | ||
c.Memset(c.Pointer(&server), 0, unsafe.Sizeof(server)) | ||
server.Family = net.AF_INET | ||
server.Port = net.Htons(SERVER_PORT) | ||
server.Addr.Addr = net.InetAddr(c.Str(SERVER_IP)) | ||
|
||
// connect to server | ||
if net.Connect(sock, (*net.SockAddr)(c.Pointer(&server)), c.Uint(unsafe.Sizeof(server))) < 0 { | ||
c.Perror(c.Str("Connect failed")) | ||
return | ||
} | ||
|
||
var writefds, readfds syscall.FdSet | ||
var timeout sys.TimeVal | ||
|
||
// Monitor socket writes | ||
sys.FD_ZERO(&writefds) | ||
sys.FD_SET(sock, &writefds) | ||
timeout.TvSec = 10 | ||
timeout.TvUSec = 0 | ||
// Use select to monitor the readiness of writes | ||
if sys.Select(sock+1, nil, &writefds, nil, &timeout) > 0 { | ||
if sys.FD_ISSET(sock, &writefds) != 0 { | ||
bytes_sent = c.Int(net.Send(sock, c.Pointer(sendBuf), c.Strlen(sendBuf), 0)) | ||
if bytes_sent < 0 { | ||
c.Perror(c.Str("send failed")) | ||
return | ||
} | ||
} | ||
} else { | ||
c.Perror(c.Str("Select write error")) | ||
return | ||
} | ||
|
||
// Monitor socket reads | ||
sys.FD_ZERO(&readfds) | ||
sys.FD_SET(sock, &readfds) | ||
|
||
// Use select to monitor the readiness of the read operation | ||
if sys.Select(sock+1, &readfds, nil, nil, &timeout) > 0 { | ||
if sys.FD_ISSET(sock, &writefds) != -1 { | ||
bytes_received = c.Int(net.Recv(sock, c.Pointer(&recvBuf[:][0]), BUFFER_SIZE-1, 0)) | ||
if bytes_received < 0 { | ||
c.Perror(c.Str("receive failed")) | ||
return | ||
} | ||
recvBuf[bytes_received] = c.Char(0) | ||
c.Printf(c.Str("Received:\n%s\n"), &recvBuf[0]) | ||
} | ||
} else { | ||
c.Perror(c.Str("Select read error")) | ||
return | ||
} | ||
|
||
os.Close(sock) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,10 @@ const ( | |
O_TRUNC = 0x00000400 | ||
) | ||
|
||
const ( | ||
EAGAIN = 35 | ||
) | ||
|
||
type ( | ||
ModeT C.mode_t | ||
UidT C.uid_t | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <sys/types.h> | ||
|
||
int fd_isset(int n, fd_set *fd) { | ||
return FD_ISSET(n, fd); | ||
} | ||
|
||
void fdSet(int n, fd_set *fd) { | ||
FD_SET(n, fd); | ||
} | ||
|
||
void fd_zero(fd_set *fd) { | ||
FD_ZERO(fd); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sys | ||
|
||
import ( | ||
"github.com/goplus/llgo/c/syscall" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put |
||
_ "unsafe" | ||
|
||
"github.com/goplus/llgo/c" | ||
) | ||
|
||
const ( | ||
LLGoFiles = "_wrap/fddef.c" | ||
LLGoPackage = "link" | ||
) | ||
|
||
// (TODO) merge to timeval | ||
type TimeVal struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put |
||
TvSec c.Long | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
TvUSec c.Int | ||
} | ||
|
||
//go:linkname FD_ZERO C.fd_zero | ||
func FD_ZERO(fdSet *syscall.FdSet) | ||
|
||
//go:linkname FD_SET C.fdSet | ||
func FD_SET(fd c.Int, fdSet *syscall.FdSet) | ||
|
||
//go:linkname FD_ISSET C.fd_isset | ||
func FD_ISSET(fd c.Int, fdSet *syscall.FdSet) c.Int | ||
|
||
//go:linkname Select C.select | ||
func Select(n c.Int, r *syscall.FdSet, w *syscall.FdSet, e *syscall.FdSet, timeout *TimeVal) c.Int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
Ai
prefix