This commit is contained in:
azw 2023-08-13 14:10:13 +08:00
parent 1db5053277
commit af8b118178
6 changed files with 250 additions and 96 deletions

15
app.go
View File

@ -17,7 +17,7 @@ type App interface {
GetZoneId() int32
HasFlag(int32) bool
AddIMMsg(uint16, q5.Args)
RegisterIMMsgHandle(uint16, func(uint16,q5.Args))
RegisterIMMsgHandle(uint16, func(q5.Args))
NotifyLoopCond()
NowUnix() int64
NowUnixMilli() int64
@ -35,7 +35,6 @@ type app struct {
nodeId int32
instanceId int32
terminated bool
pkgName string
flags map[int32]int32
servicing bool
contextHash map[int32]q5.Args
@ -48,7 +47,7 @@ type app struct {
chGoLoopWait chan int64
nowTime time.Time
nowUnixNano int64
imMsgHandlers [1024]func(uint16, q5.Args)
imMsgHandlers [1024]func(q5.Args)
maxRunDelay int64
maxScheduleTime int64
userApp UserApp
@ -127,11 +126,7 @@ func (this *app) GetZoneId() int32 {
}
func (this *app) GetPkgName() string {
return this.pkgName
}
func (this *app) setPkgName(pkgName string) {
this.pkgName = pkgName
return this.userApp.GetPkgName()
}
func (this *app) HasFlag(flag int32) bool {
@ -157,7 +152,7 @@ func (this *app) AddIMMsg(msgId uint16, params q5.Args) {
this.loopCond.Broadcast()
}
func (this *app) RegisterIMMsgHandle(msgId uint16, handle func(uint16,q5.Args)) {
func (this *app) RegisterIMMsgHandle(msgId uint16, handle func(q5.Args)) {
this.imMsgHandlers[msgId] = handle
}
@ -215,7 +210,7 @@ func (this *app) dispatchIMMsg() {
currNode := this.imWorkNode
this.imWorkNode = this.imWorkNode.next
if this.imMsgHandlers[currNode.msgId] != nil {
this.imMsgHandlers[currNode.msgId](currNode.msgId, currNode.params)
this.imMsgHandlers[currNode.msgId](currNode.params)
}
}
}

5
constant.go Normal file
View File

@ -0,0 +1,5 @@
package f5
const (
IM_HTTP_CLI_MGR_RESPONSE = 1
)

View File

@ -4,6 +4,11 @@ var _app *app
var _timer *Timer
var _sysLog *SysLog_
var _tgLog *TGLog_
var _httpCliMgr *HttpCliMgr
func GetApp() App {
return _app
}
func GetTimer() *Timer {
return _timer
@ -17,8 +22,8 @@ func GetTgLog() *TGLog_ {
return _tgLog
}
func GetApp() App {
return _app
func GetHttpCliMgr() *HttpCliMgr {
return _httpCliMgr
}
func Run(userApp UserApp) {

144
httpclimgr.go Normal file
View File

@ -0,0 +1,144 @@
package f5
import (
"q5"
)
/*
http的请求分为两种风格
go like: go风格回调的时候不会join主线程被回调方需要处理线程同步问题
js like: js风格回调时候会自动join主线程被回调方无需处理线程同步问题
*/
const (
GO_LIKE_REQUEST = 1
JS_LIKE_REQUEST = 2
NORMAL_CHANNEL = 1
QUICK_CHANNEL = 2
)
type HttpCliResponse interface {
GetErr() error
GetJsonData() map[string]interface{}
GetRawData() string
IsTimeOut() bool
}
type httpCliResponse struct {
err error
data map[string]interface{}
rawData string
isTimeOut bool
}
type HttpCliMgr struct {
}
func (this *HttpCliMgr) init() {
_app.RegisterIMMsgHandle(
IM_HTTP_CLI_MGR_RESPONSE,
func (args q5.Args) {
cb := args[0].(func (HttpCliResponse))
rsp := args[1].(*httpCliResponse)
cb(rsp)
})
}
func (this *HttpCliMgr) unInit() {
}
func (this *HttpCliMgr) SendGoLikeRequest(
url string, params map[string]string,
cb func (HttpCliResponse)) {
this.internalSendRequest(
GO_LIKE_REQUEST,
NORMAL_CHANNEL,
url,
params,
cb)
}
func (this *HttpCliMgr) SendJsLikeRequest(
url string, params map[string]string,
cb func (HttpCliResponse)) {
this.internalSendRequest(
JS_LIKE_REQUEST,
NORMAL_CHANNEL,
url,
params,
cb)
}
func (this *HttpCliMgr) SendQuickChannelGoLikeRequest(
url string, params map[string]string,
cb func (HttpCliResponse)) {
this.internalSendRequest(
GO_LIKE_REQUEST,
QUICK_CHANNEL,
url,
params,
cb)
}
func (this *HttpCliMgr) SendQuickChannelJsLikeRequest(
url string, params map[string]string,
cb func (HttpCliResponse)) {
this.internalSendRequest(
JS_LIKE_REQUEST,
QUICK_CHANNEL,
url,
params,
cb)
}
func (this *HttpCliMgr) internalSendRequest(
style int32, channel int32,
url string, params map[string]string,
cb func (HttpCliResponse)) {
if !(style == GO_LIKE_REQUEST || style == JS_LIKE_REQUEST) {
panic("HttpCliMgr sytel error")
}
if !(channel == NORMAL_CHANNEL || channel == QUICK_CHANNEL) {
panic("HttpCliMgr channel error")
}
doFunc := func() {
data, err := q5.HttpGet(url, params)
if cb == nil {
return
}
rsp := new(httpCliResponse)
rsp.init(data, err)
if style == GO_LIKE_REQUEST {
cb(rsp)
} else {
_app.AddIMMsg(IM_HTTP_CLI_MGR_RESPONSE, []interface{} {
cb,
rsp,
})
}
}
go doFunc()
}
func (this *httpCliResponse) init(data string, err error) {
this.err = err
this.rawData = data
}
func (this *httpCliResponse) GetErr() error {
return this.err
}
func (this *httpCliResponse) GetJsonData() map[string]interface{} {
return this.data
}
func (this *httpCliResponse) GetRawData() string {
return this.rawData
}
func (this *httpCliResponse) IsTimeOut() bool {
return this.isTimeOut
}

89
prototils.go Normal file
View File

@ -0,0 +1,89 @@
package f5
import (
"q5"
"net"
)
type MsgHdr struct {
MsgId uint16
SeqId uint32
SocketHandle uint16
IpSaddr uint32
Conn net.Conn
Data []byte
Msg interface{}
Entry q5.ListHead
}
type NetMsg interface {
GetNetMsgId() uint16
}
/*
struct WSProxyPackHead_C
{
unsigned short packlen;
unsigned short msgid;
unsigned int seqid;
unsigned short magic_code;
unsigned short socket_handle;
unsigned long ip_saddr;
};
*/
type WSProxyPackHead_C struct {
PackLen uint16
MsgId uint16
SeqId uint32
MagicCode uint16
SocketHandle uint16
IpSaddr uint64
}
/*
struct WSProxyPackHead_S
{
unsigned short packlen;
unsigned short msgid;
unsigned int seqid;
unsigned short magic_code;
unsigned short rpc_error_code;
unsigned short socket_handle;
unsigned short ext_len;
};
*/
type WSProxyPackHead_S struct {
PackLen uint16
MsgId uint16
SeqId uint32
MagicCode uint16
RpcErrCode uint16
SocketHandle uint16
ExtLen uint16
}
func (this *WSProxyPackHead_C) Read(data []byte, offset int) {
this.PackLen = q5.MkUInt16(data[offset + 0], data[offset + 1])
this.MsgId = q5.MkUInt16(data[offset + 2], data[offset + 3])
this.SeqId = q5.MkUInt32(data[offset + 4], data[offset + 5],
data[offset + 6], data[offset + 7])
this.MagicCode = q5.MkUInt16(data[offset + 8], data[offset + 9])
this.SocketHandle = q5.MkUInt16(data[offset + 10], data[offset + 11])
}
func (this *WSProxyPackHead_S) Write(data []byte, offset int) {
data[offset + 0] = byte(this.PackLen & 0xFF)
data[offset + 1] = byte(this.PackLen >> 8)
data[offset + 2] = byte(this.MsgId & 0xFF)
data[offset + 3] = byte(this.MsgId >> 8)
data[offset + 8] = byte('K')
data[offset + 9] = byte('S')
data[offset + 12] = byte(this.SocketHandle & 0xFF)
data[offset + 13] = byte(this.SocketHandle >> 8)
}

View File

@ -2,7 +2,6 @@ package f5
import (
"q5"
"net"
)
const (
@ -28,86 +27,3 @@ type middleware struct {
handlerFunc HandlerFunc
entry q5.ListHead
}
type MsgHdr struct {
MsgId uint16
SeqId uint32
SocketHandle uint16
IpSaddr uint32
Conn net.Conn
Data []byte
Msg interface{}
Entry q5.ListHead
}
type NetMsg interface {
GetNetMsgId() uint16
}
/*
struct WSProxyPackHead_C
{
unsigned short packlen;
unsigned short msgid;
unsigned int seqid;
unsigned short magic_code;
unsigned short socket_handle;
unsigned long ip_saddr;
};
*/
type WSProxyPackHead_C struct {
PackLen uint16
MsgId uint16
SeqId uint32
MagicCode uint16
SocketHandle uint16
IpSaddr uint64
}
/*
struct WSProxyPackHead_S
{
unsigned short packlen;
unsigned short msgid;
unsigned int seqid;
unsigned short magic_code;
unsigned short rpc_error_code;
unsigned short socket_handle;
unsigned short ext_len;
};
*/
type WSProxyPackHead_S struct {
PackLen uint16
MsgId uint16
SeqId uint32
MagicCode uint16
RpcErrCode uint16
SocketHandle uint16
ExtLen uint16
}
func (this *WSProxyPackHead_C) Read(data []byte, offset int) {
this.PackLen = q5.MkUInt16(data[offset + 0], data[offset + 1])
this.MsgId = q5.MkUInt16(data[offset + 2], data[offset + 3])
this.SeqId = q5.MkUInt32(data[offset + 4], data[offset + 5],
data[offset + 6], data[offset + 7])
this.MagicCode = q5.MkUInt16(data[offset + 8], data[offset + 9])
this.SocketHandle = q5.MkUInt16(data[offset + 10], data[offset + 11])
}
func (this *WSProxyPackHead_S) Write(data []byte, offset int) {
data[offset + 0] = byte(this.PackLen & 0xFF)
data[offset + 1] = byte(this.PackLen >> 8)
data[offset + 2] = byte(this.MsgId & 0xFF)
data[offset + 3] = byte(this.MsgId >> 8)
data[offset + 8] = byte('K')
data[offset + 9] = byte('S')
data[offset + 12] = byte(this.SocketHandle & 0xFF)
data[offset + 13] = byte(this.SocketHandle >> 8)
}