upgrade-dev v2.3.26

This commit is contained in:
杨红岩 2023-03-23 16:03:20 +08:00
parent 417bdd3c06
commit 0d736b0b68
4 changed files with 38 additions and 41 deletions

View File

@ -11,7 +11,10 @@
// GO browser IPC通道
package cef
import "github.com/energye/energy/pkgs/channel"
import (
"fmt"
"github.com/energye/energy/pkgs/channel"
)
// browserIPCChan
type browserIPCChan struct {
@ -23,6 +26,7 @@ func (m *ipcBrowserProcess) ipcChannelBrowser() {
m.ipcChannel = new(browserIPCChan)
m.ipcChannel.ipc = channel.NewBrowser()
m.ipcChannel.ipc.Handler(func(context channel.IIPCContext) {
fmt.Println("data:", context.Message().JSON().ToJSONString())
context.Free()
})
}

View File

@ -15,7 +15,6 @@ import (
"fmt"
"github.com/energye/energy/consts"
"github.com/energye/energy/pkgs/json"
"time"
)
// ipcRenderProcess 渲染进程
@ -257,7 +256,7 @@ func (m *ipcRenderProcess) jsExecuteGoEvent(name string, object *ICefV8Value, ar
} else { //多进程
// 同步
if isSync {
callback := &ipcCallback{isSync: true, result: make(chan []byte)}
callback := &ipcCallback{isSync: true}
if emitCallback != nil {
callback.resultType = rt_function
callback.function = emitCallback
@ -265,7 +264,7 @@ func (m *ipcRenderProcess) jsExecuteGoEvent(name string, object *ICefV8Value, ar
callback.resultType = rt_variable
}
m.emitHandler.addCallback(callback)
m.multiProcessSync(callback)
m.multiProcessSync(callback, isSync, emitNameValue, args)
if callback.resultType == rt_variable {
if callback.variable != nil {
retVal.SetResult(callback.variable)
@ -317,23 +316,18 @@ func (m *ipcRenderProcess) singleProcess(emitName string, callback *ipcCallback,
}
// multiProcessSync 多进程同步消息
func (m *ipcRenderProcess) multiProcessSync(callback *ipcCallback) {
var isClose = false
var closeResultChan = func() {
if !isClose {
close(callback.result)
isClose = true
}
}
fmt.Println("等待结果 1")
delayedWait := time.AfterFunc(2*time.Second, closeResultChan)
fmt.Println("等待结果 2")
data, isOpen := <-callback.result
fmt.Println("结果 isOpen:", isOpen, "data:", string(data))
if isOpen {
delayedWait.Stop()
closeResultChan()
func (m *ipcRenderProcess) multiProcessSync(callback *ipcCallback, isSync bool, emitName string, data []byte) {
fmt.Println("data", string(data))
message := json.NewJSONObject(nil)
message.Set(ipc_id, 1)
message.Set(ipc_type, isSync)
message.Set(ipc_event, emitName)
if data != nil {
message.Set(ipc_argumentList, json.NewJSONArray(data).Data())
} else {
message.Set(ipc_argumentList, nil)
}
m.ipcChannel.ipc.Send(message.Bytes())
}
// multiProcessAsync 多进程异步消息
@ -401,13 +395,11 @@ func (m *ipcRenderProcess) ipcJSExecuteGoEventMessageReply(browser *ICefBrowser,
}()
//[]byte
returnArgs = argumentList.GetArrayByIndex(2)
if callback.isSync {
if callback.isSync { // 同步
if returnArgs != nil {
callback.result <- returnArgs.Bytes()
} else {
callback.result <- nil
}
} else {
} else { //异步
if callback.function != nil {
//设置允许释放
callback.function.SetCanNotFree(false)

View File

@ -55,7 +55,7 @@ var (
ipcBrowser *ipcBrowserProcess // 主进程 IPC
)
// ipcEmitHandler
// ipcEmitHandler ipc.emit 处理器
type ipcEmitHandler struct {
handler *ICefV8Handler // ipc.emit handler
handlerSync *ICefV8Handler // ipc.emitSync handler
@ -64,24 +64,22 @@ type ipcEmitHandler struct {
callbackLock sync.Mutex // ipc.emit lock
}
// ipcOnHandler
// ipcOnHandler ipc.on 处理器
type ipcOnHandler struct {
handler *ICefV8Handler // ipc.on handler
callbackList map[string]*ipcCallback // ipc.on callbackList
callbackMessageId int32 // ipc.on messageId
callbackLock sync.Mutex // ipc.emit lock
handler *ICefV8Handler // ipc.on handler
callbackList map[string]*ipcCallback // ipc.on callbackList
callbackLock sync.Mutex // ipc.emit lock
}
// ipcCallback
// ipcCallback ipc.emit 回调结果
type ipcCallback struct {
isSync bool //是否异步
result chan []byte //
resultType result_type //返回值类型 0:function 1:variable 默认:0
variable *ICefV8Value //回调函数, 根据 resultType
function *ICefV8Value //回调函数, 根据 resultType
}
// isIPCInternalKey IPC 内部 key 不允许使用
// isIPCInternalKey IPC 内部定义使用 key 不允许使用
func isIPCInternalKey(key string) bool {
return key == internalIPC || key == internalIPCEmit || key == internalIPCOn || key == internalIPCEmitSync ||
key == internalIPCJSExecuteGoEvent || key == internalIPCJSExecuteGoEventReplay ||
@ -120,7 +118,7 @@ func (m *ipcEmitHandler) addCallback(callback *ipcCallback) int32 {
return m.callbackMessageId
}
// 获取下一个消息ID
// nextMessageId 获取下一个消息ID
func (m *ipcEmitHandler) nextMessageId() int32 {
m.callbackMessageId++
if m.callbackMessageId == -1 {
@ -129,7 +127,7 @@ func (m *ipcEmitHandler) nextMessageId() int32 {
return m.callbackMessageId
}
// getCallback
// getCallback 返回回调函数
func (m *ipcEmitHandler) getCallback(messageId int32) *ipcCallback {
//return (*list.Element)(unsafe.Pointer(ptr)).Value.(*ipcCallback)
m.callbackLock.Lock()
@ -141,23 +139,24 @@ func (m *ipcEmitHandler) getCallback(messageId int32) *ipcCallback {
return nil
}
//clear 清空所有回调函数
func (m *ipcEmitHandler) clear() {
for _, v := range m.callbackList {
v.free()
}
m.callbackMessageId = 0
m.callbackList = make(map[int32]*ipcCallback)
}
// addCallback
func (m *ipcOnHandler) addCallback(eventName string, callback *ipcCallback) int32 {
// addCallback 根据事件名添加回调函数
func (m *ipcOnHandler) addCallback(eventName string, callback *ipcCallback) {
//return uintptr(unsafe.Pointer(m.callbackList.PushBack(callback)))
m.callbackLock.Lock()
defer m.callbackLock.Unlock()
m.callbackList[eventName] = callback
return m.callbackMessageId
}
// removeCallback
// removeCallback 根据事件名移除回调函数
func (m *ipcOnHandler) removeCallback(eventName string) {
//m.callbackList.Remove((*list.Element)(unsafe.Pointer(ptr)))
m.callbackLock.Lock()
@ -165,7 +164,7 @@ func (m *ipcOnHandler) removeCallback(eventName string) {
delete(m.callbackList, eventName)
}
// getCallback
// getCallback 根据事件名返回回调函数
func (m *ipcOnHandler) getCallback(eventName string) *ipcCallback {
//return (*list.Element)(unsafe.Pointer(ptr)).Value.(*ipcCallback)
m.callbackLock.Lock()
@ -173,6 +172,7 @@ func (m *ipcOnHandler) getCallback(eventName string) *ipcCallback {
return m.callbackList[eventName]
}
//clear 清空所有回调函数
func (m *ipcOnHandler) clear() {
for _, v := range m.callbackList {
v.free()
@ -180,6 +180,7 @@ func (m *ipcOnHandler) clear() {
m.callbackList = make(map[string]*ipcCallback)
}
//free 清空所有回调函数
func (m *ipcCallback) free() {
if m.function != nil {
m.function.Free()

View File

@ -284,7 +284,7 @@
function testEmitSync() {
ipc.emitSync("testEmitSync")
ipc.emitSync("testEmitSync",["同步参数",1,2,3,["aaaa","bbb",6666]])
}