SHM_Transporter.win32.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. #include <ndb_global.h>
  14. #include "SHM_Transporter.hpp"
  15. #include "TransporterInternalDefinitions.hpp"
  16. #include <TransporterCallback.hpp>
  17. #include <NdbSleep.h>
  18. #include <NdbOut.hpp>
  19. #include <windows.h>
  20. bool
  21. SHM_Transporter::connectServer(Uint32 timeOutMillis){
  22.   if(!_shmSegCreated)
  23.   {
  24.     char szName[32];
  25.     sprintf(szName, "ndb%lu", shmKey);
  26.     hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, 
  27.      0, 
  28.      PAGE_READWRITE, 
  29.      0, 
  30.      shmSize, 
  31.      szName);
  32.     if(!hFileMapping)
  33.     {
  34.       reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_CREATE_SEGMENT);
  35.       NdbSleep_MilliSleep(timeOutMillis);
  36.       return false;
  37.     }
  38.     _shmSegCreated = true;
  39.   }
  40.   if(!_attached){
  41.     shmBuf = (char*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  42.     if(shmBuf == 0){
  43.       reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_ATTACH_SEGMENT);
  44.       NdbSleep_MilliSleep(timeOutMillis);
  45.       return false;
  46.     }
  47.     volatile Uint32 * sharedCountAttached = 
  48.       (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
  49.     ++*sharedCountAttached;
  50.     _attached = true;
  51.   }
  52.   volatile Uint32 * sharedCountAttached = 
  53.     (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
  54.   if(*sharedCountAttached == 2 && !setupBuffersDone) {
  55.     setupBuffers();
  56.     setupBuffersDone=true;
  57.   }
  58.   if(*sharedCountAttached > 2) {
  59.     reportThreadError(remoteNodeId, TE_SHM_DISCONNECT); 
  60.     return false;
  61.   }
  62.   
  63.   if(setupBuffersDone) {
  64.     NdbSleep_MilliSleep(timeOutMillis);
  65.     if(*serverStatusFlag==1 && *clientStatusFlag==1)
  66.       return true;
  67.   }
  68.   NdbSleep_MilliSleep(timeOutMillis);
  69.   return false;
  70. }
  71. bool
  72. SHM_Transporter::connectClient(Uint32 timeOutMillis){
  73.   if(!_shmSegCreated)
  74.   {
  75.     char szName[32];
  76.     sprintf(szName, "ndb%lu", shmKey);
  77.     hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
  78.     if(!hFileMapping)
  79.     {
  80.       NdbSleep_MilliSleep(timeOutMillis);
  81.       return false;
  82.     }
  83.     _shmSegCreated = true;
  84.   }
  85.   if(!_attached){
  86.     shmBuf = (char*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  87.     if(shmBuf == 0){
  88.       reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_ATTACH_SEGMENT);
  89.       NdbSleep_MilliSleep(timeOutMillis);
  90.       return false;
  91.     }
  92.     volatile Uint32 * sharedCountAttached = 
  93.       (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
  94.     ++*sharedCountAttached;
  95.     _attached = true;
  96.   }
  97.   
  98.   volatile Uint32 * sharedCountAttached = 
  99.     (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
  100.   if(*sharedCountAttached == 2 && !setupBuffersDone) {
  101.     setupBuffers();
  102.     setupBuffersDone=true;
  103.   }
  104.   if(setupBuffersDone) {
  105.     if(*serverStatusFlag==1 && *clientStatusFlag==1)
  106.       return true;
  107.   }
  108.   NdbSleep_MilliSleep(timeOutMillis);
  109.   return false;
  110. }
  111. bool
  112. SHM_Transporter::checkConnected(){
  113.   volatile Uint32 * sharedCountAttached = 
  114.     (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
  115.   if(*sharedCountAttached != 2) {
  116.     reportError(callbackObj, remoteNodeId, TE_SHM_DISCONNECT);
  117.     return false;
  118.   }
  119.   return true;
  120. }
  121. void
  122. SHM_Transporter::disconnectImpl(){
  123.   if(_attached) {
  124.     volatile Uint32 * sharedCountAttached = 
  125.       (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
  126.     
  127.     --*sharedCountAttached;
  128.     if(!UnmapViewOfFile(shmBuf)) {
  129.       reportError(callbackObj, remoteNodeId, TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
  130.       return;
  131.     }
  132.     
  133.     _attached = false;
  134.     if(!isServer && _shmSegCreated)
  135.       _shmSegCreated = false;
  136.   }
  137.   
  138.   if(_shmSegCreated){
  139.     if(!CloseHandle(hFileMapping)) {
  140.       reportError(callbackObj, remoteNodeId, TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
  141.       return;
  142.     }
  143.     _shmSegCreated = false;
  144.   }
  145.   setupBuffersDone=false;
  146. }