Files
DBService/ComposeService/MessageHandle.cpp
T
2026-07-09 11:27:59 +08:00

2821 lines
79 KiB
C++

#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#else
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <semaphore.h>
// #include <fcntl.h>
#include <sys/file.h>
// #include <signal.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "typedef.h"
#include "thread.h"
#include "debug.h"
#include "MessageHandle.h"
#include "DBInterface.h"
#include "DataBuffers.h"
#include "ResponseManager.h"
#define AUTHORITY_CHECKER 0x02
#define AUTHORITY_CREATOR 0x1000
#define AUTHORITY_VIP 0x0F
extern TCLIENTVERSION* g_pVersionClient;
extern int g_nClientTypeNum;
extern TTESTVERSION* g_pVersionClientTest;
extern int g_nClientTestNum;
// extern HANDLE g_hEvClientCheck;
extern CSema * g_pSemaClientCheck;
extern CDataBufferManager* g_pDataBuffers;
extern CResponseManager* g_pResponses;
extern char g_strStyleFileDir[];
extern char g_strWebFileDir[];
extern char g_strMusicFileDir[];
extern char g_strMusicLocation[];
extern int g_aStyleRecomList[MAX_STYLE_NUM+1];
extern TQNAITEM g_lstCommonQnA[];
extern TNOTICEITEM g_lstPublicNotice[];
extern int g_nCommonQnANum, g_nPublicNoticeNum;
// long g_lMemUsed = 0;
char g_strClients[][16] = {
"PC Home",
"CZ Andriod",
"CZ iOS",
"Ac Andriod",
"Ac iOS",
"unknown",
"PC Pro",
"StyleChecker",
"PC Pro_Lite"
};
//////////////////////////////////////////////////////
// 统计 PC pro 函数
// #include <set>
// set<UINT> pro826, proOth;
// void GetProCount(int& n826, int& nOth)
// {
// n826 = pro826.size();
// nOth = proOth.size();
// pro826.clear();
// proOth.clear();
// }
// void SetProCount(UINT ip, WORD ver)
// {
// if(ver==826){
// pro826.insert(ip);
// }
// else{
// if(ver==125){
// pro826.erase(ip);
// }
// proOth.insert(ip);
// }
// }
//
/////////////////////////////////////////////////////
UINT ShiftInt( UINT data, bool bReverse, int shNum )
{
int shift = bReverse ? (32-shNum) : shNum;
UINT mR = (1<<(32-shift)) - 1,
mL = ((UINT)-1) - mR;
return (UINT)( ((data & mL)>>(32-shift)) | ((data & mR)<<shift) );
}
BYTE ShiftByte( BYTE data, bool bReverse, int shNum )
{
int shift = bReverse ? (8-shNum) : shNum;
BYTE mR = (1<<(8-shift)) - 1,
mL = ((BYTE)-1) - mR;
return (BYTE)( ((data & mL)>>(8-shift)) | ((data & mR)<<shift) );
}
bool HandleRequest( TNETMESSAGE* pRequestMsg, TNETMESSAGE* pResponseMsg, UINT ip, USHORT port, bool bUDP )
{
int ret = 0, iClientType = -1, iMsgVer = 0;
pResponseMsg->nSequenceNum = pRequestMsg->nSequenceNum;
pResponseMsg->eMessageType = pRequestMsg->eMessageType + 1;
if( pRequestMsg->eMessageType==e_connectRequest ){
return true;
}
iMsgVer = pRequestMsg->iExtra & 0xFF00;
if(iMsgVer<0x0A00 && pRequestMsg->eMessageType!=e_versionRequest){
pResponseMsg->iExtra = e_UpdateMandatory;
return true;
}
else if( iMsgVer>=0x0100 && iMsgVer<=PROTOCOL_VERSION ){
iClientType = pRequestMsg->iExtra & 0x00FF;
// if( (iClientType==e_Ac_Windows || iClientType==e_Pro_Windows ) && iMsgVer<0x0A00 ){
// pResponseMsg->iExtra = e_UpdateMandatory;
// return true;
// }
}
// else{
// iMsgVer = 0;
// }
// if( iMsgVer==0 && pRequestMsg->iExtra!=0x6E01 && pRequestMsg->eMessageType!=e_versionRequest ){
// pResponseMsg->iExtra = e_UpdateMandatory;
// return true;
// }
if( pRequestMsg->eMessageType==e_logoffRequest ){
TLogoffRequest * pOffReq = (TLogoffRequest*)pRequestMsg->byMessageBody;
UINT idUser;
char idDevice[18] = "";
if(iMsgVer<0x0D00){
TLogoffRequest0 * pOffReq = (TLogoffRequest0*)pRequestMsg->byMessageBody;
sprintf( idDevice, "%02X%02X%02X%02X%02X%02X", (UCHAR)pOffReq->idDevice[0], (UCHAR)pOffReq->idDevice[1],
(UCHAR)pOffReq->idDevice[2], (UCHAR)pOffReq->idDevice[3],
(UCHAR)pOffReq->idDevice[4], (UCHAR)pOffReq->idDevice[5] );
idUser = pOffReq->idUser;
}
else{
TLogoffRequest * pOffReq = (TLogoffRequest*)pRequestMsg->byMessageBody;
idUser = pOffReq->idUser;
sprintf( idDevice, "%02X%02X%02X%02X%02X%02X", (UCHAR)pRequestMsg->idDevice[0], (UCHAR)pRequestMsg->idDevice[1],
(UCHAR)pRequestMsg->idDevice[2], (UCHAR)pRequestMsg->idDevice[3],
(UCHAR)pRequestMsg->idDevice[4], (UCHAR)pRequestMsg->idDevice[5] );
}
f_DB_Logoff( pOffReq->idUser, idDevice );
return false;
}
if( pRequestMsg->eMessageType!=e_dataSendRequest
&& pRequestMsg->eMessageType!=e_dataReceiveRequest ){
TNETMESSAGE* pRspBuffer = g_pResponses->GetResponse( pRequestMsg );
if( pRspBuffer ){
memcpy( pResponseMsg, pRspBuffer, pRspBuffer->nLength+14 );
// char *pb=(char *)&(ip);
// unsigned b1=(unsigned char)(*pb);
// unsigned b2=(unsigned char)(*(pb+1));
// unsigned b3=(unsigned char)(*(pb+2));
// unsigned b4=(unsigned char)(*(pb+3));
// DebugPrint( "重复响应:%d.%d.%d.%d:%d, msg 0x%X", b1, b2, b3, b4, port, pRequestMsg->eMessageType );
return true;
}
}
int iRespExtra = 0;
switch( pRequestMsg->eMessageType ){
case e_versionRequest:
{
TVERSION version;
// char idDevice[18] = "";
bool bOldClient = false;
if(iMsgVer>=0x0D00){
TVersionRequest * pVReq = (TVersionRequest*)pRequestMsg->byMessageBody;
version.majorVersion = HIWORD(pVReq->dwVersionMS);
version.minorVersion = LOWORD(pVReq->dwVersionMS);
version.modifyVersion = HIWORD(pVReq->dwVersionLS);
version.buildVersion = LOWORD(pVReq->dwVersionLS);
}
else if( iMsgVer>=0x0900 ){
TVersionRequest2 * pVReq = (TVersionRequest2*)pRequestMsg->byMessageBody;
version.majorVersion = HIWORD(pVReq->dwVersionMS);
version.minorVersion = LOWORD(pVReq->dwVersionMS);
version.modifyVersion = HIWORD(pVReq->dwVersionLS);
version.buildVersion = LOWORD(pVReq->dwVersionLS);
}
else{
if( pRequestMsg->nLength!=sizeof(TVersionRequest1) )
bOldClient = true;
if( bOldClient ){
TVersionRequest0 * pVReq = (TVersionRequest0*)pRequestMsg->byMessageBody;
version.majorVersion = HIWORD(pVReq->dwVersionMS);
version.minorVersion = LOWORD(pVReq->dwVersionMS);
version.modifyVersion = HIWORD(pVReq->dwVersionLS);
version.buildVersion = LOWORD(pVReq->dwVersionLS);
}
else{
TVersionRequest1 * pVReq = (TVersionRequest1*)pRequestMsg->byMessageBody;
version.majorVersion = HIWORD(pVReq->dwVersionMS);
version.minorVersion = LOWORD(pVReq->dwVersionMS);
version.modifyVersion = HIWORD(pVReq->dwVersionLS);
version.buildVersion = LOWORD(pVReq->dwVersionLS);
}
}
if( iClientType<0 ){
iClientType = version.minorVersion;
}
// char bufCT[40];
// if( iClientType==e_Ac_Windows )
// strcpy( bufCT, "PC Home" );
// else if( iClientType==e_Ac_Android )
// strcpy( bufCT, "Ac Andriod" );
// else if( iClientType==e_Ac_iOS )
// strcpy( bufCT, "Ac iOS" );
// else if( iClientType==e_CZ_Android )
// strcpy( bufCT, "CZ Andriod" );
// else if( iClientType==e_CZ_iOS )
// strcpy( bufCT, "CZ iOS" );
// else if( iClientType==e_SM_Checker )
// strcpy( bufCT, "StyleChecker" );
// else if( iClientType==e_Pro_Windows )
// strcpy( bufCT, "PC Pro" );
// else if( iClientType==e_Pro_Lite )
// strcpy( bufCT, "PC Pro_Lite" );
// else
// strcpy( bufCT, "Unknown" );
char* sCT = iClientType<=8 ? g_strClients[iClientType] : g_strClients[5];
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
DebugPrint( "ACCESS %d.%d.%d.%d:%d %s\n\t\t\t [%s] %d.%d.%d.%d", b1, b2, b3, b4, port,
bUDP?"UDP":"TCP", sCT,
version.majorVersion, version.minorVersion,
version.modifyVersion, version.buildVersion );
//////////////////////////////////////////////////////
// 统计 PC pro 函数
// if(iClientType==e_Pro_Windows)
// SetProCount(ip, version.buildVersion);
//
//////////////////////////////////////////////////////
//先判断是否为待/新发布版
bool bNewRelease = false,
bLastVersion = false;
if(g_pSemaClientCheck)
g_pSemaClientCheck->ActP();
TCLIENTVERSION * pLatest = NULL;
for( int i=0; i<g_nClientTypeNum; i++ ){
if( g_pVersionClient[i].iClientType==iClientType ){
TVERSION& ver = g_pVersionClient[i].swInfo.version;
if( version.majorVersion == ver.majorVersion
&& version.minorVersion == ver.minorVersion
&& version.modifyVersion == ver.modifyVersion
&& version.buildVersion == ver.buildVersion ){
bLastVersion = true;
}
else if( version.majorVersion > ver.majorVersion
|| version.majorVersion == ver.majorVersion
&& (version.minorVersion > ver.minorVersion
|| version.minorVersion == ver.minorVersion
&& (version.modifyVersion > ver.modifyVersion
|| version.modifyVersion == ver.modifyVersion
&& version.buildVersion > ver.buildVersion ))){
bNewRelease = true;
}
pLatest = &g_pVersionClient[i];
break;
}
}
if(g_pSemaClientCheck)
g_pSemaClientCheck->ActV();
if( bNewRelease ){
if(g_pSemaClientCheck)
g_pSemaClientCheck->ActP();
// WaitForSingleObject( g_hEvClientCheck, INFINITE );
bool bMatch = false;
for( int i=0; i<g_nClientTestNum; i++ ){
if( g_pVersionClientTest[i].iClientType==iClientType ){
TVERSION& ver = g_pVersionClientTest[i].version;
if( version.majorVersion==ver.majorVersion
&& version.minorVersion==ver.minorVersion
&& version.modifyVersion==ver.modifyVersion ) //不判断构建版本
// && version.buildVersion==ver.buildVersion )
bMatch = true;
break;
}
}
if(g_pSemaClientCheck)
g_pSemaClientCheck->ActV();
// SetEvent( g_hEvClientCheck );
if( bMatch ){
pResponseMsg->iExtra = e_UpdateNoNeed;
break;
}
else{
//pResponseMsg->iExtra = e_UpdateMandatory;
ret = e_UpdateMandatory;
}
}
else{
ret = f_DB_GetClientUpdateFlag( iClientType, &version, NULL );
}
if( bLastVersion ){ //最新版只需要通过 f_DB_GetClientUpdateFlag 查询和统计
pResponseMsg->iExtra = e_UpdateNoNeed;
break;
}
else if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
break;
}
else{
if( ret==1 ){
pResponseMsg->iExtra = e_UpdateOptional;
}
else{ //无论非法与否均强制更新
pResponseMsg->iExtra = e_UpdateMandatory;
DebugPrint( "UpdateMandatory!" );
}
if(pLatest){
int lenDesc = strlen(pLatest->swInfo.strDescribe);
if( bOldClient ){
pResponseMsg->nLength = sizeof(TVersionResponse0) - MAX_DESCRIB_LENGTH;
TVersionResponse0* pVRsp = (TVersionResponse0*)pResponseMsg->byMessageBody;
pVRsp->nAvailibleNum = 0;
pVRsp->dwVersionMS = MAKELONG( pLatest->swInfo.version.minorVersion, pLatest->swInfo.version.majorVersion );
pVRsp->dwVersionLS = MAKELONG( pLatest->swInfo.version.buildVersion, pLatest->swInfo.version.modifyVersion );
pVRsp->tIssueDate = pLatest->swInfo.tIssueDate;
pVRsp->nFileSize = pLatest->swInfo.nFileSize;
pVRsp->nDescribLen = lenDesc;
strncpy( pVRsp->strDescrib, pLatest->swInfo.strDescribe, lenDesc );
}
else{
pResponseMsg->nLength = sizeof(TVersionResponse) - MAX_DESCRIB_LENGTH;
TVersionResponse* pVRsp = (TVersionResponse*)pResponseMsg->byMessageBody;
pVRsp->dwVersionMS = MAKELONG( pLatest->swInfo.version.minorVersion, pLatest->swInfo.version.majorVersion );
pVRsp->dwVersionLS = MAKELONG( pLatest->swInfo.version.buildVersion, pLatest->swInfo.version.modifyVersion );
pVRsp->tIssueDate = pLatest->swInfo.tIssueDate;
pVRsp->nFileSize = pLatest->swInfo.nFileSize;
pVRsp->nDescribLen = lenDesc;
strncpy( pVRsp->strDescrib, pLatest->swInfo.strDescribe, lenDesc );
}
pResponseMsg->nLength += lenDesc;
}
}
}
break;
case e_styleUpdateRequest:
{
if( iMsgVer<0x0700 ){
TStyleUpdateRequest0 * pSUReq = (TStyleUpdateRequest0*)pRequestMsg->byMessageBody;
TStyleUpdateResponse0 * pSURsp = (TStyleUpdateResponse0*)pResponseMsg->byMessageBody;
int nUpdateNum = 10000;
int tUpdateDate = pSUReq->tLastUpdateDate;
if( iMsgVer<=0x0500 ){
TSTYLEUPDATE0 aUpdate[10000];
if( f_DB_GetStyleUpdateList( pSUReq->iClientType, &tUpdateDate, &nUpdateNum, aUpdate )<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
pSURsp->tUpdateDate = tUpdateDate;
pSURsp->nAvailibleNum = nUpdateNum;
pSURsp->nDataSize = sizeof(TSTYLEUPDATE0) * nUpdateNum;
if( nUpdateNum>0 ){
char* pData = new char[pSURsp->nDataSize];
int idData = g_pDataBuffers->NewDataBuffer( pSUReq->idUser, DTYPE_STYLESTATE, pSURsp->nDataSize, pData, pSUReq->iClientType );
if(idData>0){ //正常创建传输数据
pSURsp->idData = idData;
memcpy( pData, (void*)aUpdate, pSURsp->nDataSize );
DebugPrint( "更新风格数:%d个", nUpdateNum );
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pSURsp->nDataSize = 0;
pSURsp->idData = 0;
}
else{ //重复创建
pSURsp->idData = -idData;
}
delete[] pData;
}
}
else{
pSURsp->idData = 0;
}
pResponseMsg->nLength = sizeof(TStyleUpdateResponse);
}
}
else{
TSTYLEUPDATE aUpdate[10000];
if( f_DB_GetStyleUpdateListNew( &tUpdateDate, &nUpdateNum, aUpdate )<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
pSURsp->tUpdateDate = tUpdateDate;
pSURsp->nAvailibleNum = nUpdateNum;
pSURsp->nDataSize = sizeof(TSTYLEUPDATE) * nUpdateNum;
if( nUpdateNum>0 ){
char* pData = new char[pSURsp->nDataSize];
int idData = g_pDataBuffers->NewDataBuffer( pSUReq->idUser, DTYPE_STYLESTATE, pSURsp->nDataSize, pData, pSUReq->iClientType );
if(idData>0){ //正常创建传输数据
pSURsp->idData = idData;
memcpy( pData, (void*)aUpdate, pSURsp->nDataSize );
DebugPrint( "更新风格数:%d个", nUpdateNum );
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pSURsp->nDataSize = 0;
pSURsp->idData = 0;
}
else{ //重复创建
pSURsp->idData = -idData;
}
delete[] pData;
}
}
else{
pSURsp->idData = 0;
}
pResponseMsg->nLength = sizeof(TStyleUpdateResponse);
}
}
}
else{
TStyleUpdateRequest * pSUReq = (TStyleUpdateRequest*)pRequestMsg->byMessageBody;
TStyleUpdateResponse* pSURsp = (TStyleUpdateResponse*)pResponseMsg->byMessageBody;
if( pSUReq->iClientType==e_SM_Checker ){
ret = f_DB_CheckUserAuthority( pSUReq->idUser, AUTHORITY_CHECKER );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
break;
}
else if( ret==0 ){
pResponseMsg->iExtra = e_InvalidAuthority;
break;
}
}
int nOrigin = 3000,
tDateOrigin = pSUReq->tUpdateDateOrigin;
TSTYLEUPDATE aDataOrigin[3000];
if( f_DB_GetStyleUpdateListNew( &tDateOrigin, &nOrigin, aDataOrigin )<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
pSURsp->tUpdateDateOrigin = tDateOrigin;
pSURsp->nOriginNum = nOrigin;
pSURsp->nDataSize = sizeof(TSTYLEUPDATE) * nOrigin;
BYTE* pSecData = NULL;
int nShare = 2000,
tDateShare = pSUReq->tUpdateDateShare,
nSecSize = 0;
if( pSUReq->iClientType==e_Pro_Windows ){
pSecData = new BYTE[nShare * sizeof(TSHAREUPDATE)];
if( f_DB_GetShareUpdateList( &tDateShare, &nShare, (TSHAREUPDATE*)pSecData )<0 ){
nShare = 0;
}
else{
nSecSize = nShare * sizeof(TSHAREUPDATE);
}
}
else if( pSUReq->iClientType==e_SM_Checker ){
pSecData = new BYTE[nShare * sizeof(TCHECKUPDATE)];
if( f_DB_GetCheckUpdateList( &tDateShare, &nShare, (TCHECKUPDATE*)pSecData )<0 ){
nShare = 0;
}
else{
nSecSize = nShare * sizeof(TCHECKUPDATE);
}
}
else{
nShare = 0;
}
pSURsp->tUpdateDateShare = tDateShare;
pSURsp->nShareNum = nShare;
pSURsp->nDataSize += nSecSize;
if( pSURsp->nDataSize > 0 ){
char* pData = new char[pSURsp->nDataSize];
int idData = g_pDataBuffers->NewDataBuffer( pSUReq->idUser, DTYPE_STYLESTATE, pSURsp->nDataSize, pData, pSUReq->iClientType );
if(idData>0){ //正常创建传输数据
pSURsp->idData = idData;
int offset = 0;
if( nOrigin>0 ){
memcpy( pData, (void*)aDataOrigin, sizeof(TSTYLEUPDATE) * nOrigin );
offset = sizeof(TSTYLEUPDATE) * nOrigin;
}
if( nSecSize>0 ){
memcpy( pData + offset, (void*)pSecData, nSecSize );
}
DebugPrint( "更新风格数:%d - %d size: %d", nOrigin, nShare, pSURsp->nDataSize );
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pSURsp->nDataSize = 0;
pSURsp->idData = 0;
}
else{ //重复创建
pSURsp->idData = -idData;
}
delete[] pData;
}
}
else{
pSURsp->idData = 0;
}
if( pSecData )
delete[] pSecData;
pResponseMsg->nLength = sizeof(TStyleUpdateResponse);
}
}
}
break;
case e_styleInfoRequest:
{
TStyleInfoRequest * pSIReq = (TStyleInfoRequest*)pRequestMsg->byMessageBody;
int iType = HIWORD( pSIReq->iType );
if( iType==0 ){
if( LOWORD( pSIReq->iType )==e_SM_Checker )
iType = e_ST_Temp;
else
iType = e_ST_Origin;
}
iRespExtra = (iType<<16) + pSIReq->idStyle;
int ret = f_DB_CheckStyleInfo( iType, pSIReq->idStyle );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==-2 ){
pResponseMsg->iExtra = e_InvalidStyleID;
}
else{
int nDataSize = 0;
char* pData = NULL;
if( GetStyleInfo( iType, pSIReq->idStyle, nDataSize, &pData )<0 || pData==NULL ){
pResponseMsg->iExtra = e_OpenFileFailed;
}
else{
if( iMsgVer<0x0700 ){
TStyleInfoResponse0* pSIRsp = (TStyleInfoResponse0*)pResponseMsg->byMessageBody;
pSIRsp->idStyle = pSIReq->idStyle;
pSIRsp->size = nDataSize;
pSIRsp->verDetail = 0;
memcpy( pSIRsp->detail, pData, nDataSize );
pResponseMsg->nLength = pSIRsp->size + 3*sizeof(int);
delete[] pData;
}
else{
TStyleInfoResponse* pSIRsp = (TStyleInfoResponse*)pResponseMsg->byMessageBody;
int idData = g_pDataBuffers->NewDataBuffer( pSIReq->idStyle, DTYPE_STYLEINFO, nDataSize, pData, pSIReq->iType );
if(idData>0){ //正常创建传输数据
pSIRsp->nDataSize = nDataSize;
pSIRsp->idData = idData;
// DebugPrint( "New data: idData=%d size=%d\n", idData, nDataSize );
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pSIRsp->nDataSize = 0;
pSIRsp->idData = 0;
}
else{ //重复创建
pSIRsp->idData = -idData;
pSIRsp->nDataSize = nDataSize;
}
delete[] pData;
}
pResponseMsg->nLength = sizeof(TStyleInfoResponse);
}
}
}
}
break;
case e_styleDataRequest:
{
TStyleDataRequest * pSDReq = (TStyleDataRequest*)pRequestMsg->byMessageBody;
TStyleDataResponse* pSDRsp = (TStyleDataResponse*)pResponseMsg->byMessageBody;
int iType = HIWORD( pSDReq->iType );
if( iType==0 ){
if( LOWORD( pSDReq->iType )==e_SM_Checker )
iType = e_ST_Temp;
else
iType = e_ST_Origin;
}
iRespExtra = (iType<<16) + pSDReq->idStyle;
int ret = f_DB_CheckStyleInfo( iType, pSDReq->idStyle );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==-2 ){
pResponseMsg->iExtra = e_InvalidStyleID;
}
else{
int nDataSize = 0;
char* pData = NULL;
if( GetStyleData( iType, pSDReq->idStyle, nDataSize, &pData )<0 || pData==NULL ){
pResponseMsg->iExtra = e_OpenFileFailed;
}
else{
int idData = g_pDataBuffers->NewDataBuffer( pSDReq->idStyle, DTYPE_STYLEDATA, nDataSize, pData, pSDReq->iType );
if(idData>0){ //正常创建传输数据
pSDRsp->nDataSize = nDataSize;
pSDRsp->idData = idData;
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pSDRsp->nDataSize = 0;
pSDRsp->idData = 0;
}
else{ //重复创建
pSDRsp->idData = -idData;
pSDRsp->nDataSize = nDataSize;
}
delete[] pData;
}
pResponseMsg->nLength = sizeof(TStyleDataResponse);
}
}
}
break;
case e_styleRecomRequest:
{
TStyleRecomResponse* pSRRsp = (TStyleRecomResponse*)pResponseMsg->byMessageBody;
pSRRsp->nRecomNum = g_aStyleRecomList[MAX_STYLE_NUM];
memcpy( pSRRsp->aStyleIDs, g_aStyleRecomList, sizeof(int)*MAX_STYLE_NUM );
pResponseMsg->nLength = (pSRRsp->nRecomNum+1)*sizeof(UINT);
}
break;
case e_registerRequest:
{
char *strName = NULL,
*strPwd = NULL,
*strCode = NULL;
UINT param = 0;
int idUser = 0;
int isEmailOrMobile = 0;
char strNickName[32] = "";
// idDevice[18] = "";
if( iMsgVer<0x0600 ){
TRegisterRequest0 * pRegReq = (TRegisterRequest0*)pRequestMsg->byMessageBody;
strName = pRegReq->strName;
strPwd = pRegReq->strPassword;
}
else if( iMsgVer<0x0A00 ){
TRegisterRequest1 * pRegReq = (TRegisterRequest1*)pRequestMsg->byMessageBody;
strName = pRegReq->strName;
strPwd = pRegReq->strPassword;
param = pRegReq->param;
}
else if( iMsgVer<0x0B00 ){
TRegisterRequest2 * pRegReq = (TRegisterRequest2*)pRequestMsg->byMessageBody;
strName = pRegReq->strName;
strPwd = pRegReq->strPassword;
param = pRegReq->param;
// sprintf( idDevice, "%02X:%02X:%02X:%02X:%02X:%02X", (UCHAR)pRegReq->idDevice[0], (UCHAR)pRegReq->idDevice[1],
// (UCHAR)pRegReq->idDevice[2], (UCHAR)pRegReq->idDevice[3],
// (UCHAR)pRegReq->idDevice[4], (UCHAR)pRegReq->idDevice[5] );
if( strlen(pRegReq->strNickname) )
strcpy( strNickName, pRegReq->strNickname );
if( strlen( pRegReq->strCode) ){
strCode = pRegReq->strCode;
isEmailOrMobile = 2;
}
}
else{
TRegisterRequest * pRegReq = (TRegisterRequest*)pRequestMsg->byMessageBody;
strName = pRegReq->strName;
strPwd = pRegReq->strPassword;
param = pRegReq->param;
isEmailOrMobile = pRegReq->isEmailOrMobile;
}
// if( strCode ){
// DebugPrint( "Phone check: %s, %s, %s; ret=%d", idDevice, strName, strCode, ret );
// if( ret!=0 ){
// if( ret<0 ){
// pResponseMsg->iExtra = e_DatabaseError;
// }
// else{
// pResponseMsg->iExtra = e_PhoneCheckFailed;
// }
// break;
// }
// bByPhone = true;
// }
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
#ifdef _WIN32
string ansiName;
UTF8ToGBK( strName, ansiName );
DebugPrint( "REGISTER %d.%d.%d.%d:%d name: %s", b1, b2, b3, b4, port, ansiName.c_str() );
#else
DebugPrint( "REGISTER %d.%d.%d.%d:%d name: %s", b1, b2, b3, b4, port, strName );
#endif
ret = f_DB_Register( strName, strPwd, isEmailOrMobile, idUser, strNickName, iClientType );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==0 ){
pResponseMsg->iExtra = e_RequestSuccess;
if(iMsgVer<0x0600 ){
TRegisterResponse0* pRegRsp = (TRegisterResponse0*) pResponseMsg->byMessageBody;
pRegRsp->idUser = idUser;
strcpy( pRegRsp->strNickname, strNickName );
pResponseMsg->nLength = sizeof(TRegisterResponse0);
}
else{
TRegisterResponse* pRegRsp = (TRegisterResponse*) pResponseMsg->byMessageBody;
pRegRsp->param = f_PM_HandleParam( param, 13 );
pRegRsp->idUser = idUser;
strcpy( pRegRsp->strNickname, strNickName );
pResponseMsg->nLength = sizeof(TRegisterResponse);
}
}
else if( ret==1 ){
pResponseMsg->iExtra = e_UsernameExist;
#ifdef _WIN32
DebugPrint( "%s 用户名已存在.", ansiName.c_str() );
#else
DebugPrint( "%s 用户名已存在.", strName );
#endif
}
}
break;
case e_loginRequest:
{
char *strName = NULL,
*strPwd = NULL;
char idDevice[18] = "";
UINT param = 0;
if( iMsgVer<0x0600 ){
TLoginRequest0 * pLogReq = (TLoginRequest0*)pRequestMsg->byMessageBody;
strName = pLogReq->strName;
strPwd = pLogReq->strPassword;
}
else if( iMsgVer<0x0800 ){
TLoginRequest1 * pLogReq = (TLoginRequest1*)pRequestMsg->byMessageBody;
strName = pLogReq->strName;
strPwd = pLogReq->strPassword;
param = pLogReq->param;
}
else if( iMsgVer<0x0D00) {
TLoginRequest2 * pLogReq = (TLoginRequest2*)pRequestMsg->byMessageBody;
strName = pLogReq->strName;
strPwd = pLogReq->strPassword;
param = pLogReq->param;
if( iClientType!=e_SM_Checker ){
sprintf( idDevice, "%02X%02X%02X%02X%02X%02X", (UCHAR)pLogReq->idDevice[0], (UCHAR)pLogReq->idDevice[1],
(UCHAR)pLogReq->idDevice[2], (UCHAR)pLogReq->idDevice[3],
(UCHAR)pLogReq->idDevice[4], (UCHAR)pLogReq->idDevice[5] );
}
}
else{
TLoginRequest * pLogReq = (TLoginRequest*)pRequestMsg->byMessageBody;
strName = pLogReq->strName;
strPwd = pLogReq->strPassword;
param = pLogReq->param;
if( iClientType!=e_SM_Checker ){
sprintf( idDevice, "%02X%02X%02X%02X%02X%02X", (UCHAR)pRequestMsg->idDevice[0], (UCHAR)pRequestMsg->idDevice[1],
(UCHAR)pRequestMsg->idDevice[2], (UCHAR)pRequestMsg->idDevice[3],
(UCHAR)pRequestMsg->idDevice[4], (UCHAR)pRequestMsg->idDevice[5] );
}
}
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
#ifdef _WIN32
string ansiName;
UTF8ToGBK( strName, ansiName );
DebugPrint( "LOGIN %d.%d.%d.%d:%d name: %s", b1, b2, b3, b4, port, ansiName.c_str() );
#else
DebugPrint( "LOGIN %d.%d.%d.%d:%d name: %s", b1, b2, b3, b4, port, strName );
#endif
TACCOUNTINFO accountInfo;
char strNickName[32] = "", strIconFile[32] = "";
ret = f_DB_Login( strName, strPwd, idDevice, &accountInfo, strNickName, strIconFile, iClientType );
int nickLen = strlen( strNickName );
int iconLen = strlen( strIconFile );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==0 ){
pResponseMsg->iExtra = e_RequestSuccess;
if(iMsgVer<0x0600 ){
TLoginResponse0* pLgRsp = (TLoginResponse0*) pResponseMsg->byMessageBody;
TACCOUNTINFO0 account0;
account0.ID = accountInfo.ID;
account0.balance = accountInfo.balance;
account0.pops = accountInfo.pops;
account0.fans = accountInfo.fans;
account0.songs = accountInfo.songs;
memcpy( &pLgRsp->accountInfo, &account0, account0.size );
pResponseMsg->nLength = account0.size;
if( nickLen>0 ){
memcpy( pResponseMsg->byMessageBody+account0.size, strNickName, nickLen+1 );
pResponseMsg->nLength += nickLen+1;
}
}
else if( iMsgVer<0x0800 ){
TACCOUNTINFO1 account1;
account1.ID = accountInfo.ID;
account1.balance = accountInfo.balance;
account1.pops = accountInfo.pops;
account1.fans = accountInfo.fans;
account1.songs = accountInfo.songs;
if(iMsgVer<0x0700) {
TLoginResponse1* pLgRsp = (TLoginResponse1*) pResponseMsg->byMessageBody;
if( iClientType==e_Pro_Windows ){
pLgRsp->param = f_PM_HandleParam( param, 13 );
///////////////////体验版/////////////////////
accountInfo.authority |= AUTHORITY_CREATOR;
//////////////////////////////////////////////
account1.authority = accountInfo.authority;
}
if( nickLen>0 ){
memcpy( pLgRsp->strNickname, strNickName, nickLen+1 );
}
memcpy( &pLgRsp->accountInfo, &account1, account1.size );
pResponseMsg->nLength = 36 + account1.size;
}
else{
TLoginResponse* pLgRsp = (TLoginResponse*) pResponseMsg->byMessageBody;
if( iClientType==e_Pro_Windows ){
pLgRsp->param = f_PM_HandleParam( param, 13 );
///////////////////体验版/////////////////////
accountInfo.authority |= AUTHORITY_CREATOR;
//////////////////////////////////////////////
account1.authority = accountInfo.authority;
}
if( nickLen>0 ){
memcpy( pLgRsp->strNickname, strNickName, nickLen+1 );
}
if( iconLen>0 ){
memcpy( pLgRsp->strIconFile, strIconFile, iconLen+1 );
}
memcpy( &pLgRsp->accountInfo, &account1, account1.size );
pResponseMsg->nLength = 68 + account1.size;
}
}
else{
TLoginResponse* pLgRsp = (TLoginResponse*) pResponseMsg->byMessageBody;
if( iClientType==e_Pro_Windows ){
pLgRsp->param = f_PM_HandleParam( param, 13 );
///////////////////体验版/////////////////////
accountInfo.authority |= AUTHORITY_CREATOR;
//////////////////////////////////////////////
}
if( nickLen>0 ){
memcpy( pLgRsp->strNickname, strNickName, nickLen+1 );
}
if( iconLen>0 ){
memcpy( pLgRsp->strIconFile, strIconFile, iconLen+1 );
}
if( iMsgVer<0x0A00 ){
memcpy( &pLgRsp->accountInfo, &accountInfo, sizeof(TACCOUNTINFO) );
}
else{
int count = sizeof(TACCOUNTINFO)/sizeof(int);
int* pDest = (int*)&pLgRsp->accountInfo;
int* pSrc = (int*)&accountInfo;
int shNum = pResponseMsg->nSequenceNum % 8;
for( int i=0; i<count; i++ ){
pDest[i] = ShiftInt( pSrc[i], false, shNum );
}
}
pResponseMsg->nLength = 68 + sizeof(TACCOUNTINFO);
}
}
else if( ret==1 ){
pResponseMsg->iExtra = e_LoginPasswordError;
DebugPrint( "%s 密码错误.", strName );
}
else if( ret==2 ){
pResponseMsg->iExtra = e_LoginUserNotExist;
DebugPrint( "%s 用户不存在.", strName );
}
else if( ret==3 ){
pResponseMsg->iExtra = e_VipExceeded;
DebugPrint( "%s vip用户设备超限.", strName );
}
else if( ret==4 ){
pResponseMsg->iExtra = iMsgVer<0x0C00 ? e_LoginUserNotExist : e_UserDeleted;
DebugPrint( "%s 账户已被删除.", strName );
}
else if( ret==5 ){
pResponseMsg->iExtra = iMsgVer<0x0C00 ? e_LoginUserNotExist : e_UserLocked;
DebugPrint( "%s 账户已被冻结.", strName );
}
}
break;
case e_verifyRequest:
{
TVerifyRequest * pVerifyReq = (TVerifyRequest*)pRequestMsg->byMessageBody;
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
string strIDs;
char buf[20];
for( int i=0; i<pVerifyReq->nStyleNum; i++ ){
sprintf(buf, "%d", pVerifyReq->aIdStyles[i]);
strIDs += buf;//itoa( pVerifyReq->aIdStyles[i], buf, 10 );
if( i<pVerifyReq->nStyleNum-1 ){
strIDs += ",";
}
}
DebugPrint( "VERIFY %d.%d.%d.%d:%d uid: %d sid:[%s]", b1, b2, b3, b4, port,
pVerifyReq->idUser, strIDs.c_str() );
int aOriginCost[MAX_STYLE_NUM], aCurrentCost[MAX_STYLE_NUM], nUserBalance;
memset( aOriginCost, 0, sizeof(int)*MAX_STYLE_NUM );
memset( aCurrentCost, 0, sizeof(int)*MAX_STYLE_NUM );
ret = f_DB_Verify( pVerifyReq->idUser, (int*)pVerifyReq->aIdStyles, pVerifyReq->nStyleNum,
aOriginCost, aCurrentCost, &nUserBalance );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
TVerifyResponse * pVerifyRsp = (TVerifyResponse*) pResponseMsg->byMessageBody;
pVerifyRsp->fUserBalance = nUserBalance;
pVerifyRsp->nStyleNum = pVerifyReq->nStyleNum;
for( int i=0; i<pVerifyReq->nStyleNum; i++ ){
pVerifyRsp->aStylePrices[i] = aOriginCost[i];
pVerifyRsp->aStylePrices[pVerifyReq->nStyleNum+i] = aCurrentCost[i];
}
pResponseMsg->nLength = 12 + pVerifyRsp->nStyleNum * sizeof(float) * 2;
if( ret==1 ){
pResponseMsg->iExtra = e_VerifyInsufficient;
DebugPrint( "%d 账户余额不足", pVerifyReq->idUser );
}
else if( ret==2 ){
pResponseMsg->iExtra = e_InvalidUserID;
DebugPrint( "%d 无效的用户ID", pVerifyReq->idUser );
}
else if( ret==3 ){
pResponseMsg->iExtra = e_InvalidStyleID;
DebugPrint( "%d 无效的风格ID", pVerifyReq->idUser);
}
else{
DWORD dwParam2 = 13;
if( iMsgVer>0x0100 ){
dwParam2 = pVerifyReq->aIdStyles[0];
}
pVerifyRsp->dwMMParam = f_PM_HandleParam( pVerifyReq->dwMMParam, dwParam2 );
//DebugPrint( "%d 通过鉴权", pVerifyReq->idUser );
}
}
}
break;
case e_purchaseRequest:
{
TPurchaseRequest * pPurchaseReq = (TPurchaseRequest*)pRequestMsg->byMessageBody;
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
string strIDs;
char buf[20];
for( int i=0; i<pPurchaseReq->nStyleNum; i++ ){
sprintf(buf, "%d", pPurchaseReq->aIdStyles[i]);
strIDs += buf;// itoa( pPurchaseReq->aIdStyles[i], buf, 10 );
if( i<pPurchaseReq->nStyleNum-1 ){
strIDs += ",";
}
}
DebugPrint( "PURCHASE %d.%d.%d.%d:%d uid: %d iap:%d sid:[%s]", b1, b2, b3, b4, port,
pPurchaseReq->idUser, pPurchaseReq->iapType, strIDs.c_str() );
int nUserBalance;
ret = f_DB_Purchase( pPurchaseReq->idUser, (int*)pPurchaseReq->aIdStyles,
pPurchaseReq->nStyleNum, &nUserBalance, 0 );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
if( ret==1 ){
pResponseMsg->iExtra = e_VerifyInsufficient;
DebugPrint( "%d 账户余额不足", pPurchaseReq->idUser );
}
else{
TPurchaseResponse * pPurchaseRsp = (TPurchaseResponse*) pResponseMsg->byMessageBody;
pPurchaseRsp->fUserBalance = nUserBalance;
pResponseMsg->nLength =sizeof(TPurchaseResponse);
DebugPrint( "%d 付费成功", pPurchaseReq->idUser );
}
}
}
break;
case e_checkPaymentRequest:
{
UINT idUser;
UINT* aIdStyles;
DWORD dwMMParam = 0;
WORD wOperation, wStyleNum;
if( iMsgVer<0x0A00 ){
TCheckPaymentRequest0 * pCPReq = (TCheckPaymentRequest0*)pRequestMsg->byMessageBody;
idUser = pCPReq->idUser;
aIdStyles = pCPReq->aIdStyles;
wOperation = pCPReq->wOperation;
wStyleNum = pCPReq->wStyleNum;
}
else{
TCheckPaymentRequest * pCPReq = (TCheckPaymentRequest*)pRequestMsg->byMessageBody;
idUser = pCPReq->idUser;
aIdStyles = pCPReq->aIdStyles;
wOperation = pCPReq->wOperation;
wStyleNum = pCPReq->wStyleNum;
dwMMParam = pCPReq->dwMMParam;
}
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
string strIDs;
char buf[20];
for( int i=0; i<wStyleNum; i++ ){
sprintf(buf, "%d", aIdStyles[i]);
strIDs += buf;//itoa( aIdStyles[i], buf, 10 );
if( i<wStyleNum-1 ){
strIDs += ",";
}
}
DebugPrint( "CHECKPAYMENT %d.%d.%d.%d:%d uid: %d sn:%d sid:[%s] op:%d", b1, b2, b3, b4, port,
idUser, wStyleNum, strIDs.c_str(), wOperation );
int nUserBalance, aCost[MAX_STYLE_NUM], nTotal;
memset( aCost, 0, MAX_STYLE_NUM );
ret = f_DB_CheckPayment( iClientType, idUser, wOperation, wStyleNum, aIdStyles,
aCost, &nUserBalance, &nTotal );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==2 ){
pResponseMsg->iExtra = e_InvalidUserID;
DebugPrint( "%d 无效的用户ID", idUser );
}
else{
bool bInvalidStyle = false;
//////临时代码///////////////////////////
if( iMsgVer<0x0800 && nTotal==0 ){// 0<f_DB_CheckUserAuthority( pCPReq->idUser, AUTHORITY_VIP ) ){
nTotal = 1;
}
////////////////////////////////////////
if( wOperation==0 || wOperation & e_PO_Pay4Compose ){
for( int i=0; i<wStyleNum; i++ ){
if( aCost[i]<0 ){
bInvalidStyle = true;
}
else{
nTotal += aCost[i];
}
}
}
if( bInvalidStyle ){
pResponseMsg->iExtra = e_InvalidStyleID;
DebugPrint( "%d 无效的风格ID", idUser);
}
if( iMsgVer>=0x0A00 ){
TCheckPaymentResponse * pCPRsp = (TCheckPaymentResponse*) pResponseMsg->byMessageBody;
int shNum = pRequestMsg->nSequenceNum % 8;
pCPRsp->nUserBalance = ShiftInt( nUserBalance, false, shNum );
pCPRsp->nPayment = ShiftInt( nTotal, false, shNum );
if( wOperation==0 || wOperation & e_PO_Pay4Compose ){
for( int i=0; i<wStyleNum; i++ ){
pCPRsp->aStyleCost[i] = ShiftInt( aCost[i], false, shNum );
}
}
DWORD dwParam2 = 0;
for( int i=0; i<wStyleNum; i++ ){
dwParam2 += aIdStyles[i];
}
pCPRsp->dwMMParam = ShiftInt( f_PM_HandleParam( dwMMParam, dwParam2 ), false, shNum );
pResponseMsg->nLength = ( wStyleNum + 3 ) * sizeof(int);
}
else if( iMsgVer>=0x0500 ){
TCheckPaymentResponse1 * pCPRsp = (TCheckPaymentResponse1*) pResponseMsg->byMessageBody;
pCPRsp->nUserBalance = nUserBalance;
pCPRsp->nPayment = nTotal;
if( wOperation==0 || wOperation & e_PO_Pay4Compose ){
for( int i=0; i<wStyleNum; i++ ){
pCPRsp->aStyleCost[i] = aCost[i];
}
}
pResponseMsg->nLength = ( wStyleNum + 2 ) * sizeof(int);
}
else{
TCheckPaymentResponse0 * pCPRsp = (TCheckPaymentResponse0*) pResponseMsg->byMessageBody;
pCPRsp->nUserBalance = nUserBalance;
pCPRsp->nPayment = nTotal;
pResponseMsg->nLength = sizeof(TCheckPaymentResponse0);
}
}
}
break;
case e_payRequest:
{
WORD wStyleNum,
wPaidNum,
wOperation;
UINT idUser;
UINT* aIdStyles;
DWORD dwMMParam = 0;
if( iMsgVer<0x0A00 ){
TPayRequest0 * pPayReq = (TPayRequest0*)pRequestMsg->byMessageBody;
wStyleNum = LOBYTE(pPayReq->wStyleNum),
wPaidNum = HIBYTE(pPayReq->wStyleNum),
wOperation = pPayReq->wOperation;
idUser = pPayReq->idUser;
aIdStyles = pPayReq->aIdStyles;
dwMMParam = pPayReq->dwMMParam;
}
else{
TPayRequest * pPayReq = (TPayRequest*)pRequestMsg->byMessageBody;
wStyleNum = LOBYTE(pPayReq->wStyleNum),
wPaidNum = HIBYTE(pPayReq->wStyleNum),
wOperation = pPayReq->wOperation;
idUser = pPayReq->idUser;
aIdStyles = pPayReq->aIdStyles;
}
string strIDs;
char buf[20];
int i;
for( i=0; i<wStyleNum; i++ ){
sprintf(buf, "%d", aIdStyles[i]);
strIDs += buf;//itoa( aIdStyles[i], buf, 10 );
if( i<wStyleNum-1 ){
strIDs += ",";
}
}
if( wPaidNum>0 ){
strIDs += ";";
for( i=0; i<wPaidNum; i++ ){
sprintf(buf, "%d", aIdStyles[i+wStyleNum]);
strIDs += buf;//itoa( aIdStyles[i+wStyleNum], buf, 10 );
if( i<wPaidNum-1 ){
strIDs += ",";
}
}
}
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
DebugPrint( "CONFIRMPAYMENT %d.%d.%d.%d:%d uid: %d sn:%d pn:%d sid:[%s] op:%d", b1, b2, b3, b4, port,
idUser, wStyleNum, wPaidNum, strIDs.c_str(), wOperation );
int nUserBalance;
ret = f_DB_ConfirmPayment( iClientType, idUser, wOperation, wStyleNum, aIdStyles,
wPaidNum, aIdStyles+wStyleNum, &nUserBalance );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==1 ){
pResponseMsg->iExtra = e_VerifyInsufficient;
DebugPrint( "%d 账户余额不足", idUser );
}
else if( ret==2 ){
pResponseMsg->iExtra = e_InvalidUserID;
DebugPrint( "%d 无效的用户ID", idUser );
}
else if( ret==3 ){
pResponseMsg->iExtra = e_InvalidStyleID;
DebugPrint( "无效的风格ID:[%s]", strIDs.c_str() );
}
else{
if( iMsgVer<0x0A00 ){
TPayResponse0 * pPayRsp = (TPayResponse0*) pResponseMsg->byMessageBody;
pPayRsp->nUserBalance = nUserBalance;
pResponseMsg->nLength = sizeof(TPayResponse0);
DWORD dwParam2 = 0;
if( iMsgVer>=0x0500 ){
for( int i=0; i<wStyleNum; i++ ){
dwParam2 += aIdStyles[i];
}
}
else{
dwParam2 = aIdStyles[0];
}
pPayRsp->dwMMParam = f_PM_HandleParam( dwMMParam, dwParam2 );
}
else{
TPayResponse * pPayRsp = (TPayResponse*) pResponseMsg->byMessageBody;
pPayRsp->nUserBalance = ShiftInt( nUserBalance, false, (pRequestMsg->nSequenceNum % 8) );
pResponseMsg->nLength = sizeof(TPayResponse);
}
}
}
break;
case e_uploadStyleRequest:
{
int idFile = 0, iStyleType = 0;
TUploadStyleRequest * pUploadReq = (TUploadStyleRequest*)pRequestMsg->byMessageBody;
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
DebugPrint( "UPLOAD STYLE %d.%d.%d.%d:%d uid: %d", b1, b2, b3, b4, port, pUploadReq->idUser );
UINT iAuthority = 0;
if( pUploadReq->iType==1 ){
iStyleType = DTYPE_STYLEFILE;
iAuthority = AUTHORITY_CHECKER;
}
else if( pUploadReq->iType==2 ){
iStyleType = DTYPE_STYLETEMP;
///////////////////体验版/////////////////////
iAuthority = 1;
//////////////////////////////////////////////
//iAuthority = AUTHORITY_CREATOR;
}
else if( pUploadReq->iType==3 ){
iStyleType = DTYPE_STYLESHARE;
iAuthority = 1;
}
else{
pResponseMsg->iExtra = e_InvalidAuthority;
break;
}
//检查checkerID权限
ret = f_DB_CheckUserAuthority( pUploadReq->idUser, iAuthority );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
break;
}
else if( ret==0 ){
pResponseMsg->iExtra = e_InvalidAuthority;
break;
}
else{
ret = 0;
//创建接收对象
if( g_pDataBuffers ){
idFile = g_pDataBuffers->NewDataBuffer( pUploadReq->idUser, iStyleType, pUploadReq->nDataSize, NULL, pUploadReq->iType );
if( idFile==0 )
ret = e_ServiceBusy;
else if( idFile<0 )
idFile = -idFile;
}
else{
ret = e_NoService;
}
}
if( ret!=0 ){
pResponseMsg->iExtra = ret;
}
else{
TUploadStyleResponse * pUploadRsp = (TUploadStyleResponse*) pResponseMsg->byMessageBody;
pUploadRsp->idFile = idFile;
pResponseMsg->nLength =sizeof(TUploadStyleResponse);
}
}
break;
case e_uploadSongRequest:
{
int idFile = 0, iFileType;
TUploadSongRequest * pUploadReq = (TUploadSongRequest*)pRequestMsg->byMessageBody;
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
if( pUploadReq->nDataSize==0 ){
DebugPrint( "PERMISSION UPLOAD SONG %d.%d.%d.%d:%d uid: %d", b1, b2, b3, b4, port, pUploadReq->idUser );
//检查用户上传权限
ret = f_DB_CheckUserAuthority( pUploadReq->idUser, 0 );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==0 ){
pResponseMsg->iExtra = e_InvalidAuthority;
}
}
else if( pUploadReq->nDataSize - sizeof(TSONGINFO0)<=0 ){
pResponseMsg->iExtra = e_InvalidData;
}
else{
DebugPrint( "UPLOAD SONG %d.%d.%d.%d:%d uid: %d", b1, b2, b3, b4, port, pUploadReq->idUser );
iFileType = DTYPE_SONGFILE;
//创建接收对象
if( g_pDataBuffers ){
idFile = g_pDataBuffers->NewDataBuffer( pUploadReq->idUser, iFileType,
pUploadReq->nDataSize, NULL, pUploadReq->iClientType );
if( idFile==0 )
ret = e_ServiceBusy;
else if( idFile<0 )
idFile = -idFile;
}
else{
ret = e_NoService;
}
if( ret!=0 ){
pResponseMsg->iExtra = ret;
}
else{
TUploadSongResponse * pUploadRsp = (TUploadSongResponse*) pResponseMsg->byMessageBody;
pUploadRsp->idFile = idFile;
pResponseMsg->nLength =sizeof(TUploadSongResponse);
}
}
}
break;
case e_dataSendRequest:
{
TDataSendRequest * pSendReq = (TDataSendRequest*)pRequestMsg->byMessageBody;
int iExtra = 0, lenExtra = 0;
unsigned char buffExtra[1024];
if( g_pDataBuffers ){
ret = g_pDataBuffers->ReceiveData( pSendReq->idData,
pSendReq->idPacket,
pSendReq->nPackSize,
pSendReq->arrPacket );
if( ret==ERR_DATA_END ){
DATABUFFER* pBuffer = g_pDataBuffers->GetBuffer( pSendReq->idData );
if( pBuffer->iDataType==DTYPE_STYLETEMP || pBuffer->iDataType==DTYPE_STYLEFILE
|| pBuffer->iDataType==DTYPE_STYLESHARE ){
iExtra = CreateStyle( pBuffer->idUser, pBuffer->iDataType, pBuffer->nDataSize, pBuffer->pData );
}
else if( pBuffer->iDataType==DTYPE_SONGFILE ){
TSHAREINFO info;
iExtra = CreateSong( pBuffer->idUser, pBuffer->iClientType, pBuffer->nDataSize, pBuffer->pData, &info );
if( iExtra==0 ){
*(UINT*)buffExtra = info.idSong;
lenExtra += 4;
unsigned char len = strlen( info.strSongURL );
buffExtra[lenExtra++] = len;
if( len>0 ){
memcpy( buffExtra+lenExtra, info.strSongURL, len );
lenExtra += len;
}
len = strlen( info.strCoverURL );
buffExtra[lenExtra++] = len;
if( len>0 ){
memcpy( buffExtra+lenExtra, info.strCoverURL, len );
lenExtra += len;
}
}
}
if( iExtra==0 && iMsgVer>=0x400 ){
iExtra = e_ReceiveComplete;
}
g_pDataBuffers->DeleteBuffer( pSendReq->idData );
}
else if( ret==ERR_DATA_ID ){
iExtra = e_InvalidDataID;
}
else if( ret==ERR_PACK_ID ){
iExtra = e_InvalidPackID;
}
else if( ret==ERR_DATA_SIZE ){
iExtra = e_OutOfBound;
}
}
else{
iExtra = e_NoService;
}
pResponseMsg->iExtra = iExtra;
if( iExtra<0x8000 ){
TDataSendResponse * pSendResp = (TDataSendResponse*) pResponseMsg->byMessageBody;
pSendResp->idData = pSendReq->idData;
pSendResp->idPacket = pSendReq->idPacket;
pResponseMsg->nLength =sizeof(TDataSendResponse);
if( iExtra==e_ReceiveComplete && lenExtra>0 ){
memcpy( pResponseMsg->byMessageBody + pResponseMsg->nLength, buffExtra, lenExtra );
pResponseMsg->nLength += lenExtra;
}
}
}
break;
case e_dataReceiveRequest:
{
TDataReceiveRequest * pDRReq = (TDataReceiveRequest*)pRequestMsg->byMessageBody;
int iExtra = 0;
char* pData = NULL;
int nSize = 0;
if( g_pDataBuffers ){
ret = g_pDataBuffers->GetData( pDRReq->idData, pDRReq->idPacket,
nSize, pData );
if( ret==ERR_DATA_ID ){
iExtra = e_InvalidDataID;
}
else if( ret==ERR_PACK_ID ){
iExtra = e_InvalidPackID;
}
}
else{
iExtra = e_NoService;
}
if( iExtra>=0x8000 ){
pResponseMsg->iExtra = iExtra;
}
else if( pDRReq->idPacket!=-1 ){ //idPacket==-1表示接收结束
TDataReceiveResponse * pDRResp = (TDataReceiveResponse*) pResponseMsg->byMessageBody;
pDRResp->idData = pDRReq->idData;
pDRResp->idPacket = pDRReq->idPacket;
pDRResp->nPackSize = nSize;
memcpy( pDRResp->arrPacket, pData, nSize );
pResponseMsg->nLength = 12 + nSize;
}
}
break;
case e_styleCheckRequest:
{
TStyleCheckRequest * pSCReq = (TStyleCheckRequest*)pRequestMsg->byMessageBody;
TStyleCheckResponse * pSCResp = (TStyleCheckResponse*)pResponseMsg->byMessageBody;
char *pb=(char *)&(ip);
unsigned b1=(unsigned char)(*pb);
unsigned b2=(unsigned char)(*(pb+1));
unsigned b3=(unsigned char)(*(pb+2));
unsigned b4=(unsigned char)(*(pb+3));
DebugPrint( "CHECK STYLE %d.%d.%d.%d:%d uid: %d", b1, b2, b3, b4, port, pSCReq->idChecker );
//检查checkerID权限
ret = f_DB_CheckUserAuthority( pSCReq->idChecker, AUTHORITY_CHECKER );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
break;
}
else if( ret==0 ){
pResponseMsg->iExtra = e_InvalidAuthority;
break;
}
//读取文件
char strTempFileName[256];
char buf[20];
strcpy( strTempFileName, g_strStyleFileDir );
strcat( strTempFileName, "Temp/" );
sprintf( buf, "%08X.YSI", pSCReq->idStyle );
strcat( strTempFileName, buf );
FILE *fp = fopen( strTempFileName, "rb" );
if( !fp ){
int err = GetLastError();
DebugPrint( "打开风格 %s 失败, err=%d", strTempFileName, err );
pResponseMsg->iExtra = e_OpenFileFailed;
break;
}
else{
fclose( fp );
char* strMsg = NULL;
if( pSCReq->score==0 )
strMsg = pSCReq->strDescrib;
ret = f_DB_SetStyleCheck( pSCReq->idChecker, pSCReq->idStyle, pSCReq->score, strMsg );
if( ret==-1 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else if( ret==-2 ){
pResponseMsg->iExtra = e_CheckedAlready;
}
else if( ret>0 ){
UINT idStyle = ret;
ret = PublishStyle( pSCReq->idStyle, idStyle );
if( ret<0 ){
pResponseMsg->iExtra = e_OpenFileFailed;
}
else{
pSCResp->idStyle = idStyle;
pResponseMsg->nLength = sizeof(TStyleCheckResponse);
}
}
}
}
break;
case e_iconRequest:
{
TIconRequest * pIconReq = (TIconRequest*)pRequestMsg->byMessageBody;
int nDataSize = 0;
char* pData = NULL;
if( GetIconFile( pIconReq->strIconFile, nDataSize, &pData )<0 || pData==NULL ){
pResponseMsg->iExtra = e_OpenFileFailed;
}
else{
TIconResponse* pIconRsp = (TIconResponse*)pResponseMsg->byMessageBody;
char fname[256];
strcpy(fname, pIconReq->strIconFile);
char* pDot = strchr(fname, '.');
if (pDot != NULL)
*pDot = 0;
int id = 0, ts = 0;
char* pSlash = strchr(fname, '_');
if(pSlash==fname){
sscanf( fname, "_%d", &ts );
}
else if(pSlash!=NULL){
sscanf( fname, "%d_%d", &id, &ts);
}
int idData = g_pDataBuffers->NewDataBuffer( id, DTYPE_ICONFILE, nDataSize, pData, ts );
if(idData>0){ //正常创建传输数据
pIconRsp->nDataSize = nDataSize;
pIconRsp->idData = idData;
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pIconRsp->nDataSize = 0;
pIconRsp->idData = 0;
}
else{ //重复创建
pIconRsp->idData = -idData;
pIconRsp->nDataSize = nDataSize;
}
delete[] pData;
}
pResponseMsg->nLength = sizeof(TIconResponse);
}
}
break;
case e_qnaUpdateRequest:
{
TQnAUpdateRequest * pQUReq = (TQnAUpdateRequest*)pRequestMsg->byMessageBody;
TQnAUpdateResponse* pQURsp = (TQnAUpdateResponse*)pResponseMsg->byMessageBody;
TQNAITEM lstUserQnA[MAX_QNA_NUM];
int tUpdateDate = pQUReq->tLastUpdateDate;
int nUserQANum = f_DB_GetUserQnA( pQUReq->nQnANum, (int*)pQUReq->aIdQnAs, pQUReq->idUser, pQUReq->iClientType,
tUpdateDate, MAX_QNA_NUM, lstUserQnA );
if( nUserQANum<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
iRespExtra = (pQUReq->iClientType<<20) + pQUReq->idUser;
//计算更新条数
int i, nUpdateNum = nUserQANum;
for( i=0; i<g_nCommonQnANum; i++ ){
if( g_lstCommonQnA[i].dtUpdate > tUpdateDate ){
for( int j=0; j<8; j++ ){
if( g_lstCommonQnA[i].iClientType[j]==pQUReq->iClientType ){
nUpdateNum++;
break;
}
}
}
}
if( nUpdateNum > 0 ){
//分配数据缓冲区
int nDataSize = nUpdateNum * 980;
char* pData = new char[nDataSize];
*(int*)pData = nUpdateNum;
int pos = 4;
int tMaxUpdateDate = tUpdateDate;
//写入常见问题数据
for( i=0; i<g_nCommonQnANum; i++ ){
if( g_lstCommonQnA[i].dtUpdate>tUpdateDate ){
bool bApply = false;
for( int j=0; j<8; j++ ){
if( g_lstCommonQnA[i].iClientType[j]==pQUReq->iClientType ){
bApply = true;
break;
}
}
if( bApply ){
*(UINT*)(pData+pos) = g_lstCommonQnA[i].id; pos+=4;
*(UINT*)(pData+pos) = g_lstCommonQnA[i].idUser; pos+=4;
*(UINT*)(pData+pos) = g_lstCommonQnA[i].state; pos+=4;
if( g_lstCommonQnA[i].state!=5 ){
int len = strlen( g_lstCommonQnA[i].strQuestion );
*(UINT*)(pData+pos) = len; pos+=4;
if( len>0 ){
memcpy( pData+pos, g_lstCommonQnA[i].strQuestion, len );
pos += len;
}
len = strlen( g_lstCommonQnA[i].strAnswer );
*(UINT*)(pData+pos) = len; pos+=4;
if( len>0 ){
memcpy( pData+pos, g_lstCommonQnA[i].strAnswer, len );
pos += len;
}
}
if( g_lstCommonQnA[i].dtUpdate > tMaxUpdateDate ){
tMaxUpdateDate = g_lstCommonQnA[i].dtUpdate;
}
}
}
}
//写入用户提问数据
for( i=0; i<nUserQANum; i++ ){
*(UINT*)(pData+pos) = lstUserQnA[i].id; pos+=4;
*(UINT*)(pData+pos) = lstUserQnA[i].idUser; pos+=4;
*(UINT*)(pData+pos) = lstUserQnA[i].state; pos+=4;
if( lstUserQnA[i].state!=5 ){
int len = strlen( lstUserQnA[i].strQuestion );
*(UINT*)(pData+pos) = len; pos+=4;
if( len>0 ){
memcpy( pData+pos, lstUserQnA[i].strQuestion, len );
pos += len;
}
len = strlen( lstUserQnA[i].strAnswer );
*(UINT*)(pData+pos) = len; pos+=4;
if( len>0 ){
memcpy( pData+pos, lstUserQnA[i].strAnswer, len );
pos += len;
}
}
if( lstUserQnA[i].dtUpdate > tMaxUpdateDate ){
tMaxUpdateDate = lstUserQnA[i].dtUpdate;
}
}
nDataSize = pos;
char* pDataBuf = new char[nDataSize];
int idData = g_pDataBuffers->NewDataBuffer( pQUReq->idUser, DTYPE_QAUPDATE, nDataSize, pDataBuf, pQUReq->iClientType );
if(idData>0){ //正常创建传输数据
memcpy( pDataBuf, pData, nDataSize );
pQURsp->tUpdateDate = tMaxUpdateDate;
pQURsp->nDataSize = nDataSize;
pQURsp->idData = idData;
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pQURsp->nDataSize = 0;
pQURsp->idData = 0;
}
else{ //重复创建
pQURsp->idData = -idData;
pQURsp->tUpdateDate = tMaxUpdateDate;
pQURsp->nDataSize = nDataSize;
}
delete[] pDataBuf;
}
delete[] pData;
}
else{
pQURsp->tUpdateDate = tUpdateDate;
pQURsp->idData = 0;
pQURsp->nDataSize = 0;
}
pResponseMsg->nLength = sizeof(TQnAUpdateResponse);
}
}
break;
case e_qnaSubmitRequest:
{
TQnASubmitResponse * pQSRsp = (TQnASubmitResponse*)pResponseMsg->byMessageBody;
WORD vMajor, vMinor, vModify, vBuild;
int idUser = 0, iCType = 0;
char *strQuestion = NULL, strVersion[32] = {0};
if( iMsgVer<0x0400 ){
TQnASubmitRequest0 * pQSReq = (TQnASubmitRequest0*)pRequestMsg->byMessageBody;
idUser = pQSReq->idUser;
iCType = pQSReq->iClientType;
strQuestion = pQSReq->strQuestion;
vMinor = iCType;
vMajor = ( iMsgVer==0x300 && iCType==1 ) ? 2 : 1;
vModify = ( iMsgVer==0x300 && iCType==0 ) ? 4 : 1;
if( iMsgVer==0x000 ){
vBuild = 718;
}
else if( iMsgVer==0x100 ){
if( iCType==0 )
vBuild = 929;
else
vBuild = 820;
}
else if( iMsgVer==0x200 ){
vBuild = 1030;
}
else if( iMsgVer==0x300 ){
if( iCType==2 )
vBuild = 127;
else
vBuild = 117;
}
}
else{
TQnASubmitRequest * pQSReq = (TQnASubmitRequest*)pRequestMsg->byMessageBody;
idUser = pQSReq->idUser;
iCType = iClientType;
strQuestion = pQSReq->strQuestion;
vMajor = HIWORD(pQSReq->dwVersionMS);
vMinor = LOWORD(pQSReq->dwVersionMS);
vModify = HIWORD(pQSReq->dwVersionLS);
vBuild = LOWORD(pQSReq->dwVersionLS);
}
sprintf( strVersion, "%d.%d.%d.%d", vMajor, vMinor, vModify, vBuild );
//DebugPrint( "msgver: 0x%x, len: %d, client: %s\n question: %s", iMsgVer, pRequestMsg->nLength, strVersion, strQuestion );
int id = f_DB_SubmitQuestion( idUser, iCType, strQuestion, strVersion );
if( id>0 ){
pQSRsp->idQnA = id;
}
else{
pResponseMsg->iExtra = e_DatabaseError;
}
//DebugPrint( "qna id: %d", id );
pResponseMsg->nLength = sizeof(TQnASubmitResponse);
}
break;
case e_noticeUpdateRequest:
{
TNoticeUpdateRequest * pNUReq = (TNoticeUpdateRequest*)pRequestMsg->byMessageBody;
TNoticeUpdateResponse* pNURsp = (TNoticeUpdateResponse*)pResponseMsg->byMessageBody;
vector<UINT> lstInvalidID;
TNOTICEITEM lstNotices[MAX_NOTICE_NUM];
TNOTICEITEMLIST lstNewNotice;
iRespExtra = pNUReq->idUser;
if( pNUReq->idUser ){
int num = f_DB_GetNotice( MAX_NOTICE_NUM, lstNotices, pNUReq->idUser );
for( int i=0; i<num; i++ ){
lstNewNotice.push_back( &lstNotices[i] );
}
}
else{
for( int i=0; i<g_nPublicNoticeNum; i++ ){
lstNewNotice.push_back( &g_lstPublicNotice[i] );
}
}
for( int i=0; i<pNUReq->nNoticeNum; i++ ){
UINT id = pNUReq->aIdNotices[i];
bool bInvalid = true;
for( int j=0; j<lstNewNotice.size(); j++ ){
if( id==lstNewNotice[j]->id ){
lstNewNotice.erase( lstNewNotice.begin() + j );
bInvalid = false;
break;
}
}
if( bInvalid ){
lstInvalidID.push_back( id );
}
}
int nUpdateNum = lstInvalidID.size() + lstNewNotice.size();
if( nUpdateNum>0 ){
//分配数据缓冲区
int nDataSize = nUpdateNum * 980;
char* pData = new char[nDataSize];
*(int*)pData = nUpdateNum;
int ind, pos = 4;
for( ind=0; ind<lstInvalidID.size(); ind++ ){
*(UINT*)(pData+pos) = lstInvalidID[ind]; pos+=4;
*(USHORT*)(pData+pos) = 2; pos+=2;
}
for( ind=0; ind<lstNewNotice.size(); ind++ ){
*(UINT*)(pData+pos) = lstNewNotice[ind]->id; pos+=4;
*(USHORT*)(pData+pos) = 0; pos+=2;
*(USHORT*)(pData+pos) = lstNewNotice[ind]->iType; pos+=2;
int len = strlen( lstNewNotice[ind]->strTitle );
*(UINT*)(pData+pos) = len; pos+=4;
if( len>0 ){
memcpy( pData+pos, lstNewNotice[ind]->strTitle, len );
pos += len;
}
len = strlen( lstNewNotice[ind]->strContent );
*(UINT*)(pData+pos) = len; pos+=4;
if( len>0 ){
memcpy( pData+pos, lstNewNotice[ind]->strContent, len );
pos += len;
}
}
nDataSize = pos;
char* pDataBuf = new char[nDataSize];
int idData = g_pDataBuffers->NewDataBuffer( pNUReq->idUser, DTYPE_NTUPDATE, nDataSize, pDataBuf, pNUReq->nNoticeNum );
if(idData>0){ //正常创建传输数据
memcpy( pDataBuf, pData, nDataSize );
pNURsp->nDataSize = nDataSize;
pNURsp->idData = idData;
}
else{
if(idData==0){ //创建传输数据失败
pResponseMsg->iExtra = e_ServiceBusy;
pNURsp->nDataSize = 0;
pNURsp->idData = 0;
}
else{ //重复创建
pNURsp->idData = -idData;
pNURsp->nDataSize = nDataSize;
}
delete[] pDataBuf;
}
delete[] pData;
}
else{
pNURsp->idData = 0;
pNURsp->nDataSize = 0;
}
pResponseMsg->nLength = sizeof(TNoticeUpdateResponse);
}
break;
case e_styleScoreRequest:
{
UINT idStyle, idUser;
int scrUser, iType = e_ST_Origin;
if( pRequestMsg->nLength==sizeof(TStyleScoreRequest) ){
TStyleScoreRequest * pSSReq = (TStyleScoreRequest*)pRequestMsg->byMessageBody;
idStyle = pSSReq->idStyle;
idUser = pSSReq->idUser;
iType = pSSReq->iType;
scrUser = pSSReq->scrUser;
}
else{
TStyleScoreRequest0 * pSSReq = (TStyleScoreRequest0*)pRequestMsg->byMessageBody;
idStyle = pSSReq->idStyle;
idUser = pSSReq->idUser;
scrUser = pSSReq->scrUser;
}
TStyleScoreResponse* pSSRsp = (TStyleScoreResponse*)pResponseMsg->byMessageBody;
iRespExtra = idStyle<<16 + iType<<8 + scrUser;
int scrAverage = -1;
bool bGet = scrUser<0;
int ret = f_DB_StyleScore( iType, idStyle, &scrAverage, true );
if( ret==0 ){
int oldAverage = scrAverage;
if( bGet ){
ret = GetStyleUserScore( iType, idStyle, idUser, &scrUser, NULL );
}
else{
ret = GetStyleUserScore( iType, idStyle, idUser, &scrUser, &scrAverage );
if( ret==0 && oldAverage!=scrAverage )
ret = f_DB_StyleScore( iType, idStyle, &scrAverage, false );
}
if( ret==0 ){
pSSRsp->scrUser = scrUser;
pSSRsp->scrAverage = scrAverage;
pResponseMsg->nLength = sizeof(TStyleScoreResponse);
}
}
if( ret==-2 ){
pResponseMsg->iExtra = e_ServiceBusy;
}
else if( ret==1 ){
pResponseMsg->iExtra = e_InvalidStyleID;
}
else if( ret!=0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
}
break;
case e_webURLRequest:
{
TIntRequest * pUrlReq = (TIntRequest*)pRequestMsg->byMessageBody;
iRespExtra = pUrlReq->iReq;
char sURL[256];
ret = f_DB_GetWebURL( pUrlReq->iReq, sURL );
if( ret<0 ){
pResponseMsg->iExtra = e_DatabaseError;
}
else{
TStringResponse * pUrlRsp = (TStringResponse*) pResponseMsg->byMessageBody;
strcpy(pUrlRsp->sResp, sURL);
pResponseMsg->nLength = strlen(sURL); //sizeof(TStringResponse);
}
}
break;
default:
return false;
}
if( pResponseMsg->iExtra!=e_DatabaseError && pResponseMsg->iExtra!=e_ServiceBusy
&& pRequestMsg->eMessageType!=e_dataSendRequest && pRequestMsg->eMessageType!=e_dataReceiveRequest ){
if(g_pResponses->NewResponse( iRespExtra, pResponseMsg )){
CheckMemoryUsage(pResponseMsg->eMessageType);
}
}
return true;
}
int CreateStyle( int idUser, int iType, int nDataSize, char* pData )
{
int ret = 0;
if( !pData ){
return e_InvalidData;
}
if( iType==DTYPE_STYLEFILE || iType==DTYPE_STYLETEMP ){
TMAKESTYLEINFO info;
int pos = 0, size = 0;
info.price = *(short*)&pData[pos];
pos += 2;
info.cost = *(short*)&pData[pos];
pos += 2;
size = (*(short*)&pData[pos]);
if( StyleTrans2Info( info.detail, (char*)(pData+pos+2) ) ){
ret = f_DB_InsertStyle( iType, &info );
}
else{
return e_InvalidData;
}
if( ret<=0 ){
return e_DatabaseError;
}
char strFileName[256], strInfoName[256];
char buf[20];
strcpy( strFileName, g_strStyleFileDir );
if( iType==DTYPE_STYLETEMP ){
strcat( strFileName, "Temp/" );
#ifdef _WIN32
if( _chdir(strFileName) == -1)
_mkdir(strFileName);
#else
if( chdir(strFileName) == -1)
mkdir(strFileName, S_IRWXU|S_IRWXG);
#endif
}
else{
UINT nDir = ((UINT)ret-1) >> 13;
sprintf( buf, "%08X/", nDir );
strcat( strFileName, buf );
#ifdef _WIN32
if( _chdir(strFileName) == -1)
_mkdir(strFileName);
#else
if( chdir(strFileName) == -1)
mkdir(strFileName, S_IRWXU|S_IRWXG);
#endif
}
sprintf( buf, "%08X.YSI", ret );
strcpy( strInfoName, strFileName );
strcat( strInfoName, buf );
FILE *pFile = fopen( strInfoName, "wb" );
if( !pFile ){
int err = GetLastError();
DebugPrint( "创建风格信息 %s 失败, err=%d", strInfoName, err );
return e_DatabaseError;
}
else{
size += sizeof(short);
fwrite( pData+pos, 1, size, pFile );
fclose( pFile );
DebugPrint( "创建风格信息 %s 成功, len=%d", strInfoName, size );
pos += size;
}
sprintf( buf, "%08X.YSF", ret );
strcat( strFileName, buf );
pFile = fopen( strFileName, "wb" );
if( !pFile ){
int err = GetLastError();
DebugPrint( "创建风格 %s 失败, err=%d", strFileName, err );
return e_DatabaseError;
}
else{
size = nDataSize-pos;
fwrite( pData+pos, 1, size, pFile );
fclose( pFile );
DebugPrint( "创建风格 %s 成功, len=%d", strFileName, size );
ret = 0;
}
}
else{
TSTYLESHARE info;
int size = *(short*)pData;
if( ShareTrans2Info( info, (char*)(pData+2) ) ){
ret = f_DB_InsertShareStyle( &info );
}
else{
return e_InvalidData;
}
if( ret<=0 ){
return e_DatabaseError;
}
char strFileName[256];
char buf[20];
strcpy( strFileName, g_strStyleFileDir );
strcat( strFileName, "Share/" );
#ifdef _WIN32
if( _chdir(strFileName) == -1)
_mkdir(strFileName);
#else
if( chdir(strFileName) == -1)
mkdir(strFileName, S_IRWXU|S_IRWXG);
#endif
UINT nDir = ((UINT)ret-1) >> 13;
sprintf( buf, "%08X/", nDir );
strcat( strFileName, buf );
#ifdef _WIN32
if( _chdir(strFileName) == -1)
_mkdir(strFileName);
#else
if( chdir(strFileName) == -1)
mkdir(strFileName, S_IRWXU|S_IRWXG);
#endif
sprintf( buf, "%08X.YSI", ret );
strcat( strFileName, buf );
FILE *pFile = fopen( strFileName, "wb" );
if( !pFile ){
int err = GetLastError();
DebugPrint( "创建分享风格 %s 失败, err=%d", strFileName, err );
ret = e_DatabaseError;
}
else{
size += sizeof(short);
fwrite( pData, 1, size, pFile );
fclose( pFile );
DebugPrint( "创建分享风格 %s 成功, len=%d", strFileName, nDataSize );
ret = 0;
}
}
return ret;
}
int GetStyleInfo( int iType, UINT id, int& nDataSize, char** ppData )
{
char strFileName[256];
char buf[20];
strcpy( strFileName, g_strStyleFileDir );
if( iType==e_ST_Temp ){
strcat( strFileName, "Temp/" );
}
else{
if( iType==e_ST_Share ){
strcat( strFileName, "Share/" );
}
UINT nDir = ((UINT)id-1) >> 13;
sprintf( buf, "%08X/", nDir );
strcat( strFileName, buf );
}
sprintf( buf, "%08X.YSI", id );
strcat( strFileName, buf );
FILE *fp = fopen( strFileName, "rb" );
if( !fp ){
int err = GetLastError();
DebugPrint( "打开%d类风格%d失败, err=%d, fn=%s", iType, id, err, strFileName );
return -1;
}
else{
short len = 0;
fread( &len, 1, sizeof(short), fp );
if( len>0 ){
*ppData = new char[len];
fread( *ppData, 1, len, fp );
}
fclose( fp );
nDataSize = len;
}
return 0;
}
int GetStyleData( int iType, UINT id, int& nDataSize, char** ppData )
{
char strFileName[256];
char buf[20];
strcpy( strFileName, g_strStyleFileDir );
if( iType==e_ST_Temp ){
strcat( strFileName, "Temp/" );
}
else{
UINT nDir = ((UINT)id-1) >> 13;
sprintf( buf, "%08X/", nDir );
strcat( strFileName, buf );
}
sprintf( buf, "%08X.YSF", id );
strcat( strFileName, buf );
FILE *fp = fopen( strFileName, "rb" );
if( !fp ){
int err = GetLastError();
DebugPrint( "打开风格%d失败, err=%d", id, err );
return -1;
}
else{
fseek( fp, 0, SEEK_END );
nDataSize = ftell( fp );
if( nDataSize>0 ){
*ppData = new char[nDataSize];
fseek( fp, 0, SEEK_SET );
fread( *ppData, 1, nDataSize, fp );
}
fclose( fp );
}
return 0;
}
int PublishStyle( UINT idTemp, UINT idStyle )
{
FILE* fp = NULL;
char strTempPath[256], strStylePath[256],
strTempFile[256], strStyleFile[256],
buf[20];
strcpy( strTempPath, g_strStyleFileDir );
strcat( strTempPath, "Temp/" );
UINT nDir = ((UINT)idStyle-1) >> 13;
sprintf( buf, "%08X/", nDir );
strcpy( strStylePath, g_strStyleFileDir );
strcat( strStylePath, buf );
sprintf( strTempFile, "%s%08X.YSI", strTempPath, idTemp );
fp = fopen( strTempFile, "rb" );
if( fp ){
fclose( fp );
}
else{
int err = GetLastError();
DebugPrint( "打开临时风格%d失败, err=%d", idTemp, err );
return -1;
}
sprintf( strStyleFile, "%s%08X.YSI", strStylePath, idStyle );
fp = fopen( strStyleFile, "rb" );
if( fp ){
fclose( fp );
remove( strStyleFile );
}
rename( strTempFile, strStyleFile );
sprintf( strTempFile, "%s%08X.YSF", strTempPath, idTemp );
fp = fopen( strTempFile, "rb" );
if( fp ){
fclose( fp );
}
else{
int err = GetLastError();
DebugPrint( "打开临时风格%d失败, err=%d", idTemp, err );
return -1;
}
sprintf( strStyleFile, "%s%08X.YSF", strStylePath, idStyle );
fp = fopen( strStyleFile, "rb" );
if( fp ){
fclose( fp );
remove( strStyleFile );
}
rename( strTempFile, strStyleFile );
return 0;
}
int GetIconFile( char* strFile, int& nDataSize, char** ppData )
{
char strFullPath[256];
sprintf( strFullPath, "%sUser/Portrait/%s", g_strWebFileDir, strFile );
FILE *fp = fopen( strFullPath, "rb" );
if( !fp ){
int err = GetLastError();
DebugPrint( "打开用户头像%s失败, err=%d", strFullPath, err );
return -1;
}
else{
fseek( fp, 0, SEEK_END );
nDataSize = ftell( fp );
if( nDataSize>0 ){
*ppData = new char[nDataSize];
fseek( fp, 0, SEEK_SET );
fread( *ppData, 1, nDataSize, fp );
}
fclose( fp );
}
return 0;
}
#pragma pack (2)
typedef struct{
UINT id;
short score;
} TUserScore;
#pragma pack()
int GetStyleUserScore( int iType, UINT idStyle, UINT idUser, int* pScrUser, int* pScrAverage )
{
bool bGet = !pScrAverage;
TUserScore * array = NULL;
char strFileName[256];
char buf[20];//, evtName[40]="";
int scrUser = *pScrUser,
scrAverage = bGet ? -1 : *pScrAverage;
strcpy( strFileName, g_strStyleFileDir );
if( iType==e_ST_Share ){
strcat( strFileName, "Share/" );
// strcat( evtName, "Share" );
}
UINT nDir = ((UINT)idStyle-1) >> 13;
sprintf( buf, "%08X/", nDir );
strcat( strFileName, buf );
sprintf( buf, "%08X.SCR", idStyle );
strcat( strFileName, buf );
// strcat( evtName, buf );
//创建互斥变量
#ifdef _WIN32
HANDLE hNamedEvent = CreateEvent(
NULL, // no security attributes
FALSE, // auto reset event
TRUE, // signalled
evtName ); // named event
if( WAIT_OBJECT_0!=WaitForSingleObject( hNamedEvent, 2000 ) ){
DebugPrint( "!!!Wait for event %s time out", buf );
SetEvent( hNamedEvent );
return -2;
}
// #else
// sem_t* sema = sem_open(evtName, O_CREAT, 0666, 1);
// if( sema==SEM_FAILED ){
// DebugPrint("!!!Create sema for %s error", buf);
// return -2;
// }
// else{
// struct timespec ts;
// clock_gettime(CLOCK_REALTIME, &ts);
// ts.tv_sec += 2;
// if( sem_timedwait(sema, &ts)==-1 ){
// DebugPrint( "!!!Wait for event %s error or time out", buf );
// sem_post(sema);
// return -2;
// }
// }
#endif
int num = 0;
FILE *fp = NULL;
if( bGet )
fp = fopen( strFileName, "rb" );
else{
if( !(fp = fopen( strFileName, "r+b" )) )
fp = fopen( strFileName, "wb" );
}
if( fp ){
#ifndef _WIN32
int fd = fileno(fp);
bool bLocked = false;
for(int i=0; i<2 ; i++){
if(flock(fd, LOCK_NB | (bGet ? LOCK_SH : LOCK_EX))==-1){
sleep(1);
}
else{
bLocked = true;
break;
}
}
if(!bLocked){
fclose(fp);
DebugPrint( "!!!Lock file %s error or time out", buf );
return -2;
}
#endif
fseek( fp, 0, SEEK_SET );
if( fread( &num, 1, sizeof(int), fp ) < sizeof(int) )
num = 0;
array = (TUserScore*)malloc( sizeof(TUserScore) * (num+1) );
int index = num;
if( num>0 ){
fread( array, sizeof(TUserScore), num, fp );
for( int i=0; i<num; i++ ){
if( array[i].id==idUser ){
index = i;
if( bGet )
scrUser = array[i].score;
break;
}
}
}
if( !bGet && ( index==num || scrUser!=array[index].score )){
TUserScore thisScore;
thisScore.id = idUser;
thisScore.score = scrUser;
int count = num<20 ? 20 : num;
double dblTotal = scrAverage * count;
if( index<num ){
dblTotal -= array[index].score;
}
else{
num++;
count++;
fseek( fp, 0, SEEK_SET );
fwrite( &num, sizeof(int), 1, fp );
}
fseek( fp, sizeof(int) + sizeof(TUserScore) * index, SEEK_SET );
fwrite( &thisScore, sizeof(TUserScore), 1, fp );
dblTotal += scrUser;
scrAverage = (int)( dblTotal / count + 0.5 );
}
free( array );
#ifndef _WIN32
flock(fd, LOCK_UN);
#endif
fclose( fp );
}
else{
DebugPrint("open file %s error %d", strFileName, errno);
}
#ifdef _WIN32
SetEvent( hNamedEvent );
// #else
// sem_post(sema);
// sem_close(sema);
// sem_unlink(evtName);
#endif
*pScrUser = scrUser;
if( !bGet ) *pScrAverage = scrAverage;
return 0;
}
int CreateSong( int idUser, int iClientType, int nDataSize, char* pData, TSHAREINFO* pShareInfo )
{
int ret = 0;
if( !pData || nDataSize<sizeof(TSONGINFO0) ){
return e_InvalidData;
}
struct tm *tmTime;
time_t tTime;
time( &tTime );
tmTime = localtime( &tTime );
char strFullPath[256];
char strSubdir[128];
char strFile[32];
sprintf( strSubdir, "%d%02d/", tmTime->tm_year+1900, tmTime->tm_mon+1 );
sprintf( strFile, "%d_%d.mp3", idUser, (UINT)tTime );
if( iClientType>=e_CZ_Android && iClientType<=e_Ac_iOS )
sprintf( strFullPath, "%sMusic/Music/%s", g_strWebFileDir, strSubdir );
else
sprintf( strFullPath, "%sMusic/Music/%s", g_strMusicFileDir, strSubdir );
#ifdef _WIN32
if( _chdir(strFullPath) == -1)
_mkdir(strFullPath);
#else
if( chdir(strFullPath) == -1)
mkdir(strFullPath, S_IRWXU|S_IRWXG);
#endif
strcat( strFullPath, strFile );
FILE *pFile = fopen( strFullPath, "wb" );
if( !pFile ){
int err = GetLastError();
DebugPrint( "create song %s failed, err=%d", strFullPath, err );
ret = e_DatabaseError;
}
else{
long nSongSize = nDataSize - sizeof(TSONGINFO0);
fwrite( pData+sizeof(TSONGINFO0), 1, nSongSize, pFile );
fclose( pFile );
DebugPrint( "create song %s success, len=%d", strFile, nSongSize );
TSONGINFO0 * pSongInfo = (TSONGINFO0*)pData;
strcat( strSubdir, strFile );
int iExtra = 0;
char strCover[200];
//再次检查用户上传权限
ret = f_DB_CheckUserAuthority( idUser, 0 );
if( ret==0 ){
iExtra = e_InvalidAuthority;
}
else if( ret>0 ){
char loc[8] = "";
if( iClientType==e_Ac_Windows || iClientType==e_Pro_Windows )
strcpy( loc, g_strMusicLocation );
ret = f_DB_InsertSong( idUser, iClientType, pSongInfo, nSongSize, strSubdir, strCover, loc );
}
if( ret==0 ){
iExtra = e_DatabaseError;
}
if( iExtra==0 ){
pShareInfo->idSong = ret;
sprintf( pShareInfo->strSongURL, "iChangzuo/File/Music/Music/%s", strSubdir );
sprintf( pShareInfo->strCoverURL, "iChangzuo/File/%s", strCover );
}
else{
if( remove( strFullPath )==-1 ){
DebugPrint( "delete song %s -- failed", strFullPath );
}
else{
DebugPrint( "delete song %s -- success", strFullPath );
}
}
ret = iExtra;
}
return ret;
}
bool StyleTrans2Info( TSTYLEDETAIL& info, char* pData )
{
int size = 0;
// fix part
memcpy( &info, pData, 20 );
size += 20;
// var part
int len;
len = (BYTE)pData[size++];
memcpy( info.strAuthor, &pData[size], len );
info.strAuthor[len] = '\0';
size += len;
len = (BYTE)pData[size++];
memcpy( info.strStyleName, &pData[size], len );
info.strStyleName[len] = '\0';
size += len;
len = (BYTE)pData[size++];
memcpy( info.strDescription, &pData[size], len );
info.strDescription[len] = '\0';
return true;
}
bool ShareTrans2Info( TSTYLESHARE& info, char* pData )
{
int size = 0;
// fix part
memcpy( &info, pData, 64 );
size += 64;
// var part
int len;
len = (BYTE)pData[size++];
memcpy( info.strAuthor, &pData[size], len );
info.strAuthor[len] = '\0';
size += len;
len = (BYTE)pData[size++];
memcpy( info.strStyleName, &pData[size], len );
info.strStyleName[len] = '\0';
size += len;
len = (BYTE)pData[size++];
memcpy( info.strDescription, &pData[size], len );
info.strDescription[len] = '\0';
return true;
}