energy/pkgs/channel/channel-render.go

137 lines
3.5 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-05-31 17:41:14 +08:00
// ipc Channel render process (or client)
2023-03-20 10:09:54 +08:00
package channel
2023-03-19 00:00:09 +08:00
import (
"fmt"
2023-05-31 18:00:34 +08:00
. "github.com/energye/energy/v2/consts"
"github.com/energye/energy/v2/logger"
2023-03-19 00:00:09 +08:00
"net"
)
2023-05-31 17:41:14 +08:00
// renderChannel renderer process
2023-03-19 00:00:09 +08:00
type renderChannel struct {
2023-03-19 22:40:10 +08:00
channel *channel
handler IPCCallback
2023-03-19 00:00:09 +08:00
}
2023-05-31 17:41:14 +08:00
// NewRender Create the renderer process channel
2023-03-19 01:31:30 +08:00
//
2023-05-31 17:41:14 +08:00
// param: channelId Unique channel ID identifier
func NewRender(channelId int64, addresses ...string) IRenderChannel {
2023-03-19 00:39:59 +08:00
useNetIPCChannel = isUseNetIPC()
2023-05-31 17:41:14 +08:00
render := &renderChannel{}
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-05-31 17:41:14 +08:00
panic("NewRender IPC channel Error: " + err.Error())
2023-03-19 00:00:09 +08:00
}
2023-03-20 15:00:49 +08:00
render.channel = &channel{conn: conn, channelId: channelId, ipcType: IPCT_NET, channelType: Ct_Client}
2023-03-19 00:00:09 +08:00
} else {
2023-05-31 17:41:14 +08:00
if len(addresses) > 0 {
ipcSock = addresses[0]
2023-03-19 00:00:09 +08:00
}
2023-03-23 11:07:24 +08:00
logger.Debug("new render channel for IPC Sock", ipcSock)
unixAddr, err := net.ResolveUnixAddr(MemoryNetwork, ipcSock)
2023-03-19 00:00:09 +08:00
if err != nil {
2023-05-31 17:41:14 +08:00
panic("NewRender IPC channel Error: " + err.Error())
2023-03-19 00:00:09 +08:00
}
2023-05-31 17:41:14 +08:00
conn, err := net.DialUnix(MemoryNetwork, nil, unixAddr)
2023-03-19 00:00:09 +08:00
if err != nil {
2023-05-31 17:41:14 +08:00
panic("NewRender IPC channel Error: " + err.Error())
2023-03-19 00:00:09 +08:00
}
2023-05-31 17:41:14 +08:00
render.channel = &channel{conn: conn, 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-05-31 17:41:14 +08:00
// Channel Return to current channel
2023-03-19 22:40:10 +08:00
func (m *renderChannel) Channel() IChannel {
return m.channel
}
2023-05-31 17:41:14 +08:00
// onChannelConnect Establishing channel connection
2023-03-20 09:23:27 +08:00
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-05-31 17:41:14 +08:00
// Send data
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)
}
}
2023-05-31 17:41:14 +08:00
// SendToChannel Send to specified channel
2023-03-20 09:11:57 +08:00
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-05-31 17:41:14 +08:00
// UpdateChannelId
// Update channel ID
// The original channel ID is invalid after updating
func (m *renderChannel) UpdateChannelId(newChannelId int64) {
if m.channel.channelId != newChannelId {
m.sendMessage(mt_update_channel_id, m.channel.channelId, newChannelId, []byte{uint8(mt_update_channel_id)})
m.channel.channelId = newChannelId
}
}
// sendMessage
// Send data to the specified channel
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-05-31 17:41:14 +08:00
// Handler
// Set custom processing callback function
2023-03-19 00:00:09 +08:00
func (m *renderChannel) Handler(handler IPCCallback) {
m.handler = handler
}
2023-05-31 17:41:14 +08:00
// Close channel
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-05-31 17:41:14 +08:00
// receive Data
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
m.channel.isConnect = false
2023-03-19 00:00:09 +08:00
m.Close()
}()
2023-05-31 17:41:14 +08:00
// handler
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 {
2023-05-31 17:41:14 +08:00
// default handler
2023-03-19 22:40:10 +08:00
if m.handler != nil {
2023-05-31 17:41:14 +08:00
m.handler(context)
2023-03-19 22:40:10 +08:00
}
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
}