-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.go
109 lines (93 loc) · 3.26 KB
/
socket.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
109
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package socket implements a network socket that supports TCP, UNIX, HTTP, WS and INPROC.
package socket
import (
"crypto/tls"
"errors"
"fmt"
"github.com/hslam/netpoll"
"net"
"runtime"
"strings"
)
var numCPU = runtime.NumCPU()
// ErrHandler is the error when the handler is nil
var ErrHandler = errors.New("handler is nil")
// ErrOpened is the error when the opened is nil
var ErrOpened = errors.New("opened is nil")
// ErrServe is the error when the serve is nil
var ErrServe = errors.New("serve is nil")
// ErrConn is the error when the conn is nil
var ErrConn = errors.New("conn is nil")
// ErrNetwork is the error when the network is not supported
var ErrNetwork = errors.New("network is not supported")
// Conn is a generic stream-oriented network connection.
type Conn interface {
net.Conn
// Messages returns a new Messages.
Messages() Messages
// Connection returns the net.Conn.
Connection() net.Conn
}
// Dialer is a generic network dialer for stream-oriented protocols.
type Dialer interface {
// Dial connects to an address.
Dial(address string) (Conn, error)
}
// Listener is a generic network listener for stream-oriented protocols.
type Listener interface {
// Accept waits for and returns the next connection to the listener.
Accept() (Conn, error)
// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
Close() error
// Addr returns the listener's network address.
Addr() net.Addr
// Serve serves the netpoll.Handler by the netpoll.
Serve(handler netpoll.Handler) error
// ServeData serves the opened func and the serve func by the netpoll.
ServeData(opened func(net.Conn) error, serve func(req []byte) (res []byte)) error
// ServeConn serves the opened func and the serve func by the netpoll.
ServeConn(opened func(net.Conn) (Context, error), serve func(Context) error) error
// ServeMessages serves the opened func and the serve func by the netpoll.
ServeMessages(opened func(Messages) (Context, error), serve func(Context) error) error
}
// Context represents a context.
type Context interface{}
// Socket contains the Dialer and the Listener.
type Socket interface {
// Scheme returns the socket's scheme.
Scheme() string
Dialer
// Listen announces on the local address.
Listen(address string) (Listener, error)
}
// Address returns the socket's address by a url.
func Address(s Socket, url string) (string, error) {
if !strings.HasPrefix(url, s.Scheme()+"://") {
return url, errors.New("error url:" + url)
}
return url[len(s.Scheme()+"://"):], nil
}
// URL returns the socket's url by a address.
func URL(s Socket, addr string) string {
return fmt.Sprintf("%s://%s", s.Scheme(), addr)
}
// NewSocket returns a new socket by a network and a TLS config.
func NewSocket(network string, config *tls.Config) (Socket, error) {
switch network {
case "tcp", "tcps":
return NewTCPSocket(config), nil
case "unix", "unixs":
return NewUNIXSocket(config), nil
case "http", "https":
return NewHTTPSocket(config), nil
case "ws", "wss":
return NewWSSocket(config), nil
case "inproc", "inprocs":
return NewINPROCSocket(config), nil
default:
return nil, ErrNetwork
}
}