-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
157 lines (131 loc) · 4.17 KB
/
handler.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
147
148
149
150
151
152
153
154
155
156
157
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 net
import (
"errors"
"fmt"
"github.com/zhukovaskychina/xmysql-server/server/conf"
"github.com/zhukovaskychina/xmysql-server/server/innodb"
"github.com/zhukovaskychina/xmysql-server/server/innodb/engine"
"github.com/zhukovaskychina/xmysql-server/server/mysql"
"github.com/zhukovaskychina/xmysql-server/server/protocol"
"sync"
//"github.com/zhukovaskychina/xmysql-serverimpl/serverimpl/net/service"
)
import (
log "github.com/sirupsen/logrus"
)
const (
WritePkgTimeout = 1e8
)
var (
errTooManySessions = errors.New("Too many MySQL sessions!")
)
var (
ErrNotEnoughStream = errors.New("packet stream is not enough")
ErrTooLargePackage = errors.New("package length is exceed the echo package's legal maximum length.")
ErrIllegalMagic = errors.New("package magic is not right.")
)
type PackageHandler interface {
Handle(Session, *MySQLPackage) error
}
type MySQLPackageHandler struct {
}
func (h *MySQLPackageHandler) Handle(session Session, pkg *MySQLPackage) error {
log.Debug("get echo package{%s}", pkg)
// write echo message handle logic here.
return session.WritePkg(nil, WritePkgTimeout)
}
type MySQLMessageHandler struct {
rwlock sync.RWMutex
cfg *conf.Cfg
sessionMap map[Session]innodb.MySQLServerSession //内存区,用于存储mysql的session
XMySQLEngine *engine.XMySQLEngine
}
func NewMySQLMessageHandler(cfg *conf.Cfg) *MySQLMessageHandler {
var mySQLMessageHandler = new(MySQLMessageHandler)
mySQLMessageHandler.sessionMap = make(map[Session]innodb.MySQLServerSession)
mySQLMessageHandler.cfg = cfg
mySQLMessageHandler.XMySQLEngine = engine.NewXMySQLEngine(cfg)
return mySQLMessageHandler
}
func (m *MySQLMessageHandler) OnOpen(session Session) error {
var (
err error
)
m.rwlock.RLock()
if m.cfg.SessionNumber <= len(m.sessionMap) {
err = errTooManySessions
}
m.rwlock.RUnlock()
if err != nil {
return err
}
log.Info("got session:%s", session.Stat())
m.rwlock.Lock()
m.sessionMap[session] = NewMySQLServerSession(session)
m.rwlock.Unlock()
//主动与客户端握手
m.sessionMap[session].SendHandleOk()
return nil
}
func (m *MySQLMessageHandler) OnClose(session Session) {
session.Close()
delete(m.sessionMap, session)
}
func (m *MySQLMessageHandler) OnError(session Session, err error) {
session.Close()
delete(m.sessionMap, session)
}
func (m *MySQLMessageHandler) OnCron(session Session) {
fmt.Println("session 检查")
}
func (m *MySQLMessageHandler) OnMessage(session Session, pkg interface{}) {
currentMysqlSession := m.sessionMap[session]
recMySQLPkg := pkg.(*MySQLPackage)
authStatus := session.GetAttribute("auth_status")
if authStatus == nil {
a := new(protocol.AuthPacket)
var authData = make([]byte, 0)
authData = append(authData, recMySQLPkg.Header.PacketLength...)
authData = append(authData, recMySQLPkg.Header.PacketId)
authData = append(authData, recMySQLPkg.Body...)
a.DecodeAuth(authData)
session.SetAttribute("auth_status", "success")
currentMysqlSession.SetCurrentDatabase(a.Database)
m.sessionMap[session] = currentMysqlSession
buff := make([]byte, 0)
session.WriteBytes(protocol.EncodeOK(buff, 0, 0, nil))
return
}
packetType := recMySQLPkg.Body[0]
switch packetType {
case mysql.ComSleep:
{
break
}
case mysql.ComQuery:
{
sql := string(recMySQLPkg.Body[1:])
m.XMySQLEngine.ExecuteQuery(currentMysqlSession, sql)
}
case mysql.ComQuit:
{
fmt.Println("")
}
}
}