Class: Mongo::Protocol::Message Abstract
- Inherits:
-
Object
- Object
- Mongo::Protocol::Message
- Includes:
- Serializers
- Defined in:
- lib/mongo/protocol/message.rb
Overview
A base class providing functionality required by all messages in the MongoDB wire protocol. It provides a minimal DSL for defining typed fields to enable serialization and deserialization over the wire.
Constant Summary collapse
- BATCH_SIZE =
The batch size constant.
'batchSize'.freeze
- COLLECTION =
The collection constant.
'collection'.freeze
- LIMIT =
The limit constant.
'limit'.freeze
- ORDERED =
The ordered constant.
'ordered'.freeze
- Q =
The q constant.
'q'.freeze
- MAX_MESSAGE_SIZE =
Default max message size of 48MB.
50331648.freeze
Instance Attribute Summary collapse
-
#request_id ⇒ Fixnum
readonly
Returns the request id for the message.
Class Method Summary collapse
-
.deserialize(io, max_message_size = MAX_MESSAGE_SIZE, expected_response_to = nil) ⇒ Message
Deserializes messages from an IO stream.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Tests for equality between two wire protocol messages by comparing class and field values.
-
#hash ⇒ Fixnum
Creates a hash from the values of the fields of a message.
-
#replyable? ⇒ false
The default for messages is not to require a reply after sending a message to the server.
-
#serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil) ⇒ String
(also: #to_s)
Serializes message into bytes that can be sent on the wire.
-
#set_request_id ⇒ Fixnum
Generates a request id for a message.
Instance Attribute Details
#request_id ⇒ Fixnum (readonly)
Returns the request id for the message
78 79 80 |
# File 'lib/mongo/protocol/message.rb', line 78 def request_id @request_id end |
Class Method Details
.deserialize(io, max_message_size = MAX_MESSAGE_SIZE, expected_response_to = nil) ⇒ Message
Deserializes messages from an IO stream
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mongo/protocol/message.rb', line 112 def self.deserialize(io, = MAX_MESSAGE_SIZE, expected_response_to = nil) length, request_id, response_to, op_code = deserialize_header(BSON::ByteBuffer.new(io.read(16))) # Protection from potential DOS man-in-the-middle attacks. See # DRIVERS-276. if length > ( || MAX_MESSAGE_SIZE) raise Error::MaxMessageSize.new() end # Protection against returning the response to a previous request. See # RUBY-1117 if expected_response_to && response_to != expected_response_to raise Error::UnexpectedResponse.new(expected_response_to, response_to) end buffer = BSON::ByteBuffer.new(io.read(length - 16)) = allocate fields.each do |field| if field[:multi] deserialize_array(, buffer, field) else deserialize_field(, buffer, field) end end end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Tests for equality between two wire protocol messages by comparing class and field values.
145 146 147 148 149 150 151 152 |
# File 'lib/mongo/protocol/message.rb', line 145 def ==(other) return false if self.class != other.class fields.all? do |field| name = field[:name] instance_variable_get(name) == other.instance_variable_get(name) end end |
#hash ⇒ Fixnum
Creates a hash from the values of the fields of a message.
158 159 160 |
# File 'lib/mongo/protocol/message.rb', line 158 def hash fields.map { |field| instance_variable_get(field[:name]) }.hash end |
#replyable? ⇒ false
The default for messages is not to require a reply after sending a message to the server.
89 90 91 |
# File 'lib/mongo/protocol/message.rb', line 89 def replyable? false end |
#serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil) ⇒ String Also known as: to_s
Serializes message into bytes that can be sent on the wire
97 98 99 100 101 102 |
# File 'lib/mongo/protocol/message.rb', line 97 def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil) start = buffer.length serialize_header(buffer) serialize_fields(buffer, max_bson_size) buffer.replace_int32(start, buffer.length - start) end |
#set_request_id ⇒ Fixnum
Generates a request id for a message
167 168 169 170 171 |
# File 'lib/mongo/protocol/message.rb', line 167 def set_request_id @@id_lock.synchronize do @request_id = @@request_id += 1 end end |