MongoDB C++ Driver  legacy-1.1.2
message_port.h
1 // message_port.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "mongo/config.h"
21 
22 #include <boost/shared_ptr.hpp>
23 #include <boost/utility.hpp>
24 #include <vector>
25 
26 #include "mongo/util/net/message.h"
27 #include "mongo/util/net/sock.h"
28 
29 namespace mongo {
30 
31 class MessagingPort;
32 class PiggyBackData;
33 
34 class AbstractMessagingPort : boost::noncopyable {
35 public:
36  AbstractMessagingPort() : tag(0), _connectionId(0) {}
37  virtual ~AbstractMessagingPort() {}
38  virtual void reply(
39  Message& received,
40  Message& response,
41  // like the reply below, but doesn't rely on received.data still being available
42  MSGID responseTo) = 0;
43  virtual void reply(Message& received, Message& response) = 0;
44 
45  virtual HostAndPort remote() const = 0;
46  virtual unsigned remotePort() const = 0;
47  virtual SockAddr remoteAddr() const = 0;
48  virtual SockAddr localAddr() const = 0;
49 
50  void setX509SubjectName(const std::string& x509SubjectName) {
51  _x509SubjectName = x509SubjectName;
52  }
53 
54  std::string getX509SubjectName() {
55  return _x509SubjectName;
56  }
57 
58  long long connectionId() const {
59  return _connectionId;
60  }
61  void setConnectionId(long long connectionId);
62 
63 public:
64  // TODO make this private with some helpers
65 
66  /* ports can be tagged with various classes. see closeAllSockets(tag). defaults to 0. */
67  unsigned tag;
68 
69 private:
70  long long _connectionId;
71  std::string _x509SubjectName;
72 };
73 
75 public:
76  MessagingPort(int fd, const SockAddr& remote);
77 
78  // in some cases the timeout will actually be 2x this value - eg we do a partial send,
79  // then the timeout fires, then we try to send again, then the timeout fires again with
80  // no data sent, then we detect that the other side is down
81  MessagingPort(double so_timeout = 0, logger::LogSeverity logLevel = logger::LogSeverity::Log());
82 
83  MessagingPort(boost::shared_ptr<Socket> socket);
84 
85  virtual ~MessagingPort();
86 
87  void setSocketTimeout(double timeout);
88 
89  void shutdown();
90 
91  /* it's assumed if you reuse a message object, that it doesn't cross MessagingPort's.
92  also, the Message data will go out of scope on the subsequent recv call.
93  */
94  bool recv(Message& m);
95  void reply(Message& received, Message& response, MSGID responseTo);
96  void reply(Message& received, Message& response);
97  bool call(Message& toSend, Message& response);
98 
99  void say(Message& toSend, int responseTo = 0);
100 
110  bool recv(const Message& sent, Message& response);
111 
112  void piggyBack(Message& toSend, int responseTo = 0);
113 
114  unsigned remotePort() const {
115  return psock->remotePort();
116  }
117  virtual HostAndPort remote() const;
118  virtual SockAddr remoteAddr() const;
119  virtual SockAddr localAddr() const;
120 
121  boost::shared_ptr<Socket> psock;
122 
123  void send(const char* data, int len, const char* context) {
124  psock->send(data, len, context);
125  }
126  void send(const std::vector<std::pair<char*, int> >& data, const char* context) {
127  psock->send(data, context);
128  }
129  bool connect(SockAddr& farEnd) {
130  return psock->connect(farEnd);
131  }
132 #ifdef MONGO_SSL
133 
140  bool secure(SSLManagerInterface* ssl, const std::string& remoteHost) {
141  return psock->secure(ssl, remoteHost);
142  }
143 #endif
144 
145  bool isStillConnected() {
146  return psock->isStillConnected();
147  }
148 
149  uint64_t getSockCreationMicroSec() const {
150  return psock->getSockCreationMicroSec();
151  }
152 
153 private:
154  PiggyBackData* piggyBackData;
155 
156  // this is the parsed version of remote
157  // mutable because its initialized only on call to remote()
158  mutable HostAndPort _remoteParsed;
159 
160 public:
161  static void closeAllSockets(unsigned tagMask = 0xffffffff);
162 
163  friend class PiggyBackData;
164 };
165 
166 
167 } // namespace mongo
Definition: message_port.h:34
Definition: message.h:305
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
wrapped around os representation of network address
Definition: sock.h:96
Name of a process on the network.
Definition: hostandport.h:37
Representation of the severity / priority of a log message.
Definition: log_severity.h:33
Definition: message_port.h:74