1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mtest
import (
"errors"
"fmt"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
)
// ReceivedMessage represents a message received from the server.
type ReceivedMessage struct {
ResponseTo int32
RawMessage wiremessage.WireMessage
Response bsoncore.Document
}
type receivedMsgParseFn func([]byte) (*ReceivedMessage, error)
func getReceivedMessageParser(opcode wiremessage.OpCode) (receivedMsgParseFn, bool) {
switch opcode {
case wiremessage.OpReply:
return parseOpReply, true
case wiremessage.OpMsg:
return parseReceivedOpMsg, true
case wiremessage.OpCompressed:
return parseReceivedOpCompressed, true
default:
return nil, false
}
}
func parseReceivedMessage(wm []byte) (*ReceivedMessage, error) {
// Re-assign the wire message to "remaining" so "wm" continues to point to the entire message after parsing.
_, _, responseTo, opcode, remaining, ok := wiremessage.ReadHeader(wm)
if !ok {
return nil, errors.New("failed to read wiremessage header")
}
parseFn, ok := getReceivedMessageParser(opcode)
if !ok {
return nil, fmt.Errorf("unknown opcode: %s", opcode)
}
received, err := parseFn(remaining)
if err != nil {
return nil, fmt.Errorf("error parsing wiremessage with opcode %s: %v", opcode, err)
}
received.ResponseTo = responseTo
received.RawMessage = wm
return received, nil
}
func parseOpReply(wm []byte) (*ReceivedMessage, error) {
var ok bool
if _, wm, ok = wiremessage.ReadReplyFlags(wm); !ok {
return nil, errors.New("failed to read reply flags")
}
if _, wm, ok = wiremessage.ReadReplyCursorID(wm); !ok {
return nil, errors.New("failed to read cursor ID")
}
if _, wm, ok = wiremessage.ReadReplyStartingFrom(wm); !ok {
return nil, errors.New("failed to read starting from")
}
if _, wm, ok = wiremessage.ReadReplyNumberReturned(wm); !ok {
return nil, errors.New("failed to read number returned")
}
var replyDocuments []bsoncore.Document
replyDocuments, wm, ok = wiremessage.ReadReplyDocuments(wm)
if !ok {
return nil, errors.New("failed to read reply documents")
}
if len(replyDocuments) == 0 {
return nil, errors.New("no documents in response")
}
rm := &ReceivedMessage{
Response: replyDocuments[0],
}
return rm, nil
}
func parseReceivedOpMsg(wm []byte) (*ReceivedMessage, error) {
var ok bool
var err error
if _, wm, ok = wiremessage.ReadMsgFlags(wm); !ok {
return nil, errors.New("failed to read flags")
}
if wm, err = assertMsgSectionType(wm, wiremessage.SingleDocument); err != nil {
return nil, fmt.Errorf("error verifying section type for response document: %v", err)
}
response, wm, ok := wiremessage.ReadMsgSectionSingleDocument(wm)
if !ok {
return nil, errors.New("failed to read response document")
}
rm := &ReceivedMessage{
Response: response,
}
return rm, nil
}
func parseReceivedOpCompressed(wm []byte) (*ReceivedMessage, error) {
originalOpcode, wm, err := parseOpCompressed(wm)
if err != nil {
return nil, err
}
parser, ok := getReceivedMessageParser(originalOpcode)
if !ok {
return nil, fmt.Errorf("unknown original opcode %v", originalOpcode)
}
return parser(wm)
}
|