| 1 | // |
| 2 | // Connection.cpp |
| 3 | // |
| 4 | // Library: MongoDB |
| 5 | // Package: MongoDB |
| 6 | // Module: Connection |
| 7 | // |
| 8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/SocketStream.h" |
| 16 | #include "Poco/MongoDB/Connection.h" |
| 17 | |
| 18 | |
| 19 | namespace Poco { |
| 20 | namespace MongoDB { |
| 21 | |
| 22 | |
| 23 | Connection::Connection(): |
| 24 | _address(), |
| 25 | _socket() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | |
| 30 | Connection::Connection(const std::string& hostAndPort): |
| 31 | _address(hostAndPort), |
| 32 | _socket() |
| 33 | { |
| 34 | connect(); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | Connection::Connection(const std::string& host, int port): |
| 39 | _address(host, port), |
| 40 | _socket() |
| 41 | { |
| 42 | connect(); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | Connection::Connection(const Poco::Net::SocketAddress& addrs): |
| 47 | _address(addrs), |
| 48 | _socket() |
| 49 | { |
| 50 | connect(); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | Connection::Connection(const Poco::Net::StreamSocket& socket): |
| 55 | _address(socket.peerAddress()), |
| 56 | _socket(socket) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | |
| 61 | Connection::~Connection() |
| 62 | { |
| 63 | try |
| 64 | { |
| 65 | disconnect(); |
| 66 | } |
| 67 | catch (...) |
| 68 | { |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void Connection::connect() |
| 74 | { |
| 75 | _socket.connect(_address); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void Connection::connect(const std::string& hostAndPort) |
| 80 | { |
| 81 | _address = Poco::Net::SocketAddress(hostAndPort); |
| 82 | connect(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void Connection::connect(const std::string& host, int port) |
| 87 | { |
| 88 | _address = Poco::Net::SocketAddress(host, port); |
| 89 | connect(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void Connection::connect(const Poco::Net::SocketAddress& addrs) |
| 94 | { |
| 95 | _address = addrs; |
| 96 | connect(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void Connection::connect(const Poco::Net::StreamSocket& socket) |
| 101 | { |
| 102 | _address = socket.peerAddress(); |
| 103 | _socket = socket; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void Connection::disconnect() |
| 108 | { |
| 109 | _socket.close(); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void Connection::sendRequest(RequestMessage& request) |
| 114 | { |
| 115 | Poco::Net::SocketOutputStream sos(_socket); |
| 116 | request.send(sos); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void Connection::sendRequest(RequestMessage& request, ResponseMessage& response) |
| 121 | { |
| 122 | sendRequest(request); |
| 123 | |
| 124 | Poco::Net::SocketInputStream sis(_socket); |
| 125 | response.read(sis); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | } } // Poco::MongoDB |
| 130 | |