-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (135 loc) · 3.28 KB
/
Copy pathmain.go
File metadata and controls
155 lines (135 loc) · 3.28 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"flag"
"fmt"
"log/slog"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/anthdm/hollywood/actor"
)
type handler struct{}
func newHandler() actor.Receiver {
return &handler{}
}
func (handler) Receive(c *actor.Context) {
switch msg := c.Message().(type) {
case []byte:
fmt.Println("got message to handle:", string(msg))
case actor.Stopped:
for i := range 3 {
fmt.Printf("\r handler stopping in %d", 3-i)
time.Sleep(time.Second)
}
fmt.Println("handler stopped")
}
}
type session struct {
conn net.Conn
}
func newSession(conn net.Conn) actor.Producer {
return func() actor.Receiver {
return &session{
conn: conn,
}
}
}
func (s *session) Receive(c *actor.Context) {
switch c.Message().(type) {
case actor.Initialized:
case actor.Started:
slog.Info("new connection", "addr", s.conn.RemoteAddr())
go s.readLoop(c)
case actor.Stopped:
s.conn.Close()
}
}
func (s *session) readLoop(c *actor.Context) {
buf := make([]byte, 1024)
for {
n, err := s.conn.Read(buf)
if err != nil {
slog.Error("conn read error", "err", err)
break
}
// copy shared buffer, to prevent race conditions.
msg := make([]byte, n)
copy(msg, buf[:n])
// Send to the handler to process to message
c.Send(c.Parent().Child("handler/default"), msg)
}
// Loop is done due to error or we need to close due to server shutdown.
c.Send(c.Parent(), &connRem{pid: c.PID()})
}
type connAdd struct {
pid *actor.PID
conn net.Conn
}
type connRem struct {
pid *actor.PID
}
type server struct {
listenAddr string
ln net.Listener
sessions map[*actor.PID]net.Conn
}
func newServer(listenAddr string) actor.Producer {
return func() actor.Receiver {
return &server{
listenAddr: listenAddr,
sessions: make(map[*actor.PID]net.Conn),
}
}
}
func (s *server) Receive(c *actor.Context) {
switch msg := c.Message().(type) {
case actor.Initialized:
ln, err := net.Listen("tcp", s.listenAddr)
if err != nil {
panic(err)
}
s.ln = ln
// start the handler that will handle the incomming messages from clients/sessions.
c.SpawnChild(newHandler, "handler", actor.WithID("default"))
case actor.Started:
slog.Info("server started", "addr", s.listenAddr)
go s.acceptLoop(c)
case actor.Stopped:
// on stop all the childs sessions will automatically get the stop
// message and close all their underlying connection.
case *connAdd:
slog.Debug("added new connection to my map", "addr", msg.conn.RemoteAddr(), "pid", msg.pid)
s.sessions[msg.pid] = msg.conn
case *connRem:
slog.Debug("removed connection from my map", "pid", msg.pid)
delete(s.sessions, msg.pid)
}
}
func (s *server) acceptLoop(c *actor.Context) {
for {
conn, err := s.ln.Accept()
if err != nil {
slog.Error("accept error", "err", err)
break
}
pid := c.SpawnChild(newSession(conn), "session", actor.WithID(conn.RemoteAddr().String()))
c.Send(c.PID(), &connAdd{
pid: pid,
conn: conn,
})
}
}
func main() {
listenAddr := flag.String("listenaddr", ":6000", "listen address of the TCP server")
e, err := actor.NewEngine(actor.NewEngineConfig())
if err != nil {
panic(err)
}
serverPID := e.Spawn(newServer(*listenAddr), "server")
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
<-sigch
<-e.Poison(serverPID).Done()
}