登录代码提交
This commit is contained in:
109
trunk/game/common/clientMgr/clientMgr.go
Normal file
109
trunk/game/common/clientMgr/clientMgr.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package clientMgr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"goutil/debugUtil"
|
||||
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
var (
|
||||
clientMap = make(map[int32]IClient, 1024)
|
||||
mutex sync.RWMutex
|
||||
)
|
||||
|
||||
func RegisterClient(clientObj IClient) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
clientMap[clientObj.GetId()] = clientObj
|
||||
}
|
||||
|
||||
func UnregisterClient(clientObj IClient) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
delete(clientMap, clientObj.GetId())
|
||||
}
|
||||
|
||||
func GetClient(id int32) (clientObj IClient, exists bool) {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
clientObj, exists = clientMap[id]
|
||||
return
|
||||
}
|
||||
|
||||
func getClientCount() int {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
return len(clientMap)
|
||||
}
|
||||
|
||||
func getExpiredClientList() (expiredList []IClient) {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
for _, item := range clientMap {
|
||||
if item.Expired() {
|
||||
expiredList = append(expiredList, item)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 绑定连接到玩家
|
||||
func BindClientAndPlayer(clientObj IClient, playerObj *Player) {
|
||||
// 发送在另一台设备登陆的信息
|
||||
sendLoginAnotherDeviceMsg := func(clientObj IClient) {
|
||||
responseObj := NewServerResponseObject()
|
||||
responseObj.SetMethodName("Login")
|
||||
responseObj.SetResultStatus(LoginOnAnotherDevice)
|
||||
|
||||
mes, _ := json.Marshal(responseObj)
|
||||
|
||||
if debugUtil.IsDebug() {
|
||||
logUtil.WarnLog("BindClientAndPlayer11BindClientAndPlayer:%v", string(mes))
|
||||
}
|
||||
|
||||
// 先发送消息,然后再断开连接
|
||||
clientObj.SendMessage(responseObj)
|
||||
clientObj.SendMessage(NewDisconnectServerResponseObject())
|
||||
}
|
||||
|
||||
// 立即更新活跃时间,避免在将要清除时,玩家又登陆的极端情况
|
||||
clientObj.Active()
|
||||
clientObj.ClearSendData()
|
||||
|
||||
// 判断是否重复登陆
|
||||
if playerObj.ClientId > 0 {
|
||||
if oldClientObj, exist := GetClient(playerObj.ClientId); exist {
|
||||
// 如果不是同一个客户端,则先给客户端发送在其他设备登陆信息,然后断开连接
|
||||
if clientObj != oldClientObj {
|
||||
sendLoginAnotherDeviceMsg(oldClientObj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新客户端对象的玩家Id、以及玩家对象对应的客户端Id
|
||||
clientObj.PlayerLogin(playerObj.Id)
|
||||
playerObj.ClientLogin(clientObj.GetId())
|
||||
}
|
||||
|
||||
// Disconnect 清理断开的连接数据
|
||||
// / 连接对象
|
||||
func Disconnect(clientObj IClient) {
|
||||
if clientObj.GetPlayerId() != 0 {
|
||||
playerObj, exist, err := getPlayerHandler(clientObj.GetPlayerId(), false)
|
||||
if err == nil && exist && clientObj.GetId() == playerObj.ClientId {
|
||||
playerObj.ClientLogout()
|
||||
}
|
||||
}
|
||||
|
||||
clientObj.Close()
|
||||
UnregisterClient(clientObj)
|
||||
}
|
||||
41
trunk/game/common/clientMgr/expire.go
Normal file
41
trunk/game/common/clientMgr/expire.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package clientMgr
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"framework/goroutineMgr"
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 清理过期的客户端
|
||||
go clearExpiredClient()
|
||||
}
|
||||
|
||||
// 清理过期的客户端
|
||||
func clearExpiredClient() {
|
||||
// 处理goroutine数量
|
||||
goroutineName := "clientMgr.clearExpiredClient"
|
||||
goroutineMgr.Monitor(goroutineName)
|
||||
defer goroutineMgr.ReleaseMonitor(goroutineName)
|
||||
|
||||
for {
|
||||
// 休眠指定的时间(单位:秒)(放在此处是因为程序刚启动时并没有过期的客户端,所以先不用占用资源;并且此时LogPath尚未设置,如果直接执行后面的代码会出现panic异常)
|
||||
time.Sleep(5 * time.Minute)
|
||||
|
||||
// 获取过期的客户端列表
|
||||
expiredClientList := getExpiredClientList()
|
||||
expiredClientCount := len(expiredClientList)
|
||||
beforeClientCount := getClientCount()
|
||||
|
||||
// 客户端断开
|
||||
if expiredClientCount > 0 {
|
||||
for _, item := range expiredClientList {
|
||||
Disconnect(item)
|
||||
}
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
logUtil.DebugLog("清理前的客户端数量为:%d,本次清理不活跃的客户端数量为:%d", beforeClientCount, expiredClientCount)
|
||||
}
|
||||
}
|
||||
86
trunk/game/common/clientMgr/handleRequest.go
Normal file
86
trunk/game/common/clientMgr/handleRequest.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package clientMgr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
. "common/model"
|
||||
"goutil/debugUtil"
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
// HandleRequest 处理客户端请求
|
||||
// clientObj:对应的客户端对象
|
||||
// request:请求内容字节数组(json格式)
|
||||
// 返回值:无
|
||||
func HandleRequest(clientObj IClient, request []byte) {
|
||||
responseObj := NewServerResponseObject()
|
||||
|
||||
defer func() {
|
||||
// 如果是客户端数据错误,则将客户端请求数据记录下来
|
||||
if responseObj.ResultStatus == ClientDataError {
|
||||
logUtil.ErrorLog("client:%s\n请求的数据为:%s, 返回的结果为客户端数据错误", clientObj, string(request))
|
||||
}
|
||||
|
||||
// 调用发送消息接口
|
||||
clientObj.SendMessage(responseObj)
|
||||
|
||||
// 记录DEBUG日志
|
||||
if debugUtil.IsDebug() {
|
||||
result, _ := json.Marshal(responseObj)
|
||||
logUtil.DebugLog("client:%s\nRequest:%s\nResponse:%s", clientObj, string(request), string(result))
|
||||
}
|
||||
}()
|
||||
|
||||
// 解析请求字符串
|
||||
requestObj := new(ServerRequestObject)
|
||||
if err := json.Unmarshal(request, requestObj); err != nil {
|
||||
logUtil.ErrorLog("反序列化出错,字符串:%s,错误信息为:%s", string(request), err)
|
||||
responseObj.SetResultStatus(ClientDataError)
|
||||
return
|
||||
}
|
||||
|
||||
// 为参数赋值
|
||||
responseObj.SetMethodName(requestObj.MethodName)
|
||||
|
||||
// 对参数要特殊处理:将Client、Player特殊处理
|
||||
parameters := make([]interface{}, 0)
|
||||
if requestObj.MethodName == "Login" {
|
||||
parameters = append(parameters, interface{}(clientObj))
|
||||
parameters = append(parameters, requestObj.Parameters...)
|
||||
} else {
|
||||
// 判断玩家是否已经登陆
|
||||
if clientObj.GetPlayerId() == 0 {
|
||||
responseObj.SetResultStatus(NoLogin)
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否能找到玩家
|
||||
var playerObj *Player
|
||||
var exists bool
|
||||
var err error
|
||||
if getPlayerHandler == nil {
|
||||
panic("getPlayerHandler is nil, please set first")
|
||||
}
|
||||
playerObj, exists, err = getPlayerHandler(clientObj.GetPlayerId(), false)
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(DataError)
|
||||
return
|
||||
}
|
||||
|
||||
if !exists {
|
||||
responseObj.SetResultStatus(NoLogin)
|
||||
return
|
||||
}
|
||||
|
||||
parameters = append(parameters, interface{}(clientObj))
|
||||
parameters = append(parameters, interface{}(playerObj))
|
||||
parameters = append(parameters, requestObj.Parameters...)
|
||||
}
|
||||
|
||||
// 为参数赋值
|
||||
requestObj.ModuleName = "Chat"
|
||||
requestObj.Parameters = parameters
|
||||
|
||||
// 调用方法
|
||||
responseObj = callFunction(requestObj)
|
||||
}
|
||||
33
trunk/game/common/clientMgr/interface.go
Normal file
33
trunk/game/common/clientMgr/interface.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package clientMgr
|
||||
|
||||
import . "common/model"
|
||||
|
||||
// IClient 客户端连接接口
|
||||
type IClient interface {
|
||||
// GetId 获取客户端对象的唯一标识
|
||||
GetId() int32
|
||||
|
||||
// 获取玩家Id
|
||||
GetPlayerId() int64
|
||||
|
||||
// 玩家登录
|
||||
PlayerLogin(int64)
|
||||
|
||||
// 发送数据
|
||||
SendMessage(*ServerResponseObject)
|
||||
|
||||
// 清空待发送数据
|
||||
ClearSendData()
|
||||
|
||||
// 客户端活跃
|
||||
Active()
|
||||
|
||||
// 获取远程地址(IP_Port)
|
||||
GetRemoteAddr() string
|
||||
|
||||
// 客户端连接超时
|
||||
Expired() bool
|
||||
|
||||
// 客户端连接对象断开
|
||||
Close()
|
||||
}
|
||||
25
trunk/game/common/clientMgr/reflect_method.go
Normal file
25
trunk/game/common/clientMgr/reflect_method.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package clientMgr
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// 反射的方法和输入、输出参数类型组合类型
|
||||
type methodAndInOutTypes struct {
|
||||
// 反射出来的对应方法对象
|
||||
Method reflect.Value
|
||||
|
||||
// 反射出来的方法的输入参数的类型集合
|
||||
InTypes []reflect.Type
|
||||
|
||||
// 反射出来的方法的输出参数的类型集合
|
||||
OutTypes []reflect.Type
|
||||
}
|
||||
|
||||
func newmethodAndInOutTypes(_method reflect.Value, _inTypes []reflect.Type, _outTypes []reflect.Type) *methodAndInOutTypes {
|
||||
return &methodAndInOutTypes{
|
||||
Method: _method,
|
||||
InTypes: _inTypes,
|
||||
OutTypes: _outTypes,
|
||||
}
|
||||
}
|
||||
356
trunk/game/common/clientMgr/reflect_mgr.go
Normal file
356
trunk/game/common/clientMgr/reflect_mgr.go
Normal file
@@ -0,0 +1,356 @@
|
||||
package clientMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
. "common/model"
|
||||
"goutil/debugUtil"
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
// 供客户端访问的模块的后缀
|
||||
con_ModuleSuffix = "Module"
|
||||
|
||||
// 定义用于分隔模块名称和方法名称的分隔符
|
||||
con_DelimeterOfObjAndMethod = "_"
|
||||
)
|
||||
|
||||
var (
|
||||
// 定义存放所有方法映射的变量
|
||||
methodMap = make(map[string]*methodAndInOutTypes)
|
||||
)
|
||||
|
||||
// 获取结构体类型的名称
|
||||
// structType:结构体类型
|
||||
// 返回值:
|
||||
// 结构体类型的名称
|
||||
func getStructName(structType reflect.Type) string {
|
||||
reflectTypeStr := structType.String()
|
||||
reflectTypeArr := strings.Split(reflectTypeStr, ".")
|
||||
|
||||
return reflectTypeArr[len(reflectTypeArr)-1]
|
||||
}
|
||||
|
||||
// 获取完整的模块名称
|
||||
// moduleName:模块名称
|
||||
// 返回值:
|
||||
// 完整的模块名称
|
||||
func getFullModuleName(moduleName string) string {
|
||||
return moduleName + con_ModuleSuffix
|
||||
}
|
||||
|
||||
// 获取完整的方法名称
|
||||
// structName:结构体名称
|
||||
// methodName:方法名称
|
||||
// 返回值:
|
||||
// 完整的方法名称
|
||||
func getFullMethodName(structName, methodName string) string {
|
||||
return structName + con_DelimeterOfObjAndMethod + methodName
|
||||
}
|
||||
|
||||
// 解析方法的输入输出参数
|
||||
// method:方法对应的反射值
|
||||
// 返回值:
|
||||
// 输入参数类型集合
|
||||
// 输出参数类型集合
|
||||
func resolveMethodInOutParams(method reflect.Value) (inTypes []reflect.Type, outTypes []reflect.Type) {
|
||||
methodType := method.Type()
|
||||
for i := 0; i < methodType.NumIn(); i++ {
|
||||
inTypes = append(inTypes, methodType.In(i))
|
||||
}
|
||||
|
||||
for i := 0; i < methodType.NumOut(); i++ {
|
||||
outTypes = append(outTypes, methodType.Out(i))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 将需要对客户端提供方法的对象进行注册
|
||||
// structObject:对象
|
||||
func RegisterFunction(structObject interface{}) {
|
||||
// 获取structObject对应的反射 Type 和 Value
|
||||
reflectValue := reflect.ValueOf(structObject)
|
||||
reflectType := reflect.TypeOf(structObject)
|
||||
|
||||
// 提取对象类型名称
|
||||
structName := getStructName(reflectType)
|
||||
|
||||
// 获取structObject中返回值为responseObject的方法
|
||||
for i := 0; i < reflectType.NumMethod(); i++ {
|
||||
// 获得方法名称
|
||||
methodName := reflectType.Method(i).Name
|
||||
|
||||
// 判断是否为导出的方法
|
||||
|
||||
// 获得方法及其输入参数的类型列表
|
||||
method := reflectValue.MethodByName(methodName)
|
||||
inTypes, outTypes := resolveMethodInOutParams(method)
|
||||
|
||||
// 判断输出参数数量是否正确
|
||||
if len(outTypes) != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 判断返回值是否为responseObject
|
||||
outType := outTypes[0]
|
||||
if _, ok := outType.FieldByName("Code"); !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := outType.FieldByName("Message"); !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := outType.FieldByName("Data"); !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// 添加到列表中
|
||||
methodMap[getFullMethodName(structName, methodName)] = newmethodAndInOutTypes(method, inTypes, outTypes)
|
||||
|
||||
debugUtil.Println(fmt.Sprintf("%s_%s注册成功,当前共%d个方法", structName, methodName, len(methodMap)))
|
||||
}
|
||||
}
|
||||
|
||||
// 调用方法
|
||||
// requestObj:请求对象
|
||||
func callFunction(requestObj *ServerRequestObject) *ServerResponseObject {
|
||||
responseObj := NewServerResponseObject()
|
||||
var methodAndInOutTypes *methodAndInOutTypes
|
||||
var exists bool
|
||||
|
||||
// 根据传入的ModuleName和MethodName找到对应的方法对象
|
||||
key := getFullMethodName(getFullModuleName(requestObj.ModuleName), requestObj.MethodName)
|
||||
if methodAndInOutTypes, exists = methodMap[key]; !exists {
|
||||
logUtil.ErrorLog("找不到指定的方法:%v,%s", methodMap, key)
|
||||
return responseObj.SetResultStatus(NoTargetMethod)
|
||||
}
|
||||
|
||||
// 判断参数数量是否相同
|
||||
inTypesLength := len(methodAndInOutTypes.InTypes)
|
||||
paramLength := len(requestObj.Parameters)
|
||||
if paramLength != inTypesLength {
|
||||
logUtil.ErrorLog("传入的参数数量不符,本地方法%s的参数数量:%d,传入的参数数量为:%d", key, inTypesLength, paramLength)
|
||||
return responseObj.SetResultStatus(ParamNotMatch)
|
||||
}
|
||||
|
||||
// 构造参数
|
||||
in := make([]reflect.Value, inTypesLength)
|
||||
for i := 0; i < inTypesLength; i++ {
|
||||
inTypeItem := methodAndInOutTypes.InTypes[i]
|
||||
paramItem := requestObj.Parameters[i]
|
||||
|
||||
// 已支持类型:Client,Player(非基本类型)
|
||||
// 已支持类型:Bool,Int,Int8,Int16,Int32,Int64,Uint,Uint8,Uint16,Uint32,Uint64,Float32,Float64,String
|
||||
// 已支持类型:以及上面所列出类型的Slice类型
|
||||
// 未支持类型:Uintptr,Complex64,Complex128,Array,Chan,Func,Interface,Map,Ptr,Struct,UnsafePointer
|
||||
// 由于byte与int8同义,rune与int32同义,所以并不需要单独处理
|
||||
// 枚举参数的类型,并进行类型转换
|
||||
switch inTypeItem.Kind() {
|
||||
case reflect.Interface:
|
||||
if param_client, ok := paramItem.(IClient); ok {
|
||||
in[i] = reflect.ValueOf(param_client)
|
||||
}
|
||||
case reflect.Ptr:
|
||||
if param_player, ok := paramItem.(*Player); ok {
|
||||
in[i] = reflect.ValueOf(param_player)
|
||||
}
|
||||
case reflect.Bool:
|
||||
if param_bool, ok := paramItem.(bool); ok {
|
||||
in[i] = reflect.ValueOf(param_bool)
|
||||
}
|
||||
case reflect.Int:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(int(param_float64))
|
||||
}
|
||||
case reflect.Int8:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(int8(param_float64))
|
||||
}
|
||||
case reflect.Int16:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(int16(param_float64))
|
||||
}
|
||||
case reflect.Int32:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(int32(param_float64))
|
||||
}
|
||||
case reflect.Int64:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(int64(param_float64))
|
||||
}
|
||||
case reflect.Uint:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(uint(param_float64))
|
||||
}
|
||||
case reflect.Uint8:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(uint8(param_float64))
|
||||
}
|
||||
case reflect.Uint16:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(uint16(param_float64))
|
||||
}
|
||||
case reflect.Uint32:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(uint32(param_float64))
|
||||
}
|
||||
case reflect.Uint64:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(uint64(param_float64))
|
||||
}
|
||||
case reflect.Float32:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(float32(param_float64))
|
||||
}
|
||||
case reflect.Float64:
|
||||
if param_float64, ok := paramItem.(float64); ok {
|
||||
in[i] = reflect.ValueOf(param_float64)
|
||||
}
|
||||
case reflect.String:
|
||||
if param_string, ok := paramItem.(string); ok {
|
||||
in[i] = reflect.ValueOf(param_string)
|
||||
}
|
||||
case reflect.Slice:
|
||||
// 如果是Slice类型,则需要对其中的项再次进行类型判断及类型转换
|
||||
if param_interface, ok := paramItem.([]interface{}); ok {
|
||||
switch inTypeItem.String() {
|
||||
case "[]bool":
|
||||
params_inner := make([]bool, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_bool, ok := param_interface[i].(bool); ok {
|
||||
params_inner[i] = param_bool
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]int":
|
||||
params_inner := make([]int, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = int(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]int8":
|
||||
params_inner := make([]int8, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = int8(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]int16":
|
||||
params_inner := make([]int16, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = int16(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]int32":
|
||||
params_inner := make([]int32, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = int32(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]int64":
|
||||
params_inner := make([]int64, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = int64(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]uint":
|
||||
params_inner := make([]uint, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = uint(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
// case "[]uint8": 特殊处理
|
||||
case "[]uint16":
|
||||
params_inner := make([]uint16, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = uint16(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]uint32":
|
||||
params_inner := make([]uint32, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = uint32(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]uint64":
|
||||
params_inner := make([]uint64, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = uint64(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]float32":
|
||||
params_inner := make([]float32, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = float32(param_float64)
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]float64":
|
||||
params_inner := make([]float64, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_float64, ok := param_interface[i].(float64); ok {
|
||||
params_inner[i] = param_float64
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
case "[]string":
|
||||
params_inner := make([]string, len(param_interface))
|
||||
for i := 0; i < len(param_interface); i++ {
|
||||
if param_string, ok := param_interface[i].(string); ok {
|
||||
params_inner[i] = param_string
|
||||
}
|
||||
}
|
||||
in[i] = reflect.ValueOf(params_inner)
|
||||
}
|
||||
} else if inTypeItem.String() == "[]uint8" { // 由于[]uint8在传输过程中会被转化成字符串,所以单独处理;
|
||||
if param_string, ok := paramItem.(string); ok {
|
||||
param_uint8 := ([]uint8)(param_string)
|
||||
in[i] = reflect.ValueOf(param_uint8)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否有无效的参数(传入的参数类型和方法定义的类型不匹配导致没有赋值)
|
||||
for _, item := range in {
|
||||
if reflect.Value.IsValid(item) == false {
|
||||
logUtil.ErrorLog("type:%v,value:%v.方法%s传入的参数%v无效", reflect.TypeOf(item), reflect.ValueOf(item), key, requestObj.Parameters)
|
||||
return responseObj.SetResultStatus(ParamInValid)
|
||||
}
|
||||
}
|
||||
|
||||
// 传入参数,调用方法
|
||||
out := methodAndInOutTypes.Method.Call(in)
|
||||
|
||||
// 由于只有一个返回值,所以取out[0]
|
||||
if retResponseObj, ok := (out[0]).Interface().(ServerResponseObject); ok {
|
||||
responseObj.SetMethodName(requestObj.MethodName)
|
||||
responseObj.SetResultStatus(retResponseObj.ResultStatus)
|
||||
responseObj.SetData(retResponseObj.Data)
|
||||
} else {
|
||||
logUtil.ErrorLog("(&out[0]).Interface()转换类型失败")
|
||||
}
|
||||
|
||||
return responseObj
|
||||
}
|
||||
Reference in New Issue
Block a user