MongoDB  2.5.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 "mongo/util/net/message.h"
21 #include "mongo/util/net/sock.h"
22 
23 namespace mongo {
24 
25  class MessagingPort;
26  class PiggyBackData;
27 
28  typedef AtomicUInt MSGID;
29 
30  class AbstractMessagingPort : boost::noncopyable {
31  public:
32  AbstractMessagingPort() : tag(0), _connectionId(0) {}
33  virtual ~AbstractMessagingPort() { }
34  virtual void reply(Message& received, Message& response, MSGID responseTo) = 0; // like the reply below, but doesn't rely on received.data still being available
35  virtual void reply(Message& received, Message& response) = 0;
36 
37  virtual HostAndPort remote() const = 0;
38  virtual unsigned remotePort() const = 0;
39 
40  long long connectionId() const { return _connectionId; }
41  void setConnectionId( long long connectionId );
42 
43  public:
44  // TODO make this private with some helpers
45 
46  /* ports can be tagged with various classes. see closeAllSockets(tag). defaults to 0. */
47  unsigned tag;
48 
49  private:
50  long long _connectionId;
51  };
52 
54  public:
55  MessagingPort(int fd, const SockAddr& remote);
56 
57  // in some cases the timeout will actually be 2x this value - eg we do a partial send,
58  // then the timeout fires, then we try to send again, then the timeout fires again with
59  // no data sent, then we detect that the other side is down
60  MessagingPort(double so_timeout = 0, int logLevel = 0 );
61 
62  MessagingPort(boost::shared_ptr<Socket> socket);
63 
64  virtual ~MessagingPort();
65 
66  void setSocketTimeout(double timeout);
67 
68  void shutdown();
69 
70  /* it's assumed if you reuse a message object, that it doesn't cross MessagingPort's.
71  also, the Message data will go out of scope on the subsequent recv call.
72  */
73  bool recv(Message& m);
74  void reply(Message& received, Message& response, MSGID responseTo);
75  void reply(Message& received, Message& response);
76  bool call(Message& toSend, Message& response);
77 
78  void say(Message& toSend, int responseTo = -1);
79 
89  bool recv( const Message& sent , Message& response );
90 
91  void piggyBack( Message& toSend , int responseTo = -1 );
92 
93  unsigned remotePort() const { return psock->remotePort(); }
94  virtual HostAndPort remote() const;
95 
96  boost::shared_ptr<Socket> psock;
97 
98  void send( const char * data , int len, const char *context ) {
99  psock->send( data, len, context );
100  }
101  void send( const vector< pair< char *, int > > &data, const char *context ) {
102  psock->send( data, context );
103  }
104  bool connect(SockAddr& farEnd) {
105  return psock->connect( farEnd );
106  }
107 #ifdef MONGO_SSL
108 
113  void secure( SSLManagerInterface* ssl ) {
114  psock->secure( ssl );
115  }
116 #endif
117 
118  bool isStillConnected() {
119  return psock->isStillConnected();
120  }
121 
122  uint64_t getSockCreationMicroSec() const {
123  return psock->getSockCreationMicroSec();
124  }
125 
126  private:
127 
128  PiggyBackData * piggyBackData;
129 
130  // this is the parsed version of remote
131  // mutable because its initialized only on call to remote()
132  mutable HostAndPort _remoteParsed;
133 
134  public:
135  static void closeAllSockets(unsigned tagMask = 0xffffffff);
136 
137  friend class PiggyBackData;
138  };
139 
140 
141 } // namespace mongo