-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicly.go
81 lines (65 loc) · 1.87 KB
/
quicly.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
package quicly
import (
"github.com/Project-Faster/quicly-go/quiclylib"
"github.com/Project-Faster/quicly-go/quiclylib/errors"
"github.com/Project-Faster/quicly-go/quiclylib/types"
"context"
log "github.com/rs/zerolog"
"net"
"os"
"time"
)
var logger log.Logger
func Initialize(options Options) int {
if options.Logger == nil {
log.SetGlobalLevel(log.InfoLevel)
log.TimeFieldFormat = time.StampMilli
logger = log.New(os.Stdout).With().Timestamp().Logger()
} else {
logger = *options.Logger
}
result := quiclylib.QuiclyInitializeEngine(options)
if result != errors.QUICLY_OK {
logger.Error().Msgf("Failed initialization: %v", result)
return result
}
logger.Debug().Msg("Initialized")
return errors.QUICLY_OK
}
func Terminate() {
quiclylib.QuiclyCloseEngine()
logger.Debug().Msg("Terminated")
}
func Listen(localAddr *net.UDPAddr, cb types.Callbacks, ctx context.Context) types.ServerSession {
udpConn, err := net.ListenUDP("udp", localAddr)
if err != nil {
logger.Error().Msgf("Could not listen on specified udp address: %v", err)
return nil
}
_ = udpConn.SetReadBuffer(32 * 1280 * 1024)
_ = udpConn.SetWriteBuffer(32 * 1280 * 1024)
conn := &quiclylib.QServerSession{
NetConn: udpConn,
Ctx: ctx,
Logger: logger.With().Timestamp().Logger(),
Callbacks: cb,
}
return conn
}
func Dial(remoteAddr *net.UDPAddr, cb types.Callbacks, ctx context.Context) types.ClientSession {
udpConn, err := net.DialUDP("udp", nil, remoteAddr)
if err != nil {
logger.Error().Msgf("Could not dial the specified udp address: %v", err)
return nil
}
_ = udpConn.SetReadBuffer(32 * 1280 * 1024)
_ = udpConn.SetWriteBuffer(32 * 1280 * 1024)
conn := &quiclylib.QClientSession{
NetConn: udpConn,
Ctx: ctx,
Logger: logger.With().Timestamp().Logger(),
Callbacks: cb,
}
conn.Logger.Debug().Msgf("[%v] CONNECT %p", conn.ID(), conn)
return conn
}