more data
This commit is contained in:
52
rpc-pool.go
52
rpc-pool.go
@@ -1,13 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
const (
|
||||
RemoteGetState = uint16(0x01)
|
||||
RemoteHandleMessage = uint16(0x02)
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RemoteGrainPool struct {
|
||||
@@ -16,7 +12,7 @@ type RemoteGrainPool struct {
|
||||
}
|
||||
|
||||
func (id CartId) String() string {
|
||||
return string(id[:])
|
||||
return strings.Trim(string(id[:]), "\x00")
|
||||
}
|
||||
|
||||
func ToCartId(id string) CartId {
|
||||
@@ -49,46 +45,30 @@ func (g *RemoteGrain) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Packet struct {
|
||||
Version uint16
|
||||
MessageType uint16
|
||||
Id CartId
|
||||
DataLength uint16
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) SendPacket(messageType uint16, data []byte) error {
|
||||
binary.Write(g.client, binary.LittleEndian, Packet{
|
||||
Version: 2,
|
||||
MessageType: messageType,
|
||||
Id: g.Id,
|
||||
DataLength: uint16(len(data)),
|
||||
})
|
||||
return binary.Write(g.client, binary.LittleEndian, data)
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) HandleMessage(message *Message, isReplay bool) ([]byte, error) {
|
||||
data, err := GetData(message.Write)
|
||||
|
||||
err := SendCartPacket(g.client, g.Id, RemoteHandleMessage, message.Write)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = g.SendPacket(RemoteHandleMessage, data)
|
||||
result := make([]byte, 65535)
|
||||
g.client.Read(result)
|
||||
return result, err
|
||||
_, data, err := ReceivePacket(g.client)
|
||||
return data, err
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) GetId() CartId {
|
||||
return g.Id
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) GetCurrentState() (Grain, error) {
|
||||
func (g *RemoteGrain) GetCurrentState() ([]byte, error) {
|
||||
|
||||
var reply CartGrain
|
||||
err := g.SendPacket(RemoteGetState, nil)
|
||||
err := SendCartPacket(g.client, g.Id, RemoteGetState, func(w io.Writer) error {
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &reply, err
|
||||
_, data, err := ReceivePacket(g.client)
|
||||
return data, err
|
||||
}
|
||||
|
||||
func NewRemoteGrainPool(addr ...string) *RemoteGrainPool {
|
||||
@@ -106,8 +86,8 @@ func (p *RemoteGrainPool) findRemoteGrain(id CartId) *RemoteGrain {
|
||||
return &grain
|
||||
}
|
||||
|
||||
func (p *RemoteGrainPool) Process(id CartId, messages ...Message) (interface{}, error) {
|
||||
var result interface{}
|
||||
func (p *RemoteGrainPool) Process(id CartId, messages ...Message) ([]byte, error) {
|
||||
var result []byte
|
||||
var err error
|
||||
grain := p.findRemoteGrain(id)
|
||||
if grain == nil {
|
||||
@@ -121,7 +101,7 @@ func (p *RemoteGrainPool) Process(id CartId, messages ...Message) (interface{},
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (p *RemoteGrainPool) Get(id CartId) (Grain, error) {
|
||||
func (p *RemoteGrainPool) Get(id CartId) ([]byte, error) {
|
||||
grain := p.findRemoteGrain(id)
|
||||
if grain == nil {
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user