From af8b11817827dfcb4deb7fe4f8c1b5bc4edb07d0 Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 13 Aug 2023 14:10:13 +0800 Subject: [PATCH] 1 --- app.go | 15 ++---- constant.go | 5 ++ export.go | 9 +++- httpclimgr.go | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ prototils.go | 89 +++++++++++++++++++++++++++++++ types.go | 84 ----------------------------- 6 files changed, 250 insertions(+), 96 deletions(-) create mode 100644 constant.go create mode 100644 httpclimgr.go create mode 100644 prototils.go diff --git a/app.go b/app.go index 50ef174..1656ed2 100644 --- a/app.go +++ b/app.go @@ -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) } } } diff --git a/constant.go b/constant.go new file mode 100644 index 0000000..cc9af9a --- /dev/null +++ b/constant.go @@ -0,0 +1,5 @@ +package f5 + +const ( + IM_HTTP_CLI_MGR_RESPONSE = 1 +) diff --git a/export.go b/export.go index 2b8335a..b5d134c 100644 --- a/export.go +++ b/export.go @@ -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) { diff --git a/httpclimgr.go b/httpclimgr.go new file mode 100644 index 0000000..13a175a --- /dev/null +++ b/httpclimgr.go @@ -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 +} diff --git a/prototils.go b/prototils.go new file mode 100644 index 0000000..eb700ba --- /dev/null +++ b/prototils.go @@ -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) +} diff --git a/types.go b/types.go index db3d13e..0df2727 100644 --- a/types.go +++ b/types.go @@ -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) -}