1 /*
2    Copyright (C) 2003-2006, 2008 MySQL AB
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 
27 #include <ndb_global.h>
28 
29 #include "SHM_Transporter.hpp"
30 #include "TransporterInternalDefinitions.hpp"
31 #include <TransporterCallback.hpp>
32 #include <NdbSleep.h>
33 #include <NdbOut.hpp>
34 
35 #include <windows.h>
36 
37 
make_error_info(char info[],int sz)38 void SHM_Transporter::make_error_info(char info[], int sz)
39 {
40   snprintf(info,sz,"Shm key=%d sz=%d",
41 	   shmKey, shmSize);
42 }
43 
44 bool
connectServer(Uint32 timeOutMillis)45 SHM_Transporter::connectServer(Uint32 timeOutMillis){
46   if(!_shmSegCreated)
47   {
48     char szName[32];
49     sprintf(szName, "ndb%lu", shmKey);
50     hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
51 				     0,
52 				     PAGE_READWRITE,
53 				     0,
54 				     shmSize,
55 				     szName);
56 
57     if(!hFileMapping)
58     {
59       reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_CREATE_SEGMENT);
60       NdbSleep_MilliSleep(timeOutMillis);
61       return false;
62     }
63     _shmSegCreated = true;
64   }
65 
66   if(!_attached){
67     shmBuf = (char*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
68     if(shmBuf == 0){
69       reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_ATTACH_SEGMENT);
70       NdbSleep_MilliSleep(timeOutMillis);
71       return false;
72     }
73     volatile Uint32 * sharedCountAttached =
74       (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
75     ++*sharedCountAttached;
76     _attached = true;
77   }
78 
79   volatile Uint32 * sharedCountAttached =
80     (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
81 
82   if(*sharedCountAttached == 2 && !setupBuffersDone) {
83     setupBuffers();
84     setupBuffersDone=true;
85   }
86   if(*sharedCountAttached > 2) {
87     reportThreadError(remoteNodeId, TE_SHM_DISCONNECT);
88     return false;
89   }
90 
91   if(setupBuffersDone) {
92     NdbSleep_MilliSleep(timeOutMillis);
93     if(*serverStatusFlag==1 && *clientStatusFlag==1)
94       return true;
95   }
96 
97   NdbSleep_MilliSleep(timeOutMillis);
98   return false;
99 }
100 
101 bool
connectClient(Uint32 timeOutMillis)102 SHM_Transporter::connectClient(Uint32 timeOutMillis){
103   if(!_shmSegCreated)
104   {
105     char szName[32];
106     sprintf(szName, "ndb%lu", shmKey);
107     hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
108 
109     if(!hFileMapping)
110     {
111       NdbSleep_MilliSleep(timeOutMillis);
112       return false;
113     }
114     _shmSegCreated = true;
115   }
116 
117   if(!_attached){
118     shmBuf = (char*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
119     if(shmBuf == 0){
120       reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_ATTACH_SEGMENT);
121       NdbSleep_MilliSleep(timeOutMillis);
122       return false;
123     }
124     volatile Uint32 * sharedCountAttached =
125       (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
126     ++*sharedCountAttached;
127     _attached = true;
128   }
129 
130   volatile Uint32 * sharedCountAttached =
131     (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
132 
133   if(*sharedCountAttached == 2 && !setupBuffersDone) {
134     setupBuffers();
135     setupBuffersDone=true;
136   }
137 
138   if(setupBuffersDone) {
139     if(*serverStatusFlag==1 && *clientStatusFlag==1)
140       return true;
141   }
142   NdbSleep_MilliSleep(timeOutMillis);
143   return false;
144 
145 }
146 
147 
148 bool
checkConnected()149 SHM_Transporter::checkConnected(){
150   volatile Uint32 * sharedCountAttached =
151     (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
152   if(*sharedCountAttached != 2) {
153     report_error(TE_SHM_DISCONNECT);
154     return false;
155   }
156   return true;
157 }
158 
159 void
disconnectImpl()160 SHM_Transporter::disconnectImpl(){
161   if(_attached) {
162     volatile Uint32 * sharedCountAttached =
163       (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
164 
165     --*sharedCountAttached;
166 
167     if(!UnmapViewOfFile(shmBuf)) {
168       report_error(TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
169       return;
170     }
171 
172     _attached = false;
173     if(!isServer && _shmSegCreated)
174       _shmSegCreated = false;
175   }
176 
177   if(_shmSegCreated){
178     if(!CloseHandle(hFileMapping)) {
179       report_error(TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
180       return;
181     }
182     _shmSegCreated = false;
183   }
184   setupBuffersDone=false;
185 
186 }
187 
188