PcShare服务端改造

 

(1)合并PcCortr.DLL和PcClient.DLL
(2)将PcClient里的SshWork类和PcCortr里的MyMainTrans合并为PsMainTrans类,实现上线功能
(3)用自定义CFile类代替PcCortr里用到的MFC的CFile类,改造MyAdminTrans和文件相关函数里使用的MFC类,改为SDK
(4)去除不必要的功能,简化服务端

附CFile实现:

#pragma once

#include <fcntl.h> // filecontrol : _O_RDONLY symbol and other stuff
#include <tchar.h> // _fgetts ==> fgets
#include <stdio.h>
#include <io.h>
#include <windows.h>

class CFile
{
// Members
protected:
UINT   m_hFile; // handle to actual file (created by ::CreateFile WIN32 API)
BOOL   m_bCloseOnDelete;
UINT        m_FileLen;

public:
// Flag values
enum OpenFlags {
   modeRead =          0x0000,
   modeWrite =         0x0001,
   modeReadWrite =     0x0002,
   shareCompat =       0x0000,
   shareExclusive =    0x0010,
   shareDenyWrite =    0x0020,
   shareDenyRead =     0x0030,
   shareDenyNone =     0x0040,
   modeNoInherit =     0x0080,
   modeCreate =        0x1000,
   modeNoTruncate =    0x2000,
   typeText =          0x4000, // typeText and typeBinary are used in
   typeBinary =   (int)0x8000 // derived classes only
   };

enum SeekPosition {
   begin = 0x0,
   current = 0x1,
   end = 0x2 };

enum { hFileNull = -1 };

// Constructor/Destructor
public:
CFile()
{
   m_hFile = (UINT) hFileNull;
   m_bCloseOnDelete = FALSE;
   m_FileLen = 0;
}
~CFile()
{
   if (m_hFile != (UINT)hFileNull && m_bCloseOnDelete)
    Close();
}

// Methods
public:
BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags)
{
   // CFile objects are always binary and CreateFile does not need flag
   nOpenFlags &= ~(UINT)typeBinary;

   m_bCloseOnDelete = FALSE;
   m_hFile = (UINT)hFileNull;

   // map read/write mode
   DWORD dwAccess = 0;
   switch (nOpenFlags & 3)
   {
    case modeRead:
     dwAccess = GENERIC_READ;
     break;
    case modeWrite:
     dwAccess = GENERIC_WRITE;
     break;
    case modeReadWrite:
     dwAccess = GENERIC_READ|GENERIC_WRITE;
     break;
    default:
     ; // invalid share mode
   }

   // map share mode
   DWORD dwShareMode = 0;
   switch (nOpenFlags & 0x70)    // map compatibility mode to exclusive
   {
    default:
     ; // invalid share mode?
    case shareCompat:
    case shareExclusive:
     dwShareMode = 0;
     break;
    case shareDenyWrite:
     dwShareMode = FILE_SHARE_READ;
     break;
    case shareDenyRead:
     dwShareMode = FILE_SHARE_WRITE;
     break;
    case shareDenyNone:
     dwShareMode = FILE_SHARE_WRITE|FILE_SHARE_READ;
     break;
   }

   // map modeNoInherit flag
   SECURITY_ATTRIBUTES sa;
   sa.nLength = sizeof(sa);
   sa.lpSecurityDescriptor = NULL;
   sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;

   // map creation flags
   DWORD dwCreateFlag;
   if (nOpenFlags & modeCreate)
   {
    if (nOpenFlags & modeNoTruncate)
     dwCreateFlag = OPEN_ALWAYS;
    else
     dwCreateFlag = CREATE_ALWAYS;
   }
   else
    dwCreateFlag = OPEN_EXISTING;

   // attempt file creation
   HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa,
    dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
   if (hFile == INVALID_HANDLE_VALUE)
    return FALSE;

   m_hFile = (HFILE)hFile;
   m_bCloseOnDelete = TRUE;

   return TRUE;
}

LONG Seek( LONG lOff, UINT nFrom )
{
   if (m_hFile == (UINT)hFileNull) return 0;

   return ::SetFilePointer((HANDLE)m_hFile, lOff, NULL, (DWORD)nFrom);
}

DWORD SeekToBegin()
{
   return Seek(0,CFile::begin);
}
DWORD SeekToEnd()
{
   return Seek(0, CFile::end);
}


UINT Read(void* lpBuf, UINT nCount)
{
   if (nCount == 0) return 0;   // avoid Win32 "null-read"

   DWORD dwRead = 0;
  
   ::ReadFile((HANDLE)m_hFile, lpBuf, nCount, &dwRead, NULL);

   return (UINT)dwRead;
}

void Write( const void* lpBuf, UINT nCount )
{
   if (m_hFile == (UINT)hFileNull) return;

   if (nCount == 0)
    return;     // avoid Win32 "null-write" option

   DWORD nWritten;
     ::WriteFile((HANDLE)m_hFile, lpBuf, nCount, &nWritten, NULL);
   m_FileLen +=nWritten;
}

void Flush()
{
   if (m_hFile == (UINT)hFileNull) return;

   ::FlushFileBuffers((HANDLE)m_hFile);
}

void Close()
{
   if (m_hFile != (UINT)hFileNull)
    ::CloseHandle((HANDLE)m_hFile);

   m_hFile = (UINT) hFileNull;
   m_bCloseOnDelete = FALSE;
}

UINT GetLength()
{
   return m_FileLen;
}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值