封装的内存映像文件.

来源:互联网 发布:傲易棋牌数据库解密 编辑:程序博客网 时间:2024/04/30 13:38

 #pragma once
#ifndef TESTTUNNEL_
#define TESTTUNNEL_
#include <windows.h>
#include <iostream>
using namespace std;
#define TIMEOUT_NUM 5
#define MEMORY_CREATE_ERROR 3
#define MEMORY_READ_TIMEOUT 4
#define MEMORY_READ_SUCCESS  5
#define IP_ADDRESS_MAXLENGTH  16
#define PORT_MAXLENGTH  10
#define TUNNAL_NORMAL_WORK 1 //通道正常工作
#define TUNNAL_ABNORMAL_WORK 0 // 通道不正常工作
class TestTunnel
{
 public:
  TestTunnel ()
  {
   m_hFileName = NULL;
   lpData = NULL;
  }
  ~TestTunnel ()
  {
   DelMemFile();
  }
  BOOL InitalizeMemFile()
  {
   m_hFileName = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write permission
    FALSE,                             // Do not inherit the name
    _T("Global//MYSHARE"));
   if (m_hFileName == NULL)
   {
    DWORD errCode = 0 ;
    errCode = GetLastError();
    SECURITY_ATTRIBUTES sa;
    SECURITY_DESCRIPTOR sd;

    InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(&sd,TRUE,NULL,FALSE);
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    m_hFileName = CreateFileMapping(INVALID_HANDLE_VALUE ,  &sa, PAGE_READWRITE , 0, 0x0100 , _T("Global//MYSHARE") );
    if( m_hFileName == NULL )
    {
     errCode = GetLastError();
     int iii = 0;
    }
   }
   lpData = (LPSTR) MapViewOfFile(m_hFileName, // handle to mapping object
        FILE_MAP_ALL_ACCESS,               // read/write permission
        0,                                 // max. object size
        0,                                 // size of hFile
        0);                                // map entire file
   if (lpData == NULL)
   {
    return FALSE;
   }
   return TRUE;
  }
  int ReadMemFile( BYTE &feedback , char *pServerIpAdd, char *pPort,
   int iIpAddLen = IP_ADDRESS_MAXLENGTH , int iPortLen =  PORT_MAXLENGTH )
  {
   if( m_hFileName == NULL ||  lpData == NULL)
    return MEMORY_CREATE_ERROR;
   int timeout_num = TIMEOUT_NUM;
   while( strlen( lpData ) == 0 || timeout_num == 0 )
   {
    Sleep( 1000 );
    timeout_num = timeout_num -1;
    if( timeout_num == 0 )
     return MEMORY_READ_TIMEOUT ;
   }
   int tmp_size = strlen( lpData );
   char *tmp = new char[tmp_size+1];
   char *tmp_port = new char[tmp_size+1];
   char *tmp_ip_add = new char[tmp_size+1];
   memset( tmp , 0 , tmp_size+1 );
   memcpy( tmp , lpData , tmp_size );
   tmp[tmp_size] = '/0';

   int port = 0;
   int idx = 0;
   
    if( tmp[0] == '0' )
    {
     feedback = TUNNAL_ABNORMAL_WORK;  
    }
    if ( tmp[0] == '1' )
    {
     feedback = TUNNAL_NORMAL_WORK;
    }
    while( idx != tmp_size )
    {
     if( tmp[idx] == ':' )
     {
      memset( tmp_port , 0 , tmp_size+1 );
      strncpy( tmp_port , &tmp[idx+1] , tmp_size-idx-1 );
      memset( tmp_ip_add , 0, tmp_size+1 );
      strncpy(tmp_ip_add  , &tmp[1] , idx-1 );
      break;
     }
     idx++;
    }
    _ASSERT( tmp_size <(IP_ADDRESS_MAXLENGTH + PORT_MAXLENGTH +2) );
    memset( pPort , 0 , PORT_MAXLENGTH );
    strncpy(  pPort ,  tmp_port , PORT_MAXLENGTH-1 );
    memset( pServerIpAdd , 0 , IP_ADDRESS_MAXLENGTH );
    strncpy( pServerIpAdd , tmp_ip_add , IP_ADDRESS_MAXLENGTH-1 );
   delete[] tmp;
   tmp = NULL;
   delete[] tmp_port;
   tmp_port = NULL;
   delete[] tmp_ip_add ;
   tmp_ip_add  = NULL;  
   return MEMORY_READ_SUCCESS;
  }
  BOOL ProcessMemFile();
  BOOL WriteMemFile(const BYTE &feedback , const char* pServerIpAdd , const char *pPort )
  {
   int serverIpAdd_len =  strlen( pServerIpAdd );
   int port_len = strlen( pPort );
   if( serverIpAdd_len == 0 ||  port_len == 0 )
    return FALSE;
   int buffer_size = sprintf( lpData , "%d%s:%s", feedback ,pServerIpAdd , pPort );
   if( buffer_size == (2+ serverIpAdd_len+port_len ))
   {
    return TRUE;
   }
   return FALSE;
  }
  void DelMemFile()
  {
   if( lpData != NULL )
   {
    UnmapViewOfFile( lpData );
    lpData = NULL;
   }
   if( m_hFileName != NULL )
   {
    CloseHandle( m_hFileName );
   }
  }
 private:
  HANDLE m_hFileName;
  LPSTR lpData;
};
#endif