forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
152 lines (139 loc) · 3.89 KB
/
Copy pathclient.go
File metadata and controls
152 lines (139 loc) · 3.89 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
// Copyright (c) 2021 Andy Pan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gnet
import (
"context"
"net"
"os"
"sync"
"golang.org/x/sys/unix"
gerrors "github.com/panjf2000/gnet/errors"
"github.com/panjf2000/gnet/internal/netpoll"
"github.com/panjf2000/gnet/internal/socket"
"github.com/panjf2000/gnet/logging"
)
type Client struct {
opts *Options
el *eventloop
logFlush func() error
}
func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err error) {
options := loadOptions(opts...)
cli = new(Client)
var logger logging.Logger
if options.LogPath != "" {
if logger, cli.logFlush, err = logging.CreateLoggerAsLocalFile(options.LogPath, options.LogLevel); err != nil {
return
}
} else {
logger = logging.GetDefaultLogger()
}
if options.Logger == nil {
options.Logger = logger
}
if options.Codec == nil {
cli.opts.Codec = new(BuiltInFrameCodec)
}
cli.opts = options
var p *netpoll.Poller
if p, err = netpoll.OpenPoller(); err != nil {
return
}
svr := new(server)
svr.opts = options
svr.eventHandler = eventHandler
svr.ln = new(listener)
svr.cond = sync.NewCond(&sync.Mutex{})
if options.Ticker {
svr.tickerCtx, svr.cancelTicker = context.WithCancel(context.Background())
}
el := new(eventloop)
el.ln = svr.ln
el.svr = svr
el.poller = p
el.buffer = make([]byte, 16*1024)
el.connections = make(map[int]*conn)
el.eventHandler = eventHandler
cli.el = el
return
}
// Start starts the client event-loop, handing IO events.
func (cli *Client) Start() error {
cli.el.eventHandler.OnInitComplete(Server{})
go cli.el.activateSubReactor(cli.opts.LockOSThread)
// Start the ticker.
if cli.opts.Ticker {
go cli.el.loopTicker(cli.el.svr.tickerCtx)
}
return nil
}
// Stop stops the client event-loop.
func (cli *Client) Stop() (err error) {
err = cli.el.poller.UrgentTrigger(func(_ interface{}) error { return gerrors.ErrServerShutdown }, nil)
cli.el.svr.waitForShutdown()
cli.el.eventHandler.OnShutdown(Server{})
// Stop the ticker.
if cli.opts.Ticker {
cli.el.svr.cancelTicker()
}
if cli.logFlush != nil{
err = cli.logFlush()
}
logging.Cleanup()
return
}
// Dial is like net.Dial().
func (cli *Client) Dial(network, address string) (gc Conn, err error) {
var c net.Conn
c, err = net.Dial(network, address)
if err != nil {
return nil, err
}
defer c.Close()
v, ok := c.(interface{ File() (*os.File, error) })
if !ok {
return nil, gerrors.ErrUnsupportedProtocol
}
var file *os.File
file, err = v.File()
if err != nil {
return nil, err
}
fd := int(file.Fd())
var sockAddr unix.Sockaddr
switch c.(type) {
case *net.UnixConn:
if sockAddr, _, _, err = socket.GetUnixSockAddr(c.LocalAddr().Network(), c.LocalAddr().String()); err != nil {
return
}
gc = newTCPConn(fd, cli.el, sockAddr, cli.opts.Codec, c.LocalAddr(), c.RemoteAddr())
case *net.TCPConn:
if sockAddr, _, _, _, err = socket.GetTCPSockAddr(c.LocalAddr().Network(), c.LocalAddr().String()); err != nil {
return nil, err
}
gc = newTCPConn(fd, cli.el, sockAddr, cli.opts.Codec, c.LocalAddr(), c.RemoteAddr())
case *net.UDPConn:
if sockAddr, _, _, _, err = socket.GetUDPSockAddr(c.LocalAddr().Network(), c.LocalAddr().String()); err != nil {
return
}
gc = newUDPConn(fd, cli.el, c.LocalAddr(), sockAddr)
default:
return nil, gerrors.ErrUnsupportedProtocol
}
err = cli.el.poller.UrgentTrigger(cli.el.loopRegister, gc)
if err != nil {
gc.Close()
}
return
}