Class: Moped::Connection Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class contains behaviour of database socket connections.
Defined Under Namespace
Classes: TCPSocket
Instance Method Summary collapse
-
#alive? ⇒ true, false
private
Is the connection alive?.
-
#connect(host, port, timeout) ⇒ TCPSocket
private
Connect to the server.
-
#connected? ⇒ true, false
private
Is the connection connected?.
-
#disconnect ⇒ nil
private
Disconnect from the server.
-
#initialize ⇒ Connection
constructor
private
Initialize the connection.
-
#read ⇒ Hash
private
Read from the connection.
-
#receive_replies(operations) ⇒ Array<Hash>
private
Get the replies to the database operation.
-
#write(operations) ⇒ Integer
private
Write to the connection.
Constructor Details
#initialize ⇒ Connection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize the connection.
71 72 73 74 |
# File 'lib/moped/connection.rb', line 71 def initialize @sock = nil @request_id = 0 end |
Instance Method Details
#alive? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the connection alive?
18 19 20 |
# File 'lib/moped/connection.rb', line 18 def alive? connected? ? @sock.alive? : false end |
#connect(host, port, timeout) ⇒ TCPSocket
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Connect to the server.
34 35 36 |
# File 'lib/moped/connection.rb', line 34 def connect(host, port, timeout) @sock = TCPSocket.connect host, port, timeout end |
#connected? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the connection connected?
46 47 48 |
# File 'lib/moped/connection.rb', line 46 def connected? !!@sock end |
#disconnect ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Disconnect from the server.
58 59 60 61 62 63 |
# File 'lib/moped/connection.rb', line 58 def disconnect @sock.close rescue ensure @sock = nil end |
#read ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Read from the connection.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/moped/connection.rb', line 84 def read reply = Protocol::Reply.allocate reply.length, reply.request_id, reply.response_to, reply.op_code, reply.flags, reply.cursor_id, reply.offset, reply.count = @sock.read(36).unpack('l<5q<l<2') if reply.count == 0 reply.documents = [] else buffer = StringIO.new(@sock.read(reply.length - 36)) reply.documents = reply.count.times.map do BSON::Document.deserialize(buffer) end end reply end |
#receive_replies(operations) ⇒ Array<Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the replies to the database operation.
117 118 119 120 121 |
# File 'lib/moped/connection.rb', line 117 def receive_replies(operations) operations.map do |operation| operation.receive_replies(self) end end |
#write(operations) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write to the connection.
133 134 135 136 137 138 139 140 |
# File 'lib/moped/connection.rb', line 133 def write(operations) buf = "" operations.each do |operation| operation.request_id = (@request_id += 1) operation.serialize(buf) end @sock.write(buf) end |