?? client.cpp
字號:
// Client.cpp : implementation file
//
#include "stdafx.h"
#include "NServer.h"
#include "client.h"
#include "StoreFileRst.h"
#include "UserRst.h"
#include "LogRst.h"
#include "AllIndexRst.h"
#include "RequestRst.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class CNServerApp;
/////////////////////////////////////////////////////////////////////////////
// Client
Cclient::Cclient()
{
m_pNext = NULL;
Status = C_FREE;
m_NWFileName = "";
m_strUserCode = "";
m_strUserName = "";
m_FileLen = 0;
}
Cclient::~Cclient()
{
}
void Cclient::OnReceive( int nErrorCode )
{
CSocket::OnReceive(nErrorCode);
char pBuf[2049];
for(int i=0;i<2048;i++) pBuf[i] = 0;
int nLen = this->Receive(pBuf,2048,0);
pBuf[nLen] = 0;
for(i=0;i<nLen;i++){
pBuf[i] -= 10;
}
CString tempstr = "";
tempstr = (CString)pBuf;
DataOperation(tempstr);
}
bool Cclient::SendData(CString strData)
{
int nLen = strlen(strData);
if( nLen<= 0)
return true;
char pBuf[2049];
pBuf[nLen] = 0;
strcpy(pBuf,strData);
for(int i=0;i<nLen;i++){
pBuf[i] += 10;
}
this->Send(pBuf,nLen,0);
return true;
}
void Cclient::DataOperation(CString strData)
{
int thisLen = strlen(strData);
if(thisLen <= 0) return;
CString strOper = "";
if(this->Status == C_FREE){
if(thisLen >= 7){
strOper = "";
strOper = strData.Left(7);
if(strcmp(strOper,"NewUser") == 0){
strOper = strData.Right(thisLen - 7);
NewUserInfo(strOper);
return;
}
}
if(thisLen >= 7){
strOper = "";
strOper = strData.Left(7);
if(strcmp(strOper,"ChkUser") == 0){
strOper = strData.Right(thisLen - 7);
CheckUser(strOper);
return;
}
}
if(thisLen >= 7){
strOper = "";
strOper = strData.Left(7);
if(strcmp(strOper,"ModUser") == 0){
strOper = strData.Right(thisLen - 7);
ModifyUserInfo(strOper);
return;
}
}
if(thisLen >= 8){
strOper = "";
strOper = strData.Left(8);
if(strcmp(strOper,"ReqSpace") == 0){
strOper = strData.Right(thisLen - 8);
SaveRequest(strOper);
return;
}
}
if(thisLen >= 7){
strOper = "";
strOper = strData.Left(7);
if(strcmp(strOper,"ReqList") == 0){
strOper = strData.Right(thisLen - 7);
GetRequest(strOper);
return;
}
}
if(thisLen >= 11){
strOper = "";
strOper = strData.Left(11);
if(strcmp(strOper,"ReqFileList") == 0){
strOper = strData.Right(thisLen - 11);
StoreFileList(strOper);
return;
}
}
if(thisLen >= 7){
strOper = "";
strOper = strData.Left(7);
if(strcmp(strOper,"DelFile") == 0){
strOper = strData.Right(thisLen - 7);
DeleteFile(strOper);
return;
}
}
if(thisLen >= 8){
strOper = "";
strOper = strData.Left(8);
if(strcmp(strOper,"SendFile") == 0){
strOper = strData.Right(thisLen - 8);
GetFile(strOper);
return;
}
}
if(thisLen >= 7){
strOper = "";
strOper = strData.Left(7);
if(strcmp(strOper,"GetFile") == 0){
strOper = strData.Right(thisLen - 7);
BackSendFile(strOper);
return;
}
}
}
else if(Status == C_GETFILE){
if(strcmp(strData,"EndSendFile") == 0){
EndGetFile();
return;
}
WriteData(strData);
return;
}
}
void Cclient::WriteData(CString strData)
{
int nLen = strlen(strData);
if( nLen <= 0) return;
m_FileLen += nLen;
try{
m_NWFile.Write(strData , nLen);
}
catch(...){;}
}
void Cclient::NewUserInfo(CString strData)//#用戶名稱#用戶描述#
{
CString dUsername = "";
CString dUserDesc = "";
CString yData = strData;
if(yData.Find("#") == -1 || yData.GetLength() <= 3 )
{
SendData("BadData");
return;
}
yData = yData.Right(yData.GetLength() - 1);
int pNum = yData.Find("#");
if(pNum == -1)
{
SendData("BadData");
return;
}
dUsername = yData.Left(pNum);
yData = yData.Right(pNum + 1);
pNum = yData.Find("#");
if(pNum == -1)
{
SendData("BadData");
return;
}
dUserDesc = yData.Left(pNum);
if(strlen(dUsername) <= 0)
{
SendData("BadData");
return;
}
((CNServerApp *)AfxGetApp())->AddLog("",dUsername,"新用戶注冊");
try{
int nIndex = 0;
CAllIndexRst dIndex;
if(dIndex.IsOpen())
dIndex.Close();
dIndex.m_strFilter = "IndexType = 104";
dIndex.Open();
if(!dIndex.IsEOF()){
nIndex = dIndex.m_IndexValue + 1;
dIndex.Edit();
dIndex.m_IndexValue = nIndex;
dIndex.Update();
}
else{
nIndex = 100;
dIndex.AddNew();
dIndex.m_IndexValue = nIndex;
dIndex.Update();
}
dIndex.Close();
if(nIndex <= 0 )
{
SendData("DoFail");
return;
}
CUserRst dUser;
CTime tm = CTime::GetCurrentTime();
CString strTm = "";
strTm.Format("%04d-%02d-%02d %02d:%02d:%02d", tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond());
if(dUser.IsOpen())
dUser.Close();
dUser.m_strFilter = "1=0";
dUser.Open();
dUser.AddNew();
dUser.m_UserIndex = nIndex;
dUser.m_UserName = dUsername;
dUser.m_UserDesc = dUserDesc;
dUser.m_RegTime = strTm;
dUser.m_MaxSpace = 10;
dUser.m_NowSpace = 0;
dUser.m_IsLock = 0;
dUser.Update();
dUser.Close();
strTm.Format("NewUserCode#%d#",nIndex);
SendData(strTm);
}
catch(...){
SendData("DoFail");
}
}
void Cclient::ModifyUserInfo(CString strData)//#用戶編號#用戶名稱#用戶描述#
{
CString dUsercode = "";
CString dUsername = "";
CString dUserDesc = "";
CString yData = strData;
if(yData.Find("#") == -1 || yData.GetLength() <= 4 )
{
SendData("BadData");
return;
}
yData = yData.Right(yData.GetLength() - 1);
int pNum = yData.Find("#");
if(pNum == -1)
{
SendData("BadData");
return;
}
dUsercode = yData.Left(pNum);
yData = yData.Right(yData.GetLength() - pNum - 1);
pNum = yData.Find("#");
if(pNum == -1)
{
SendData("BadData");
return;
}
dUsername = yData.Left(pNum);
yData = yData.Right(yData.GetLength() - pNum - 1);
if(strlen(yData) > 0)
yData.Replace("#","");
dUserDesc = yData;
if(strlen(dUsercode) <= 0 || strlen(dUsername) <= 0)
{
SendData("BadData");
return;
}
((CNServerApp *)AfxGetApp())->AddLog(dUsercode,dUsername,"用戶信息修改");
try{
CUserRst dUser;
CString strFl = "";
if(dUser.IsOpen())
dUser.Close();
strFl.Format("UserIndex = %s",dUsercode);
dUser.m_strFilter = strFl;
dUser.Open();
if(!dUser.IsEOF()){
dUser.Edit();
dUser.m_UserName = dUsername;
dUser.m_UserDesc = dUserDesc;
dUser.Update();
}
else{
SendData("NoThisUser");
return;
}
dUser.Close();
SendData("ModUserOk");
}
catch(...){
SendData("DoFail");
}
}
void Cclient::StoreFileList(CString strData)//#UserIndex#
{
((CNServerApp *)AfxGetApp())->AddLog(m_strUserCode,m_strUserName,"獲取存儲文件列表");
CString yData = strData;
if(yData.Find("#") == -1 || yData.GetLength() <= 2 )
{
SendData("BadData");
return;
}
yData.Replace("#","");
if(yData.GetLength() <= 0 )
{
SendData("BadData");
return;
}
try{
CStoreFileRst dFile;
if(dFile.IsOpen())
dFile.Close();
CString strFl = "";
strFl.Format("StoreUserCode = %s" , yData );
dFile.m_strFilter = strFl;
dFile.Open();
if(dFile.IsEOF()){
SendData("NoThisFile");
dFile.Close();
return;
}
while(!dFile.IsEOF()){
CString strData = "";
strData.Format("#%d#%s#%d#%s#",dFile.m_FileIndex,dFile.m_FileName,dFile.m_FileSpace,dFile.m_StoreTime);
SendData(strData);
dFile.MoveNext();
}
dFile.Close();
SendData("FileListEnd");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -