MongoDB C++ Driver  legacy-1.0.5
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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(Message& received, Message& response, MSGID responseTo) = 0; // like the reply below, but doesn't rely on received.data still being available
39  virtual void reply(Message& received, Message& response) = 0;
40 
41  virtual HostAndPort remote() const = 0;
42  virtual unsigned remotePort() const = 0;
43  virtual SockAddr remoteAddr() const = 0;
44  virtual SockAddr localAddr() const = 0;
45 
46  void setX509SubjectName(const std::string& x509SubjectName) {
47  _x509SubjectName = x509SubjectName;
48  }
49 
50  std::string getX509SubjectName() {
51  return _x509SubjectName;
52  }
53 
54  long long connectionId() const { return _connectionId; }
55  void setConnectionId( long long connectionId );
56 
57  public:
58  // TODO make this private with some helpers
59 
60  /* ports can be tagged with various classes. see closeAllSockets(tag). defaults to 0. */
61  unsigned tag;
62 
63  private:
64  long long _connectionId;
65  std::string _x509SubjectName;
66  };
67 
69  public:
70  MessagingPort(int fd, const SockAddr& remote);
71 
72  // in some cases the timeout will actually be 2x this value - eg we do a partial send,
73  // then the timeout fires, then we try to send again, then the timeout fires again with
74  // no data sent, then we detect that the other side is down
75  MessagingPort(double so_timeout = 0,
76  logger::LogSeverity logLevel = logger::LogSeverity::Log() );
77 
78  MessagingPort(boost::shared_ptr<Socket> socket);
79 
80  virtual ~MessagingPort();
81 
82  void setSocketTimeout(double timeout);
83 
84  void shutdown();
85 
86  /* it's assumed if you reuse a message object, that it doesn't cross MessagingPort's.
87  also, the Message data will go out of scope on the subsequent recv call.
88  */
89  bool recv(Message& m);
90  void reply(Message& received, Message& response, MSGID responseTo);
91  void reply(Message& received, Message& response);
92  bool call(Message& toSend, Message& response);
93 
94  void say(Message& toSend, int responseTo = 0);
95 
105  bool recv( const Message& sent , Message& response );
106 
107  void piggyBack( Message& toSend , int responseTo = 0 );
108 
109  unsigned remotePort() const { return psock->remotePort(); }
110  virtual HostAndPort remote() const;
111  virtual SockAddr remoteAddr() const;
112  virtual SockAddr localAddr() const;
113 
114  boost::shared_ptr<Socket> psock;
115 
116  void send( const char * data , int len, const char *context ) {
117  psock->send( data, len, context );
118  }
119  void send(const std::vector< std::pair< char *, int > > &data, const char *context) {
120  psock->send( data, context );
121  }
122  bool connect(SockAddr& farEnd) {
123  return psock->connect( farEnd );
124  }
125 #ifdef MONGO_SSL
126 
133  bool secure( SSLManagerInterface* ssl, const std::string& remoteHost ) {
134  return psock->secure( ssl, remoteHost );
135  }
136 #endif
137 
138  bool isStillConnected() {
139  return psock->isStillConnected();
140  }
141 
142  uint64_t getSockCreationMicroSec() const {
143  return psock->getSockCreationMicroSec();
144  }
145 
146  private:
147 
148  PiggyBackData * piggyBackData;
149 
150  // this is the parsed version of remote
151  // mutable because its initialized only on call to remote()
152  mutable HostAndPort _remoteParsed;
153 
154  public:
155  static void closeAllSockets(unsigned tagMask = 0xffffffff);
156 
157  friend class PiggyBackData;
158  };
159 
160 
161 } // namespace mongo
Definition: message_port.h:34
Definition: message.h:298
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
wrapped around os representation of network address
Definition: sock.h:94
Name of a process on the network.
Definition: hostandport.h:36
Definition: message_port.h:68