Y
ydzi_
Unregistered / Unconfirmed
GUEST, unregistred user!
typedef struct tagSocketItem
{
SOCKET SocketHandle;
void* SocketData; // 这里在DELPHI中怎么写?
}tSocketItem, * ptSocketItem;
const SIZEOF_SOCKETITEM = sizeof(tSocketItem);
typedef struct tagFragment
{
int FragmentSize; // Size of fragmentBuf
void* FragmentBuf;
}tFragment, * ptFragment;
const SIZEOF_FRAGMENT = sizeof(tFragment);
UINT SocketReceiveThread(LPVOID pParam) // 这里在DELPHI中怎么写?
{
char SocketReceiveBuf[1024];
int ReceivedLen, iSocketHandle;
POSITION SocketIndex;
ptSocketItem pSocketItem;
ptFragment pFragment;
CString sInfo;
BOOL AppendResult, ParseResult;
// 这里在DELPHI中是什么类型?
SOCKET m_SocketSocket;
sockaddr_in m_SockAddr;
int m_LenOfSockAddr;
CPtrList m_SocketList;
CCriticalSection m_SocketListLock;
CCriticalSection m_AppendFragmentLock, m_ParseMsgLock, m_ClearFragmentLock;
CEvent m_evtExitSocketReceiveThread;
CLogicCardManagerDlg* pDlg = (CLogicCardManagerDlg*)AfxGetApp()->GetMainWnd();
// 接受连接
iSocketHandle = accept(pDlg->m_ServerSocket, (sockaddr*)&(pDlg->m_SockAddr),
&(pDlg->m_LenOfSockAddr));
if(iSocketHandle == INVALID_SOCKET){ // accept错误
AfxEndThread(0);
return 0;
}
pSocketItem = (ptSocketItem)malloc(SIZEOF_SOCKETITEM);
if(pSocketItem == NULL){ // 无可用内存
AfxEndThread(0);
return 0;
}
ZeroMemory(pSocketItem, SIZEOF_SOCKETITEM);
pSocketItem->SocketHandle = iSocketHandle;
// 加入SocketList
pDlg->m_SocketListLock.Lock();
try{
pDlg->m_SocketList.AddTail(pSocketItem);
}
catch(...){
}
pDlg->m_SocketListLock.Unlock();
// 启动新的Socket接收线程
AfxBeginThread(SocketReceiveThread, 0);
// 接收Socket数据
while(WaitForSingleObject(pDlg->m_evtExitSocketReceiveThread, 1) != WAIT_OBJECT_0){
ReceivedLen = recv(pSocketItem->SocketHandle, SocketReceiveBuf, 1024, 0);
if(ReceivedLen <= 0){
break;
}
// 判断Socket缓冲区是否已有数据
if(pSocketItem->SocketData){
pFragment = (ptFragment)(pSocketItem->SocketData);
}
else{ // 分配新的Fragment
pFragment = (ptFragment)malloc(SIZEOF_FRAGMENT);
if(pFragment == NULL){
break;
}
ZeroMemory(pFragment, SIZEOF_FRAGMENT);
// 将SocketData与Fragment绑定
pSocketItem->SocketData = pFragment;
}
// 判断Fragment中是否已有数据
if(pFragment->FragmentBuf == NULL){
pFragment->FragmentSize = ReceivedLen;
pFragment->FragmentBuf = SocketReceiveBuf;
}
else{
// 拼接前后收到的数据, 注意线程安全
pDlg->m_AppendFragmentLock.Lock();
try{
AppendResult = pDlg->AppendFragment(pFragment, SocketReceiveBuf, ReceivedLen);
}
catch(...){
}
pDlg->m_AppendFragmentLock.Unlock();
// 拼接数据失败
if(!AppendResult){
pDlg->m_ClearFragmentLock.Lock();
try{
pDlg->ClearFragment(pFragment);
}
catch(...){
}
pDlg->m_ClearFragmentLock.Unlock();
free(pFragment);
pSocketItem->SocketData = NULL;
break;
}
}
// 解析完整的Socket消息, 注意线程安全
pDlg->m_ParseMsgLock.Lock();
try{
ParseResult = pDlg->ParseWholeMsgFromFragment(pFragment, pSocketItem->SocketHandle);
}
catch(...){
}
pDlg->m_ParseMsgLock.Unlock();
// 解析失败
if(!ParseResult){
pDlg->m_ClearFragmentLock.Lock();
try{
pDlg->ClearFragment(pFragment);
}
catch(...){
}
pDlg->m_ClearFragmentLock.Unlock();
free(pFragment);
pSocketItem->SocketData = NULL;
}
else{
if(pFragment->FragmentSize == 0){ // 所有消息处理完毕
free(pFragment);
pSocketItem->SocketData = NULL;
}
}
} // end of while...
// 关闭连接
closesocket(pSocketItem->SocketHandle);
// 释放资源
SocketIndex = pDlg->m_SocketList.Find(pSocketItem);
if(SocketIndex != NULL){
pDlg->m_SocketListLock.Lock();
try{
pDlg->m_SocketList.RemoveAt(SocketIndex);
}
catch(...){
}
pDlg->m_SocketListLock.Unlock();
free(pSocketItem);
}
AfxEndThread(0);
return 0;
}
请大侠帮忙解决一下!问题解决马上结帐,谢谢。。。
{
SOCKET SocketHandle;
void* SocketData; // 这里在DELPHI中怎么写?
}tSocketItem, * ptSocketItem;
const SIZEOF_SOCKETITEM = sizeof(tSocketItem);
typedef struct tagFragment
{
int FragmentSize; // Size of fragmentBuf
void* FragmentBuf;
}tFragment, * ptFragment;
const SIZEOF_FRAGMENT = sizeof(tFragment);
UINT SocketReceiveThread(LPVOID pParam) // 这里在DELPHI中怎么写?
{
char SocketReceiveBuf[1024];
int ReceivedLen, iSocketHandle;
POSITION SocketIndex;
ptSocketItem pSocketItem;
ptFragment pFragment;
CString sInfo;
BOOL AppendResult, ParseResult;
// 这里在DELPHI中是什么类型?
SOCKET m_SocketSocket;
sockaddr_in m_SockAddr;
int m_LenOfSockAddr;
CPtrList m_SocketList;
CCriticalSection m_SocketListLock;
CCriticalSection m_AppendFragmentLock, m_ParseMsgLock, m_ClearFragmentLock;
CEvent m_evtExitSocketReceiveThread;
CLogicCardManagerDlg* pDlg = (CLogicCardManagerDlg*)AfxGetApp()->GetMainWnd();
// 接受连接
iSocketHandle = accept(pDlg->m_ServerSocket, (sockaddr*)&(pDlg->m_SockAddr),
&(pDlg->m_LenOfSockAddr));
if(iSocketHandle == INVALID_SOCKET){ // accept错误
AfxEndThread(0);
return 0;
}
pSocketItem = (ptSocketItem)malloc(SIZEOF_SOCKETITEM);
if(pSocketItem == NULL){ // 无可用内存
AfxEndThread(0);
return 0;
}
ZeroMemory(pSocketItem, SIZEOF_SOCKETITEM);
pSocketItem->SocketHandle = iSocketHandle;
// 加入SocketList
pDlg->m_SocketListLock.Lock();
try{
pDlg->m_SocketList.AddTail(pSocketItem);
}
catch(...){
}
pDlg->m_SocketListLock.Unlock();
// 启动新的Socket接收线程
AfxBeginThread(SocketReceiveThread, 0);
// 接收Socket数据
while(WaitForSingleObject(pDlg->m_evtExitSocketReceiveThread, 1) != WAIT_OBJECT_0){
ReceivedLen = recv(pSocketItem->SocketHandle, SocketReceiveBuf, 1024, 0);
if(ReceivedLen <= 0){
break;
}
// 判断Socket缓冲区是否已有数据
if(pSocketItem->SocketData){
pFragment = (ptFragment)(pSocketItem->SocketData);
}
else{ // 分配新的Fragment
pFragment = (ptFragment)malloc(SIZEOF_FRAGMENT);
if(pFragment == NULL){
break;
}
ZeroMemory(pFragment, SIZEOF_FRAGMENT);
// 将SocketData与Fragment绑定
pSocketItem->SocketData = pFragment;
}
// 判断Fragment中是否已有数据
if(pFragment->FragmentBuf == NULL){
pFragment->FragmentSize = ReceivedLen;
pFragment->FragmentBuf = SocketReceiveBuf;
}
else{
// 拼接前后收到的数据, 注意线程安全
pDlg->m_AppendFragmentLock.Lock();
try{
AppendResult = pDlg->AppendFragment(pFragment, SocketReceiveBuf, ReceivedLen);
}
catch(...){
}
pDlg->m_AppendFragmentLock.Unlock();
// 拼接数据失败
if(!AppendResult){
pDlg->m_ClearFragmentLock.Lock();
try{
pDlg->ClearFragment(pFragment);
}
catch(...){
}
pDlg->m_ClearFragmentLock.Unlock();
free(pFragment);
pSocketItem->SocketData = NULL;
break;
}
}
// 解析完整的Socket消息, 注意线程安全
pDlg->m_ParseMsgLock.Lock();
try{
ParseResult = pDlg->ParseWholeMsgFromFragment(pFragment, pSocketItem->SocketHandle);
}
catch(...){
}
pDlg->m_ParseMsgLock.Unlock();
// 解析失败
if(!ParseResult){
pDlg->m_ClearFragmentLock.Lock();
try{
pDlg->ClearFragment(pFragment);
}
catch(...){
}
pDlg->m_ClearFragmentLock.Unlock();
free(pFragment);
pSocketItem->SocketData = NULL;
}
else{
if(pFragment->FragmentSize == 0){ // 所有消息处理完毕
free(pFragment);
pSocketItem->SocketData = NULL;
}
}
} // end of while...
// 关闭连接
closesocket(pSocketItem->SocketHandle);
// 释放资源
SocketIndex = pDlg->m_SocketList.Find(pSocketItem);
if(SocketIndex != NULL){
pDlg->m_SocketListLock.Lock();
try{
pDlg->m_SocketList.RemoveAt(SocketIndex);
}
catch(...){
}
pDlg->m_SocketListLock.Unlock();
free(pSocketItem);
}
AfxEndThread(0);
return 0;
}
请大侠帮忙解决一下!问题解决马上结帐,谢谢。。。