⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sh_pipe.cpp

📁 rsa算法打的一个包
💻 CPP
字号:
// SH_Pipe.cpp: implementation of the SH_Pipe class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SH_Pipe.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define SH_PIPE_BUFSIZ 4096

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SH_Pipe::SH_Pipe()
{
    m_hPipe = INVALID_HANDLE_VALUE;
    
    memset(&m_RO,0,sizeof(OVERLAPPED));

	m_RO.hEvent = CreateEvent(NULL, true, false, NULL);
}

SH_Pipe::~SH_Pipe()
{
    Close();
    
    if(m_RO.hEvent != INVALID_HANDLE_VALUE)
		CloseHandle(m_RO.hEvent);

}

HANDLE SH_Pipe::GetSafeHandle() const
{
    return m_hPipe;
}

VOID SH_Pipe::Close()
{
    if(m_hPipe != INVALID_HANDLE_VALUE)
    {
        CloseHandle(m_hPipe);
        m_hPipe = NULL;
    }
}
BOOL SH_Pipe::Listen(const char * name, int timeout)
{
    m_strPipeName = name;
    m_nTimeout    = timeout;

    m_hPipe = CreateNamedPipe(name,                    
          PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,      
          PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT,
          PIPE_UNLIMITED_INSTANCES,
          SH_PIPE_BUFSIZ, 
          SH_PIPE_BUFSIZ,          
          timeout,
          NULL);   
    
    if(m_hPipe == INVALID_HANDLE_VALUE)
    {
        TRACE("SH_Pipe create pipe failed.Error no is %d\n",GetLastError());
        return FALSE;
    }

    if(ConnectNamedPipe(m_hPipe,&m_RO))
        return FALSE;

    switch (GetLastError()) 
    { 
      case ERROR_IO_PENDING: 
         break; 
      case ERROR_PIPE_CONNECTED: 
         if (SetEvent(m_RO.hEvent)) 
            break; 
      default: 
          TRACE("SH_Pipe create pipe failed.Error no is %d\n",GetLastError());
          return FALSE;
    } 
    return TRUE;
}

BOOL SH_Pipe::Connect(const char *name, int timeout)
{
     m_strPipeName = name;
     m_nTimeout    = timeout;
    
     if(!WaitNamedPipe((LPCTSTR)m_strPipeName,m_nTimeout))
     {
        TRACE("SH_Pipe fail to conenct to server.Error no is %d\n",GetLastError());
        return FALSE;
     }

     m_hPipe = CreateFile(name,   
                        GENERIC_READ|GENERIC_WRITE, 
                        0,              
                        NULL,           
                        OPEN_EXISTING,  
                        0,              
                        NULL);          

     if(m_hPipe == INVALID_HANDLE_VALUE)
         return FALSE;
     
     if(GetLastError() == ERROR_PIPE_BUSY) 
     {
         TRACE("SH_Pipe Server is busy\n"); 
         return FALSE;
     }

     return TRUE;
}

LPCTSTR SH_Pipe::GetPipeName(VOID)
{
    return (LPCTSTR)m_strPipeName;
}
int     SH_Pipe::GetTimeout(VOID) const
{
    return m_nTimeout;
}

int SH_Pipe::Read(char *buffer,int len)
{
    DWORD dwBytesRead = 0;
    
    if(!ReadFile(m_hPipe,buffer,len,&dwBytesRead,&m_RO))
    {
        if(GetLastError() == ERROR_MORE_DATA)
            OutputDebugString("SH_Pipe Receive buffer is too small.Larger buffer expected.\n");
        TRACE("SH_Pipe read data failed.Error no is %d\n",GetLastError());
    }
    return dwBytesRead;
}

int SH_Pipe::Write(const char *buffer, int len)
{
    DWORD dwBytesRead = 0;
    
    if(!WriteFile(m_hPipe,buffer,len,&dwBytesRead,&m_RO))
    {
        TRACE("SH_Pipe write data failed.Error no is %d\n",GetLastError());
    }
    return dwBytesRead;
}

int SH_Pipe::PseudoRead(char *buffer, int len)
{
    DWORD BytesRead = 0;
    DWORD TotalBytesAvail = 0;
    DWORD BytesLeftThisMessage = 0;
    if(!Peek(buffer,len,&BytesRead,&TotalBytesAvail,&BytesLeftThisMessage))
    {
        TRACE("SH_Pipe pseudo read data failed.Error no is %d\n",GetLastError());
    }
    return BytesRead;
}

int SH_Pipe::GetTotalLength()
{

    DWORD BytesRead            = 0;
    DWORD TotalBytesAvail      = 0;
    DWORD BytesLeftThisMessage = 0;
    char  buffer[1];

    if(!Peek(buffer,1,&BytesRead,&TotalBytesAvail,&BytesLeftThisMessage))
    {
        TRACE("SH_Pipe pseudo read data failed.Error no is %d\n",GetLastError());
    }
    return TotalBytesAvail;
}
int SH_Pipe::GetCurrentLength()
{

    DWORD BytesRead            = 0;
    DWORD TotalBytesAvail      = 0;
    DWORD BytesLeftThisMessage = 0;
    char  buffer[1];

    if(!Peek(buffer,1,&BytesRead,&TotalBytesAvail,&BytesLeftThisMessage))
    {
        TRACE("SH_Pipe pseudo read data failed.Error no is %d\n",GetLastError());
        TotalBytesAvail = 0;
    }
    else
        TotalBytesAvail += 1;

    return TotalBytesAvail;
}

BOOL SH_Pipe::SetMode(DWORD & dwMode)
{
    return SetNamedPipeHandleState(m_hPipe,&dwMode,NULL,NULL);
}

DWORD SH_Pipe::GetMode(VOID) const
{
    return m_iMode;
}

BOOL SH_Pipe::Peek(LPVOID pBuffer,DWORD len,LPDWORD BytesRead,LPDWORD BytesAvail,LPDWORD BytesLeftThisMessage)
{
    return PeekNamedPipe(m_hPipe,pBuffer,len,BytesRead,BytesAvail,BytesLeftThisMessage);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -