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