energy/ipc/channel/ipc-channel-render.go

125 lines
3.3 KiB
Go
Raw Normal View History

2023-03-19 00:00:09 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
2023-03-19 01:31:30 +08:00
// ipc 通道 render 进程(或客户端)
2023-03-20 10:09:54 +08:00
package channel
2023-03-19 00:00:09 +08:00
import (
2023-03-19 15:25:46 +08:00
"bytes"
2023-03-19 00:00:09 +08:00
"fmt"
. "github.com/energye/energy/consts"
"github.com/energye/energy/logger"
"net"
"sync"
)
2023-03-19 01:31:30 +08:00
// renderChannel 渲染进程
2023-03-19 00:00:09 +08:00
type renderChannel struct {
2023-03-19 22:40:10 +08:00
channel *channel
mutex sync.Mutex
handler IPCCallback
2023-03-19 00:00:09 +08:00
}
2023-03-19 01:31:30 +08:00
// NewRender 创建渲染进程通道
//
2023-03-19 21:44:39 +08:00
// 参数: channelId 唯一通道ID标识
2023-03-20 10:09:54 +08:00
func NewRender(channelId int64, memoryAddresses ...string) IRenderChannel {
2023-03-19 00:39:59 +08:00
useNetIPCChannel = isUseNetIPC()
2023-03-19 00:00:09 +08:00
if useNetIPCChannel {
2023-03-20 10:09:54 +08:00
address := fmt.Sprintf("localhost:%d", Port())
2023-03-19 00:00:09 +08:00
conn, err := net.Dial("tcp", address)
if err != nil {
2023-03-19 21:44:39 +08:00
panic("Client failed to channel to IPC service Error: " + err.Error())
2023-03-19 00:00:09 +08:00
}
2023-03-20 10:09:54 +08:00
render.channel = &channel{writeBuf: new(bytes.Buffer), conn: conn, channelId: channelId, ipcType: IPCT_NET, channelType: Ct_Client}
2023-03-19 00:00:09 +08:00
} else {
memoryAddr := ipcSock
logger.Debug("new render channel for IPC Sock", memoryAddr)
if len(memoryAddresses) > 0 {
memoryAddr = memoryAddresses[0]
}
unixAddr, err := net.ResolveUnixAddr(MemoryNetwork, memoryAddr)
if err != nil {
2023-03-19 21:44:39 +08:00
panic("Client failed to channel to IPC service Error: " + err.Error())
2023-03-19 00:00:09 +08:00
}
unixConn, err := net.DialUnix(MemoryNetwork, nil, unixAddr)
if err != nil {
2023-03-19 21:44:39 +08:00
panic("Client failed to channel to IPC service Error: " + err.Error())
2023-03-19 00:00:09 +08:00
}
2023-03-20 10:09:54 +08:00
render.channel = &channel{writeBuf: new(bytes.Buffer), conn: unixConn, channelId: channelId, ipcType: IPCT_UNIX, channelType: Ct_Client}
2023-03-19 00:00:09 +08:00
}
2023-03-20 10:09:54 +08:00
go render.receive()
render.onChannelConnect()
return render
2023-03-19 00:00:09 +08:00
}
2023-03-19 22:40:10 +08:00
func (m *renderChannel) Channel() IChannel {
return m.channel
}
2023-03-20 09:23:27 +08:00
// onChannelConnect 建立通道链接
func (m *renderChannel) onChannelConnect() {
2023-03-20 09:11:57 +08:00
m.sendMessage(mt_connection, m.channel.channelId, m.channel.channelId, []byte{uint8(mt_connection)})
2023-03-19 00:00:09 +08:00
}
2023-03-19 01:31:30 +08:00
// Send 发送数据
2023-03-19 00:00:09 +08:00
func (m *renderChannel) Send(data []byte) {
2023-03-19 22:40:10 +08:00
if m.channel != nil && m.channel.IsConnect() {
2023-03-20 09:11:57 +08:00
m.sendMessage(mt_common, m.channel.channelId, m.channel.channelId, data)
}
}
// SendToChannel 发送到指定通道
func (m *renderChannel) SendToChannel(toChannelId int64, data []byte) {
if m.channel != nil && m.channel.IsConnect() {
m.sendMessage(mt_relay, m.channel.channelId, toChannelId, data)
2023-03-19 22:40:10 +08:00
}
2023-03-19 00:00:09 +08:00
}
2023-03-19 01:31:30 +08:00
// sendMessage 发送消息
2023-03-20 09:11:57 +08:00
func (m *renderChannel) sendMessage(messageType mt, channelId, toChannelId int64, data []byte) {
_, _ = m.channel.write(messageType, channelId, toChannelId, data)
2023-03-19 00:00:09 +08:00
}
2023-03-19 01:31:30 +08:00
// Handler 设置自定义处理回调函数
2023-03-19 00:00:09 +08:00
func (m *renderChannel) Handler(handler IPCCallback) {
m.handler = handler
}
2023-03-19 01:31:30 +08:00
// Close 关闭通道链接
2023-03-19 00:00:09 +08:00
func (m *renderChannel) Close() {
2023-03-19 22:40:10 +08:00
if m.channel != nil {
m.channel.Close()
m.channel = nil
2023-03-19 00:00:09 +08:00
}
}
2023-03-19 01:31:30 +08:00
// receive 接收数据
2023-03-19 00:00:09 +08:00
func (m *renderChannel) receive() {
defer func() {
if err := recover(); err != nil {
logger.Error("IPC Render Channel Recover:", err)
}
2023-03-19 22:40:10 +08:00
fmt.Println("close")
m.channel.isConnect = false
2023-03-19 00:00:09 +08:00
m.Close()
}()
2023-03-19 22:40:10 +08:00
m.channel.handler = func(context IIPCContext) {
if context.Message().Type() == mt_connectd {
m.channel.isConnect = true
} else {
if m.handler != nil {
m.handler(context)
}
2023-03-19 15:25:46 +08:00
}
2023-03-19 00:00:09 +08:00
}
2023-03-19 22:40:10 +08:00
m.channel.ipcRead()
2023-03-19 00:00:09 +08:00
}