-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (77 loc) · 2.47 KB
/
Copy pathmain.go
File metadata and controls
89 lines (77 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// The `so/net` package provides the tools we need
// to easily build TCP socket servers.
package main
import (
"solod.dev/so/bufio"
"solod.dev/so/conc"
"solod.dev/so/fmt"
"solod.dev/so/mem"
"solod.dev/so/net"
"solod.dev/so/strings"
)
func main() {
// Resolve the local address to listen on (port 8090 on all interfaces).
laddr, err := net.ResolveTCPAddr("tcp", ":8090")
if err != nil {
panic(err)
}
// net.ListenTCP starts the server on the TCP network and given address.
listener, err := net.ListenTCP("tcp", &laddr)
if err != nil {
panic(err)
}
// Close the listener to free the port when the application exits.
defer listener.Close()
buf := make([]byte, fmt.BufSize)
println("Server listening on", laddr.String(buf))
// Loop indefinitely to accept new client connections.
for {
// Wait for a connection.
conn, err := listener.Accept()
if err != nil {
println("Error accepting conn:", err)
continue
}
raddr := conn.RemoteAddr()
fmt.Printf("Accepted connection from %s:%d\n", raddr.IP.String(buf), raddr.Port)
// Move connection to the heap so it can be safely used in a new thread.
connPtr := mem.Alloc[net.TCPConn](mem.System)
*connPtr = conn
// We launch a new thread to handle the connection so that
// the main loop can continue accepting more connections.
// In real-world applications, prefer using conc.Pool
// instead of spawning a new thread for each connection.
th := conc.Go(handleConnection, connPtr)
th.Detach()
}
}
// handleConnection handles a single client connection,
// reading one line of text from the client and returning a response.
func handleConnection(arg any) any {
conn := arg.(*net.TCPConn)
defer mem.Free(mem.System, conn)
// Closing the connection releases resources when
// we are finished interacting with the client.
defer conn.Close()
// Use bufio.NewReader to read one line of data
// from the client (terminated by a newline).
reader := bufio.NewReader(mem.System, conn)
defer reader.Free()
message, err := reader.ReadString('\n')
if err != nil {
println("Read error:", err)
return err
}
// Create and send a response back to the client,
// demonstrating two-way communication.
ackMsg := strings.ToUpper(mem.System, strings.TrimSpace(message))
defer mem.FreeString(mem.System, ackMsg)
buf := fmt.NewBuffer(fmt.BufSize)
response := fmt.Sprintf(buf, "ACK: %s\n", ackMsg)
_, err = conn.Write([]byte(response))
if err != nil {
println("Server write error:", err)
return err
}
return nil
}