Added some more server socket fun and examples. (0.27.0)
This commit is contained in:
parent
6d9b082945
commit
92d669a409
8 changed files with 125 additions and 4 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
project(fun VERSION 0.26.6 LANGUAGES C)
|
project(fun VERSION 0.27.0 LANGUAGES C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
|
||||||
13
examples/debug_reporting.fun
Normal file → Executable file
13
examples/debug_reporting.fun
Normal file → Executable file
|
|
@ -1,4 +1,15 @@
|
||||||
//!/usr/bin/env fun
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Fun programming language.
|
||||||
|
* https://fun-lang.xyz/
|
||||||
|
*
|
||||||
|
* Copyright 2025 Johannes Findeisen
|
||||||
|
* Licensed under the terms of the Apache-2.0 license.
|
||||||
|
* https://opensource.org/license/apache-2-0
|
||||||
|
*
|
||||||
|
* Added: 2025-10-06
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Debugging and error reporting demo for Fun.
|
* Debugging and error reporting demo for Fun.
|
||||||
|
|
|
||||||
37
examples/extra/tcp_echo_server.fun
Executable file
37
examples/extra/tcp_echo_server.fun
Executable file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Fun programming language.
|
||||||
|
* https://fun-lang.xyz/
|
||||||
|
*
|
||||||
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||||
|
* Licensed under the terms of the Apache-2.0 license.
|
||||||
|
* https://opensource.org/license/apache-2-0
|
||||||
|
*
|
||||||
|
* Added: 2025-10-13
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Simple TCP echo server using built-in socket ops.
|
||||||
|
// Listens on localhost:12345 and echoes a single line/request back to each client.
|
||||||
|
|
||||||
|
port = 12345
|
||||||
|
backlog = 16
|
||||||
|
|
||||||
|
listen_fd = tcp_listen(port, backlog)
|
||||||
|
if (listen_fd <= 0)
|
||||||
|
print("tcp_listen failed")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
print("Echo server listening on port " + to_string(port))
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
client = tcp_accept(listen_fd)
|
||||||
|
if (client > 0)
|
||||||
|
data = sock_recv(client, 4096)
|
||||||
|
if (len(data) > 0)
|
||||||
|
// Echo back exactly what we received
|
||||||
|
sock_send(client, data)
|
||||||
|
sock_close(client)
|
||||||
|
|
||||||
|
// Not reached in this example, but here for completeness
|
||||||
|
sock_close(listen_fd)
|
||||||
32
examples/extra/tcp_echo_server_class.fun
Executable file
32
examples/extra/tcp_echo_server_class.fun
Executable file
|
|
@ -0,0 +1,32 @@
|
||||||
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Fun programming language.
|
||||||
|
* https://fun-lang.xyz/
|
||||||
|
*
|
||||||
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||||
|
* Licensed under the terms of the Apache-2.0 license.
|
||||||
|
* https://opensource.org/license/apache-2-0
|
||||||
|
*
|
||||||
|
* Added: 2025-10-13
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Echo server using the stdlib TcpServer class.
|
||||||
|
// Make sure FUN_LIB_DIR points to the ./lib directory containing io/socket.fun.
|
||||||
|
// Example:
|
||||||
|
// FUN_LIB_DIR="$(pwd)/lib" ./build/fun ./examples/tcp_echo_server_class.fun
|
||||||
|
|
||||||
|
include <io/socket.fun>
|
||||||
|
|
||||||
|
print("Starting echo server at localhost:12345")
|
||||||
|
|
||||||
|
server = TcpServer(12345, 16)
|
||||||
|
|
||||||
|
if (server.listen() <= 0)
|
||||||
|
print("Listen failed!")
|
||||||
|
exit(1)
|
||||||
|
else
|
||||||
|
print("Echo server (class) listening on port 12345")
|
||||||
|
|
||||||
|
// Serve clients forever, echoing back what they send.
|
||||||
|
server.serve_forever(4096)
|
||||||
0
examples/repl_on_error.fun
Normal file → Executable file
0
examples/repl_on_error.fun
Normal file → Executable file
|
|
@ -25,7 +25,6 @@
|
||||||
* print(resp)
|
* print(resp)
|
||||||
* c.close()
|
* c.close()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class TcpClient()
|
class TcpClient()
|
||||||
// file descriptor or 0 if not connected
|
// file descriptor or 0 if not connected
|
||||||
fd = 0
|
fd = 0
|
||||||
|
|
@ -72,6 +71,48 @@ class TcpClient()
|
||||||
this.fd = 0
|
this.fd = 0
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
// TcpServer: simple wrapper around TCP socket built-ins.
|
||||||
|
// Exposes listen(), accept(), echo_once(maxlen), serve_forever(maxlen), close().
|
||||||
|
class TcpServer(number port, number backlog)
|
||||||
|
|
||||||
|
fun _construct(this, port, backlog)
|
||||||
|
this.port = port
|
||||||
|
this.backlog = backlog
|
||||||
|
this.listen_fd = 0
|
||||||
|
|
||||||
|
fun listen(this)
|
||||||
|
this.listen_fd = tcp_listen(this.port, this.backlog)
|
||||||
|
return this.listen_fd
|
||||||
|
|
||||||
|
fun accept(this)
|
||||||
|
return tcp_accept(this.listen_fd)
|
||||||
|
|
||||||
|
fun close(this)
|
||||||
|
if (this.listen_fd > 0)
|
||||||
|
sock_close(this.listen_fd)
|
||||||
|
this.listen_fd = 0
|
||||||
|
|
||||||
|
fun echo_once(this, maxlen)
|
||||||
|
c = this.accept()
|
||||||
|
if (c <= 0)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
data = sock_recv(c, maxlen)
|
||||||
|
if (len(data) > 0)
|
||||||
|
sock_send(c, data)
|
||||||
|
|
||||||
|
sock_close(c)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
fun serve_forever(this, maxlen)
|
||||||
|
if (this.listen_fd <= 0)
|
||||||
|
if (this.listen() <= 0)
|
||||||
|
print("TcpServer: listen failed on port " + to_string(this.port))
|
||||||
|
return 0
|
||||||
|
print("TcpServer: listening on port " + to_string(this.port))
|
||||||
|
while (true)
|
||||||
|
this.echo_once(maxlen)
|
||||||
|
|
||||||
class UnixClient()
|
class UnixClient()
|
||||||
fd = 0
|
fd = 0
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue