upgrade-dev v2.3.15

This commit is contained in:
杨红岩 2023-03-19 01:31:30 +08:00
parent 13b1fa86e3
commit 529262f197
3 changed files with 106 additions and 58 deletions

View File

@ -8,6 +8,7 @@
//
//----------------------------------------
// ipc 通道 browser 进程(或服务端)
package ipc
import (
@ -16,10 +17,10 @@ import (
"github.com/energye/energy/logger"
"github.com/energye/energy/pkgs/json"
"net"
"os"
"sync"
)
// browserChannel browser进程
type browserChannel struct {
ipcType IPC_TYPE
unixAddr *net.UnixAddr
@ -30,28 +31,11 @@ type browserChannel struct {
handler IPCCallback
}
type channel struct {
IPCType IPC_TYPE
Conn net.Conn
}
func removeMemory() {
os.Remove(ipcSock)
}
func UseNetIPCChannel() bool {
return useNetIPCChannel
}
func MemoryAddress() string {
return memoryAddress
}
// NewBrowserChannel 主进程
func (m *ipcChannel) NewBrowserChannel(memoryAddresses ...string) *browserChannel {
// NewBrowser 创建主进程通道
func (m *ipcChannel) NewBrowser(memoryAddresses ...string) *browserChannel {
useNetIPCChannel = isUseNetIPC()
if useNetIPCChannel {
address := fmt.Sprintf("localhost:%d", IPCChannel.Port())
address := fmt.Sprintf("localhost:%d", Channel.Port())
listener, err := net.Listen("tcp", address)
if err != nil {
panic("Description Failed to create the IPC service Error: " + err.Error())
@ -82,10 +66,7 @@ func (m *ipcChannel) NewBrowserChannel(memoryAddresses ...string) *browserChanne
return m.browser
}
func (m *channel) conn() net.Conn {
return m.Conn
}
// Channel 返回指定通道链接
func (m *browserChannel) Channel(channelId int64) *channel {
if value, ok := m.channel.Load(channelId); ok {
return value.(*channel)
@ -93,6 +74,7 @@ func (m *browserChannel) Channel(channelId int64) *channel {
return nil
}
// ChannelIds 返回所有已链接通道ID
func (m *browserChannel) ChannelIds() (result []int64) {
m.channel.Range(func(key, value any) bool {
result = append(result, key.(int64))
@ -101,16 +83,22 @@ func (m *browserChannel) ChannelIds() (result []int64) {
return
}
func (m *browserChannel) putChannel(channelId int64, value *channel) {
m.channel.Store(channelId, value)
}
// Close 关闭通道链接
func (m *browserChannel) Close() {
if m.unixListener != nil {
m.unixListener.Close()
}
if m.netListener != nil {
m.netListener.Close()
}
}
// putChannel 添加一个通道链接
func (m *browserChannel) putChannel(channelId int64, value *channel) {
m.channel.Store(channelId, value)
}
// onConnect 建立链接
func (m *browserChannel) onConnect(context IIPCContext) {
logger.Info("IPC browser on connect key_channelId:", context.ChannelId())
if chn := m.Channel(context.ChannelId()); chn != nil {
@ -124,12 +112,13 @@ func (m *browserChannel) onConnect(context IIPCContext) {
}
}
// removeChannel 删除指定通道
func (m *browserChannel) removeChannel(id int64) {
logger.Debug("IPC browser channel remove key_channelId:", id)
m.channel.Delete(id)
}
// 单进程进程通道获取
// singleProcessChannelId 单进程进程通道获取
func (m *browserChannel) singleProcessChannelId() (int64, bool) {
if SingleProcess {
var channelId int64 = 0
@ -145,10 +134,12 @@ func (m *browserChannel) singleProcessChannelId() (int64, bool) {
return 0, false
}
// Send 指定通道发送数据
func (m *browserChannel) Send(channelId int64, data []byte) {
m.sendMessage(mt_common, channelId, data)
}
// Send 指定通道发送消息
func (m *browserChannel) sendMessage(messageType mt, channelId int64, data []byte) {
m.mutex.Lock()
defer m.mutex.Unlock()
@ -160,10 +151,12 @@ func (m *browserChannel) sendMessage(messageType mt, channelId int64, data []byt
}
}
// Handler 设置自定义处理回调函数
func (m *browserChannel) Handler(handler IPCCallback) {
m.handler = handler
}
// accept 接收链接的链接
func (m *browserChannel) accept() {
logger.Info("IPC Server Accept")
for {
@ -180,11 +173,12 @@ func (m *browserChannel) accept() {
logger.Info("browser channel accept Error:", err.Error())
continue
}
go m.ipcReadHandler(conn)
go m.readHandler(conn)
}
}
func (m *browserChannel) ipcReadHandler(conn net.Conn) {
// readHandler 读取数据
func (m *browserChannel) readHandler(conn net.Conn) {
defer func() {
if err := recover(); err != nil {
logger.Error("IPC Server Accept Recover:", err)

View File

@ -8,6 +8,7 @@
//
//----------------------------------------
// ipc 通道 render 进程(或客户端)
package ipc
import (
@ -19,6 +20,7 @@ import (
"sync"
)
// renderChannel 渲染进程
type renderChannel struct {
channelId int64
ipcType IPC_TYPE
@ -28,10 +30,13 @@ type renderChannel struct {
handler IPCCallback
}
func (m *ipcChannel) NewRenderChannel(channelId int64, memoryAddresses ...string) *renderChannel {
// NewRender 创建渲染进程通道
//
// channelId 唯一通道ID标识
func (m *ipcChannel) NewRender(channelId int64, memoryAddresses ...string) *renderChannel {
useNetIPCChannel = isUseNetIPC()
if useNetIPCChannel {
address := fmt.Sprintf("localhost:%d", IPCChannel.Port())
address := fmt.Sprintf("localhost:%d", Channel.Port())
conn, err := net.Dial("tcp", address)
if err != nil {
panic("Client failed to connect to IPC service Error: " + err.Error())
@ -61,6 +66,7 @@ func (m *ipcChannel) NewRenderChannel(channelId int64, memoryAddresses ...string
return m.render
}
// onConnection 建立链接
func (m *renderChannel) onConnection() {
message := json.NewJSONObject(nil)
message.Set(key_channelId, m.channelId)
@ -68,20 +74,24 @@ func (m *renderChannel) onConnection() {
message.Free()
}
// Send 发送数据
func (m *renderChannel) Send(data []byte) {
m.sendMessage(mt_common, data)
}
// sendMessage 发送消息
func (m *renderChannel) sendMessage(messageType mt, data []byte) {
m.mutex.Lock()
defer m.mutex.Unlock()
_, _ = ipcWrite(messageType, m.channelId, data, m.conn())
}
// Handler 设置自定义处理回调函数
func (m *renderChannel) Handler(handler IPCCallback) {
m.handler = handler
}
// Close 关闭通道链接
func (m *renderChannel) Close() {
if m.connect != nil {
m.connect.Close()
@ -89,10 +99,12 @@ func (m *renderChannel) Close() {
}
}
// conn 返回通道链接
func (m *renderChannel) conn() net.Conn {
return m.connect
}
// receive 接收数据
func (m *renderChannel) receive() {
defer func() {
if err := recover(); err != nil {

View File

@ -37,9 +37,9 @@ var (
var (
memoryAddress = "energy.sock"
useNetIPCChannel = false
ipcSock string
IPCChannel = &ipcChannel{
useNetIPCChannel = false
Channel = &ipcChannel{
browser: &browserChannel{
channel: sync.Map{},
mutex: sync.Mutex{},
@ -55,14 +55,17 @@ var (
type mt int8
const (
mt_connection mt = iota //建立链接消息
mt_common //普通消息
mt_invalid mt = iota - 1 //无效类型
mt_connection //建立链接消息
mt_common //普通消息
)
const (
key_channelId = "key_channelId"
)
type IPCCallback func(context IIPCContext)
func init() {
ipcSock = filepath.Join(os.TempDir(), memoryAddress)
}
@ -73,6 +76,40 @@ type ipcChannel struct {
render *renderChannel
}
type channel struct {
IPCType IPC_TYPE
Conn net.Conn
}
func removeMemory() {
os.Remove(ipcSock)
}
func UseNetIPCChannel() bool {
return useNetIPCChannel
}
func MemoryAddress() string {
return memoryAddress
}
func isUseNetIPC() bool {
if common.IsDarwin() || common.IsLinux() {
return false
}
ov := version.OSVersion
if (ov.Major > 10) || (ov.Major == 10 && ov.Build >= 17063) {
return false
}
return true
}
// conn 返回通道链接
func (m *channel) conn() net.Conn {
return m.Conn
}
// Port 获取并返回net socket端口
func (m *ipcChannel) Port() int {
if m.port != 0 {
return m.port
@ -93,43 +130,32 @@ func (m *ipcChannel) Port() int {
return m.port
}
func isUseNetIPC() bool {
if common.IsDarwin() || common.IsLinux() {
return false
}
ov := version.OSVersion
if (ov.Major > 10) || (ov.Major == 10 && ov.Build >= 17063) {
return false
}
return true
}
// Browser 返回 browser 通道
func (m *ipcChannel) Browser() *browserChannel {
return m.browser
}
// Render 返回 render 通道
func (m *ipcChannel) Render() *renderChannel {
return m.render
}
// 主进程事件emit
// IIPCChannel browser
type IIPCChannel interface {
Close()
Channel(channelId int64) *channel //IPC 获取指定的通道
ChannelIds() (result []int64) //IPC 获取所有通道
}
type IPCCallback func(context IIPCContext)
type messageCallback func(context IMessage)
// 进程间IPC通信回调上下文
// IIPCContext IPC通信回调上下文
type IIPCContext interface {
Connect() net.Conn //IPC 链接
ChannelId() int64 //render channel key_channelId
Message() IMessage //
Connect() net.Conn // IPC 通道链接
ChannelId() int64 // 通道ID
Message() IMessage // 消息
Free() //
}
// IMessage 消息内容接口
type IMessage interface {
Type() mt
Length() uint32
@ -138,6 +164,7 @@ type IMessage interface {
clear()
}
// ipcReadHandler ipc 消息读取处理
type ipcReadHandler struct {
ipcType IPC_TYPE
ct ChannelType
@ -145,13 +172,14 @@ type ipcReadHandler struct {
handler IPCCallback
}
// ipcMessage 消息内容
type ipcMessage struct {
t mt
s uint32
v []byte
}
// IPC 上下文
// IPCContext IPC 上下文
type IPCContext struct {
channelId int64 //render channelId
ipcType IPC_TYPE //
@ -159,6 +187,7 @@ type IPCContext struct {
message IMessage //
}
// Close 关闭当前ipc通道链接
func (m *ipcReadHandler) Close() {
if m.connect != nil {
m.connect.Close()
@ -166,6 +195,7 @@ func (m *ipcReadHandler) Close() {
}
}
// Read 读取内容
func (m *ipcReadHandler) Read(b []byte) (n int, err error) {
if m.ipcType == IPCT_NET {
return m.connect.Read(b)
@ -175,6 +205,7 @@ func (m *ipcReadHandler) Read(b []byte) (n int, err error) {
}
}
// Free 释放消息内存空间
func (m *IPCContext) Free() {
if m.message != nil {
m.message.clear()
@ -182,39 +213,49 @@ func (m *IPCContext) Free() {
}
}
// ChannelId 返回通道ID
func (m *IPCContext) ChannelId() int64 {
return m.channelId
}
// Message 返回消息内容
func (m *IPCContext) Message() IMessage {
return m.message
}
// Connect 返回当前通道链接
func (m *IPCContext) Connect() net.Conn {
return m.connect
}
// Type 消息类型
func (m *ipcMessage) Type() mt {
return m.t
}
// Data 消息[]byte数据
func (m *ipcMessage) Data() []byte {
return m.v
}
// Length 消息[]byte长度
func (m *ipcMessage) Length() uint32 {
return m.s
}
// JSON 消息转为JSON对象
func (m *ipcMessage) JSON() json.JSON {
return json.NewJSON(m.v)
}
// clear 清空内容
func (m *ipcMessage) clear() {
m.t = mt_invalid
m.v = nil
m.s = 0
}
// ipcWrite 写入消息
func ipcWrite(messageType mt, channelId int64, data []byte, conn net.Conn) (n int, err error) {
defer func() {
data = nil
@ -238,6 +279,7 @@ func ipcWrite(messageType mt, channelId int64, data []byte, conn net.Conn) (n in
return n, err
}
// ipcRead 读取消息
func ipcRead(handler *ipcReadHandler) {
var ipcType, chnType string
if handler.ipcType == IPCT_NET {