forked from lonng/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_session.go
94 lines (82 loc) · 3.56 KB
/
kernel_session.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
// Copyright (c) nano Authors. All Rights Reserved.
//
// 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.
package session
import (
"net"
"sync"
)
// NetworkEntity represent low-level network instance
type NetworkEntity interface {
Push(route string, v interface{}) error
RPC(mid uint64, route string, v interface{}) error
Response(mid uint64, route string, v interface{}) error
Close() error
RemoteAddr() net.Addr
}
// EventCallback is the func called after event trigged
type EventCallback func(*Session, ...interface{})
// Session represents a client session which could storage temp data during low-level
// keep connected, all data will be released when the low-level connection was broken.
// Session instance related to the client will be passed to Handler method as the first
// parameter.
type KernelSession struct {
sync.RWMutex // protect data
id int64 // session global unique id
branch uint32 // logic branch
VersionBound bool // session version bound
shortVer uint32 // session short version
version string // session version
uid int64 // binding user id
entity NetworkEntity // low-level network entity
data map[string]interface{} // session data store
router *Router // store remote addr
onEvents map[interface{}][]EventCallback // call EventCallback after event trigged
}
// New returns a new session instance
// a NetworkEntity is a low-level network instance
func NewKernelSession(entity NetworkEntity, id int64) *KernelSession {
return &KernelSession{
id: id,
entity: entity,
data: make(map[string]interface{}),
router: newRouter(),
onEvents: make(map[interface{}][]EventCallback),
}
}
// NetworkEntity returns the low-level network agent object
func (s *KernelSession) NetworkEntity() NetworkEntity {
return s.entity
}
// Router returns the service router
func (s *KernelSession) Router() *Router {
return s.router
}
// RPC sends message to remote server
func (s *KernelSession) RPC(mid uint64, route string, v interface{}) error {
return s.entity.RPC(mid, route, v)
}
// Push message to client
func (s *KernelSession) Push(route string, v interface{}) error {
return s.entity.Push(route, v)
}
// Response message to client
func (s *KernelSession) Response(mid uint64, route string, v interface{}) error {
return s.entity.Response(mid, route, v)
}