Compare commits
3 Commits
cbaff8b593
...
9bf0c596ba
Author | SHA1 | Date | |
---|---|---|---|
9bf0c596ba | |||
86cf3ee89d | |||
8fe3ba8d4f |
14
metadapta.go
14
metadapta.go
@ -103,12 +103,13 @@ func (this *a) unlistTransactionSafe(id int64) {
|
||||
func (this *a) sendMessageSafe(trans int64, method uint16, data []byte) error {
|
||||
this.sendLock.Lock()
|
||||
defer this.sendLock.Unlock()
|
||||
return encodeMessageA(this.underlying, trans, method, data)
|
||||
return encodeMessageA(this.underlying, this.sizeLimit, trans, method, data)
|
||||
}
|
||||
|
||||
func (this *a) receive() {
|
||||
defer func() {
|
||||
this.underlying.Close()
|
||||
close(this.transChan)
|
||||
this.transLock.Lock()
|
||||
defer this.transLock.Unlock()
|
||||
for _, trans := range this.transMap {
|
||||
@ -268,7 +269,16 @@ type incomingMessage struct {
|
||||
payload []byte
|
||||
}
|
||||
|
||||
func encodeMessageA(writer io.Writer, trans int64, method uint16, data []byte) error {
|
||||
func encodeMessageA(
|
||||
writer io.Writer,
|
||||
sizeLimit int64,
|
||||
trans int64,
|
||||
method uint16,
|
||||
data []byte,
|
||||
) error {
|
||||
if int64(len(data)) > sizeLimit {
|
||||
return ErrPayloadTooLarge
|
||||
}
|
||||
buffer := make([]byte, 18 + len(data))
|
||||
tape.EncodeI64(buffer[:8], trans)
|
||||
tape.EncodeI16(buffer[8:10], method)
|
||||
|
@ -31,21 +31,23 @@ func TestConnA(test *testing.T) {
|
||||
// server
|
||||
listener, err := net.Listen(network, addr)
|
||||
if err != nil { test.Fatal(err) }
|
||||
defer listener.Close()
|
||||
test.Cleanup(func() { listener.Close() })
|
||||
go func() {
|
||||
test.Log("SERVER listening")
|
||||
conn, err := listener.Accept()
|
||||
if err != nil { test.Error("SERVER", err); return }
|
||||
defer conn.Close()
|
||||
test.Cleanup(func() { conn.Close() })
|
||||
a := AdaptA(conn, ServerSide)
|
||||
trans, err := a.OpenTrans()
|
||||
if err != nil { test.Error("SERVER", err); return }
|
||||
defer trans.Close()
|
||||
test.Cleanup(func() { trans.Close() })
|
||||
for method, payload := range payloads {
|
||||
test.Log("SERVER", method, payload)
|
||||
test.Log("SERVER m:", method, "p:", payload)
|
||||
err := trans.Send(uint16(method), []byte(payload))
|
||||
if err != nil { test.Error("SERVER", err); return }
|
||||
}
|
||||
test.Log("SERVER closing connection")
|
||||
}()
|
||||
|
||||
// client
|
||||
@ -54,18 +56,18 @@ func TestConnA(test *testing.T) {
|
||||
if err != nil { test.Fatal("CLIENT", err) }
|
||||
test.Log("CLIENT dialed")
|
||||
a := AdaptA(conn, ClientSide)
|
||||
defer a.Close()
|
||||
test.Cleanup(func() { a.Close() })
|
||||
test.Log("CLIENT accepting transaction")
|
||||
trans, err := a.AcceptTrans()
|
||||
if err != nil { test.Fatal("CLIENT", err) }
|
||||
test.Log("CLIENT accepted transaction")
|
||||
defer trans.Close()
|
||||
test.Cleanup(func() { trans.Close() })
|
||||
for method, payload := range payloads {
|
||||
test.Log("CLIENT waiting...")
|
||||
gotMethod, gotPayloadBytes, err := trans.Receive()
|
||||
if err != nil { test.Fatal("CLIENT", err) }
|
||||
gotPayload := string(gotPayloadBytes)
|
||||
test.Log("CLIENT", gotMethod, gotPayload)
|
||||
test.Log("CLIENT m:", gotMethod, "p:", gotPayload)
|
||||
if int(gotMethod) != method {
|
||||
test.Errorf("CLIENT method not equal")
|
||||
}
|
||||
@ -73,11 +75,13 @@ func TestConnA(test *testing.T) {
|
||||
test.Errorf("CLIENT payload not equal")
|
||||
}
|
||||
}
|
||||
test.Log("CLIENT waiting for connection close...")
|
||||
_, _, err = trans.Receive()
|
||||
if !errors.Is(err, io.EOF) {
|
||||
test.Fatal("CLIENT wrong error:", err)
|
||||
}
|
||||
test.Log("CLIENT done")
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
func TestEncodeMessageA(test *testing.T) {
|
||||
@ -87,7 +91,7 @@ func TestEncodeMessageA(test *testing.T) {
|
||||
correct := []byte {
|
||||
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
|
||||
0x6B, 0x12,
|
||||
0x00, 0x06,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
|
||||
}
|
||||
if err != nil {
|
||||
@ -111,9 +115,9 @@ func TestDecodeMessageA(test *testing.T) {
|
||||
transID, method, _, payload, err := decodeMessageA(bytes.NewReader([]byte {
|
||||
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
|
||||
0x6B, 0x12,
|
||||
0x00, 0x06,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
|
||||
}))
|
||||
}), defaultSizeLimit)
|
||||
if err != nil {
|
||||
test.Fatal(err)
|
||||
}
|
||||
@ -133,9 +137,9 @@ func TestDecodeMessageAErr(test *testing.T) {
|
||||
_, _, _, _, err := decodeMessageA(bytes.NewReader([]byte {
|
||||
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
|
||||
0x6B, 0x12,
|
||||
0x01, 0x06,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
|
||||
}))
|
||||
}), defaultSizeLimit)
|
||||
if !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
test.Fatalf("wrong error: %v", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user