-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (87 loc) · 2.04 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"fmt"
"io"
"net"
"os"
"sync"
)
// processing state enums
const (
WAIT_FOR_MSG = iota
IN_MSG
)
func main() {
port := flag.Int("port", 9090, "Specify the port for the TCP server. (default is 9090)")
numOfWorkers := flag.Int("workers", 2, "Specify the number of workers in the thread pool. (default is 2)")
flag.Parse()
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting the server: %v", err)
os.Exit(1)
}
defer listener.Close()
fmt.Fprintf(os.Stdout, "TCP server running on port %d\n", *port)
clientCh := make(chan net.Conn)
var wg sync.WaitGroup
for i := 0; i < *numOfWorkers; i++ {
wg.Add(1)
go worker(clientCh, &wg)
}
for {
client, err := listener.Accept()
if err != nil {
fmt.Fprintf(os.Stdout, "Error accepting connection: %v", err)
continue
}
clientCh <- client
fmt.Printf("Client connected: %s\n", client.RemoteAddr().String())
}
}
func worker(clientCh chan net.Conn, wg *sync.WaitGroup) {
defer wg.Done()
for client := range clientCh {
serveConnection(client)
}
}
func serveConnection(conn net.Conn) {
defer conn.Close()
// sending acknowledgment to the client
if _, err := conn.Write([]byte("*")); err != nil {
fmt.Fprintf(os.Stderr, "Error writing to client: %v\n", err)
return
}
state := WAIT_FOR_MSG
for {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
if err == io.EOF {
break // Client closed the connection
}
fmt.Fprintf(os.Stderr, "Error reading from client: %v\n", err)
return
}
for i := 0; i < n; i++ {
switch state {
case WAIT_FOR_MSG:
if buf[i] == '^' {
state = IN_MSG
fmt.Fprintf(os.Stdout, "In-Message State\n")
}
case IN_MSG:
if buf[i] == '$' {
state = WAIT_FOR_MSG
fmt.Fprintf(os.Stdout, "Wait-For-Message State\n")
} else {
buf[i]++
if _, err := conn.Write(buf[i : i+1]); err != nil {
fmt.Fprintf(os.Stderr, "Error writing to client: %v\n", err)
return
}
}
}
}
}
}