-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtcp.go
77 lines (64 loc) · 1.95 KB
/
tcp.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
package pluto
import (
"net"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
// Address
// TODO: env
const Address = "localhost:9631"
const MAXRequestPerConnection = 1000
var ConnectionHandler = Pipeline{
"TCP_CONNECTION_HANDLER",
ProcessorBucket{[]Processor{acceptor, authenticator, processor}},
}
func init() {
go func() {
l, err := net.Listen("tcp4", Address)
if err != nil {
Log.Fatal("Create TCP listener", zap.String("address", Address))
}
for {
Log.Debug("Waiting for connections")
// TODO: Any check or feature to accept new connections.
conn, err := l.Accept()
if err != nil {
Log.Debug("Failed to accept new connection", zap.Error(err))
continue
}
go ConnectionHandler.Process(&InternalProcessable{
ID: uuid.New(),
Body: map[string]any{"connection": conn},
CreatedAt: time.Now(),
})
}
}()
}
var processor = NewFinalProcessor(
&ConnectionDecoder{
MaxDecode: MAXRequestPerConnection,
ReadDeadline: time.Hour,
ProcessableBuilder: func(context Processable, new OutComingProcessable) Processable {
defer func() { recover() }()
AuthenticatedConnectionsMutex.RLock()
defer AuthenticatedConnectionsMutex.RUnlock()
connection := AuthenticatedConnections[context.GetBody().(map[string]any)["connection_id"].(uuid.UUID)]
new.Producer = connection.Producer.(ExternalIdentifier)
new.ProducerCredential = connection.ProducerCredential.(OutComingCredential)
return &new
},
Processor: NewInlineProcessor(func(processable Processable) (Processable, bool) {
Process(processable.(RoutableProcessable))
return processable, true
}),
},
).Final(
NewInlineProcessor(func(processable Processable) (Processable, bool) {
AuthenticatedConnectionsMutex.Lock()
defer AuthenticatedConnectionsMutex.Unlock()
defer func() { recover() }()
delete(AuthenticatedConnections, processable.GetBody().(map[string]any)["connection_id"].(uuid.UUID))
return processable, true
}),
)