1 /*
2    Copyright (C) 2003-2006, 2008 MySQL AB, 2008 Sun Microsystems, Inc.
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 #ifndef SHM_Transporter_H
27 #define SHM_Transporter_H
28 
29 #include "Transporter.hpp"
30 #include "SHM_Buffer.hpp"
31 
32 #ifdef NDB_WIN32
33 typedef Uint32 key_t;
34 #endif
35 
36 /**
37  * class SHMTransporter
38  * @brief - main class for the SHM transporter.
39  */
40 
41 class SHM_Transporter : public Transporter {
42   friend class TransporterRegistry;
43 public:
44   SHM_Transporter(TransporterRegistry &,
45 		  const char *lHostName,
46 		  const char *rHostName,
47 		  int r_port,
48 		  bool isMgmConnection,
49 		  NodeId lNodeId,
50 		  NodeId rNodeId,
51 		  NodeId serverNodeId,
52 		  bool checksum,
53 		  bool signalId,
54 		  key_t shmKey,
55 		  Uint32 shmSize);
56 
57   /**
58    * SHM destructor
59    */
60   virtual ~SHM_Transporter();
61 
62   virtual bool configure_derived(const TransporterConfiguration* conf);
63 
64   /**
65    * Do initialization
66    */
67   bool initTransporter();
68 
getReceivePtr(Uint32 ** ptr,Uint32 ** eod)69   void getReceivePtr(Uint32 ** ptr, Uint32 ** eod){
70     reader->getReadPtr(* ptr, * eod);
71   }
72 
updateReceivePtr(Uint32 * ptr)73   void updateReceivePtr(Uint32 * ptr){
74     reader->updateReadPtr(ptr);
75   }
76 
77 protected:
78   /**
79    * disconnect a segmnet
80    * -# deletes the shm buffer associated with a segment
81    * -# marks the segment for removal
82    */
83   void disconnectImpl();
84 
85   /**
86    * Blocking
87    *
88    * -# Create shm segment
89    * -# Attach to it
90    * -# Wait for someone to attach (max wait = timeout), then rerun again
91    *    until connection established.
92    * @param timeOutMillis - the time to sleep before (ms) trying again.
93    * @returns - True if the server managed to hook up with the client,
94    *            i.e., both agrees that the other one has setup the segment.
95    *            Otherwise false.
96    */
97   virtual bool connect_server_impl(NDB_SOCKET_TYPE sockfd);
98 
99   /**
100    * Blocking
101    *
102    * -# Attach to shm segment
103    * -# Check if the segment is setup
104    * -# Check if the server set it up
105    * -# If all clear, return.
106    * @param timeOutMillis - the time to sleep before (ms) trying again.
107    * @returns - True if the client managed to hook up with the server,
108    *            i.e., both agrees that the other one has setup the segment.
109    *            Otherwise false.
110    */
111   virtual bool connect_client_impl(NDB_SOCKET_TYPE sockfd);
112 
113   bool connect_common(NDB_SOCKET_TYPE sockfd);
114 
115   bool ndb_shm_create();
116   bool ndb_shm_get();
117   bool ndb_shm_attach();
118 
119   /**
120    * Check if there are two processes attached to the segment (a connection)
121    * @return - True if the above holds. Otherwise false.
122    */
123   bool checkConnected();
124 
125 
126   /**
127    * Initialises the SHM_Reader and SHM_Writer on the segment
128    */
129   void setupBuffers();
130 
131   /**
132    * doSend (i.e signal receiver)
133    */
134   int doSend();
135   int m_remote_pid;
136   Uint32 m_signal_threshold;
137 
138 private:
139   bool _shmSegCreated;
140   bool _attached;
141   bool m_connected;
142 
143   key_t shmKey;
144   volatile Uint32 * serverStatusFlag;
145   volatile Uint32 * clientStatusFlag;
146   bool setupBuffersDone;
147 
148 #ifdef NDB_WIN32
149   HANDLE hFileMapping;
150 #else
151   int shmId;
152 #endif
153 
154   int shmSize;
155   char * shmBuf;
156 
157   SHM_Reader * reader;
158   SHM_Writer * writer;
159 
160   /**
161    * @return - True if the reader has data to read on its segment.
162    */
hasDataToRead() const163   bool hasDataToRead() const {
164     return reader->empty() == false;
165   }
166 
167   void make_error_info(char info[], int sz);
168 
send_limit_reached(int bufsize)169   bool send_limit_reached(int bufsize)
170   {
171     return ((Uint32)bufsize >= m_signal_threshold);
172   }
send_is_possible(int timeout_millisec) const173   bool send_is_possible(int timeout_millisec) const { return 1; }
174 };
175 
176 #endif
177