2022-06-27 21:14:07 +02:00
|
|
|
// Copyright 2022 Dashborg Inc
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-07-06 08:14:14 +02:00
|
|
|
"context"
|
2022-06-27 21:14:07 +02:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PacketParser struct {
|
|
|
|
Lock *sync.Mutex
|
|
|
|
MainCh chan PacketType
|
2022-07-06 08:14:14 +02:00
|
|
|
RpcMap map[string]*RpcEntry
|
2022-07-06 02:45:46 +02:00
|
|
|
Err error
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 08:14:14 +02:00
|
|
|
type RpcEntry struct {
|
|
|
|
ReqId string
|
|
|
|
RespCh chan RpcResponsePacketType
|
|
|
|
}
|
|
|
|
|
2022-06-27 21:14:07 +02:00
|
|
|
func CombinePacketParsers(p1 *PacketParser, p2 *PacketParser) *PacketParser {
|
|
|
|
rtnParser := &PacketParser{
|
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
MainCh: make(chan PacketType),
|
2022-07-06 23:35:27 +02:00
|
|
|
RpcMap: make(map[string]*RpcEntry),
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-07-07 07:46:59 +02:00
|
|
|
for pk := range p1.MainCh {
|
|
|
|
sent := rtnParser.trySendRpcResponse(pk)
|
|
|
|
if sent {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rtnParser.MainCh <- pk
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-07-07 07:46:59 +02:00
|
|
|
for pk := range p2.MainCh {
|
|
|
|
sent := rtnParser.trySendRpcResponse(pk)
|
|
|
|
if sent {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rtnParser.MainCh <- pk
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(rtnParser.MainCh)
|
|
|
|
}()
|
|
|
|
return rtnParser
|
|
|
|
}
|
|
|
|
|
2022-07-06 08:14:14 +02:00
|
|
|
// should have already registered rpc
|
|
|
|
func (p *PacketParser) WaitForResponse(ctx context.Context, reqId string) RpcResponsePacketType {
|
2022-07-07 07:46:59 +02:00
|
|
|
entry := p.getRpcEntry(reqId)
|
2022-07-06 08:14:14 +02:00
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer p.UnRegisterRpc(reqId)
|
|
|
|
select {
|
|
|
|
case resp := <-entry.RespCh:
|
|
|
|
return resp
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PacketParser) UnRegisterRpc(reqId string) {
|
|
|
|
p.Lock.Lock()
|
|
|
|
defer p.Lock.Unlock()
|
|
|
|
entry := p.RpcMap[reqId]
|
|
|
|
if entry != nil {
|
|
|
|
close(entry.RespCh)
|
|
|
|
delete(p.RpcMap, reqId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:35:27 +02:00
|
|
|
func (p *PacketParser) RegisterRpc(reqId string) chan RpcResponsePacketType {
|
|
|
|
return p.RegisterRpcSz(reqId, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PacketParser) RegisterRpcSz(reqId string, queueSize int) chan RpcResponsePacketType {
|
2022-07-06 08:14:14 +02:00
|
|
|
p.Lock.Lock()
|
|
|
|
defer p.Lock.Unlock()
|
|
|
|
ch := make(chan RpcResponsePacketType, queueSize)
|
|
|
|
entry := &RpcEntry{ReqId: reqId, RespCh: ch}
|
|
|
|
p.RpcMap[reqId] = entry
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2022-07-07 07:46:59 +02:00
|
|
|
func (p *PacketParser) getRpcEntry(reqId string) *RpcEntry {
|
2022-07-06 08:14:14 +02:00
|
|
|
p.Lock.Lock()
|
|
|
|
defer p.Lock.Unlock()
|
|
|
|
entry := p.RpcMap[reqId]
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
2022-07-07 07:46:59 +02:00
|
|
|
func (p *PacketParser) trySendRpcResponse(pk PacketType) bool {
|
|
|
|
respPk, ok := pk.(RpcResponsePacketType)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-06 08:14:14 +02:00
|
|
|
p.Lock.Lock()
|
|
|
|
defer p.Lock.Unlock()
|
|
|
|
entry := p.RpcMap[respPk.GetResponseId()]
|
|
|
|
if entry == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// nonblocking send
|
|
|
|
select {
|
|
|
|
case entry.RespCh <- respPk:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if respPk.GetResponseDone() {
|
|
|
|
delete(p.RpcMap, respPk.GetResponseId())
|
|
|
|
close(entry.RespCh)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-06 02:45:46 +02:00
|
|
|
func (p *PacketParser) GetErr() error {
|
|
|
|
p.Lock.Lock()
|
|
|
|
defer p.Lock.Unlock()
|
|
|
|
return p.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PacketParser) SetErr(err error) {
|
|
|
|
p.Lock.Lock()
|
|
|
|
defer p.Lock.Unlock()
|
|
|
|
if p.Err == nil {
|
|
|
|
p.Err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 21:14:07 +02:00
|
|
|
func MakePacketParser(input io.Reader) *PacketParser {
|
|
|
|
parser := &PacketParser{
|
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
MainCh: make(chan PacketType),
|
2022-07-06 23:35:27 +02:00
|
|
|
RpcMap: make(map[string]*RpcEntry),
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
bufReader := bufio.NewReader(input)
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(parser.MainCh)
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
line, err := bufReader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-06 02:45:46 +02:00
|
|
|
parser.SetErr(err)
|
2022-06-27 21:14:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if line == "\n" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// ##[len][json]\n
|
|
|
|
// ##14{"hello":true}\n
|
2022-10-28 06:59:17 +02:00
|
|
|
// ##N{...}
|
2022-06-27 21:14:07 +02:00
|
|
|
bracePos := strings.Index(line, "{")
|
|
|
|
if !strings.HasPrefix(line, "##") || bracePos == -1 {
|
|
|
|
parser.MainCh <- MakeRawPacket(line[:len(line)-1])
|
|
|
|
continue
|
|
|
|
}
|
2022-06-28 03:42:56 +02:00
|
|
|
packetLen := -1
|
|
|
|
if line[2:bracePos] != "N" {
|
|
|
|
packetLen, err = strconv.Atoi(line[2:bracePos])
|
|
|
|
if err != nil || packetLen != len(line)-bracePos-1 {
|
|
|
|
parser.MainCh <- MakeRawPacket(line[:len(line)-1])
|
|
|
|
continue
|
|
|
|
}
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
pk, err := ParseJsonPacket([]byte(line[bracePos:]))
|
|
|
|
if err != nil {
|
2022-07-06 02:45:46 +02:00
|
|
|
parser.MainCh <- MakeRawPacket(line[:len(line)-1])
|
|
|
|
continue
|
2022-06-27 21:14:07 +02:00
|
|
|
}
|
|
|
|
if pk.GetType() == DonePacketStr {
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 06:57:30 +02:00
|
|
|
if pk.GetType() == PingPacketStr {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-07 07:46:59 +02:00
|
|
|
sent := parser.trySendRpcResponse(pk)
|
|
|
|
if sent {
|
|
|
|
continue
|
2022-07-06 08:14:14 +02:00
|
|
|
}
|
2022-06-27 21:14:07 +02:00
|
|
|
parser.MainCh <- pk
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return parser
|
|
|
|
}
|