-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
executable file
·146 lines (128 loc) · 4.28 KB
/
base.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
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
/* MIT License
*
* Copyright (c) 2018 Mike Taghavi <mitghi[at]gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Server contains bunch of interfaces and functionalities for serving
// connections compatible with Protox. It has APIs for interacting with
// the underlaying subsystems.
package server
/*
* TODO:
* . decouple Buffer Pool from direct import
*/
import (
"errors"
"net"
"sync"
/* will be replaced with new lock-free implementation */
"github.com/mitghi/protox/logging"
"github.com/mitghi/protox/protobase"
)
// Logger is the default, structured package level logging service.
var (
logger protobase.LoggingInterface
)
func init() {
logger = logging.NewLogger("Server")
}
// Constants for server client types
const (
// Client
STCLIENT SConnType = iota
// Manager
STMANAGER
// Router
STROUTER
// Resource provider
STRESOURCE
// Monitor and statics collector
STMONITOR
)
type ServerType byte
// Constants for server service types (Protox over : HTTP/S, MQTT, WS, REDIS PROTOCOL, .... )
const (
// Protox over original protocol
ProtoOrigin ServerType = iota
// Protox over HTTP
ProtoHTTP
// Protox over HTTPS
ProtoHTTPS
// Protox over WS
ProtoWS
// Protox over MQTT
ProtoMQTT
// Protox over RP
ProtoRP
)
// Constants for server transport types (UNIX, TCP, SSL/TLS, .... )
const (
ProtoUNIXSO byte = iota
// Protox over TCP
ProtoTCP
// Protox over TLS
ProtoTLS
// Protox over SSL
ProtoSSL
)
// Server error messages
var (
SRVShutdownError error = errors.New("server: cannot shutdown due to incompatible state.")
SRVGeneralError error = errors.New("server: %s")
SRVInvalidAddr error = errors.New("server: invalid address.")
SRVInvalidMode error = errors.New("server: invalid serving mode.")
SRVMissingOptions error = errors.New("server: options are missing.")
SRVTLSInvalidCA error = errors.New("server: invalid caFile.")
)
// SConnTyp is server client type ( CLIENT, RESOURCE, ROUTER, MONITOR, .... )
type SConnType byte
// ServerHandlerFunc is the signature for `ClientInterface` delegation.
type ServerHandlerFunc func(net.Conn) protobase.ClientInterface
// ClientDelegate is a signature for `ClientInterface` delegation.
type ClientDelegate func(string, string, string) protobase.ClientInterface
// ConnectionDelegate is a signature for `ProtoConnection` delegation.
type ConnectionDelegate func(net.Conn) protobase.ProtoConnection
// Defaults
var (
DefaultHeartbeat int = 1
)
// Server is a main implementation of `protocol.ServerInterface`.
type Server struct {
sync.RWMutex
protobase.ServerInterface
corous sync.WaitGroup
Clients map[net.Conn]protobase.ProtoConnection
onNewClient func(string, string, string) protobase.ClientInterface
onNewConnection ConnectionDelegate
onNewMessage ServerHandlerFunc
permissionDelegate func(protobase.AuthInterface, ...string) bool
Authenticator protobase.AuthInterface
Store protobase.MessageStorage
Router protobase.RouterInterface
State *serverState
listener *net.Listener
opts *ServerConfigs
StatusChan chan uint32
critical chan struct{}
heartbeat int
Status uint32
// TODO: NOTE:
// . add timestamp and expiration date.
// . for heartbeat, uint can also be used.
}