-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
server.go
108 lines (91 loc) · 2.29 KB
/
server.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
106
107
108
package transport
import (
"context"
"net"
"sync"
"golang.org/x/xerrors"
"github.com/gotd/td/internal/proto/codec"
)
// NewCustomServer creates new MTProto server with custom transport codec.
func NewCustomServer(c func() Codec, listener net.Listener) *Server {
return &Server{
codec: c,
listener: &onceCloseListener{Listener: listener},
}
}
// NewFullServer creates new MTProto server with
// Full transport codec.
func NewFullServer(listener net.Listener) *Server {
return NewCustomServer(func() Codec {
return &codec.Full{}
}, listener)
}
// NewIntermediateServer creates new MTProto server with
// Intermediate transport codec.
func NewIntermediateServer(listener net.Listener) *Server {
return NewCustomServer(func() Codec {
return &codec.Intermediate{}
}, listener)
}
// Handler is MTProto server connection handler.
type Handler func(ctx context.Context, conn Conn) error
// Server is a simple MTProto server.
type Server struct {
codec func() Codec
listener net.Listener
ctx context.Context
cancel context.CancelFunc
cancelMx sync.Mutex
}
func (s *Server) serveConn(ctx context.Context, handler Handler, c net.Conn) error {
transportCodec := s.codec()
if err := transportCodec.ReadHeader(c); err != nil {
return xerrors.Errorf("read header: %w", err)
}
if v, ok := ctx.Deadline(); ok {
if err := c.SetDeadline(v); err != nil {
return xerrors.Errorf("set deadline: %w", err)
}
}
return handler(ctx, &connection{
conn: c,
codec: transportCodec,
})
}
// Addr returns server address.
func (s *Server) Addr() net.Addr {
return s.listener.Addr()
}
// Serve runs server using given listener.
func (s *Server) Serve(ctx context.Context, handler Handler) error {
s.cancelMx.Lock()
s.ctx, s.cancel = context.WithCancel(ctx)
s.cancelMx.Unlock()
for {
conn, err := s.listener.Accept()
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
case <-s.ctx.Done():
// If parent context is not done, so
// server context is canceled by Close.
return nil
default:
}
return err
}
go func() {
_ = s.serveConn(s.ctx, handler, conn)
}()
}
}
// Close stops server and closes given listener.
func (s *Server) Close() error {
s.cancelMx.Lock()
if s.cancel != nil {
s.cancel()
}
s.cancelMx.Unlock()
return s.listener.Close()
}