Code:
void OnAccept(const SOCKET DataSocket)
{
LPFTPDSession FTPDSessionInfo = NULL;
EnterCriticalSection(&LinkListSession);
FTPDSessionInfo = GetInfo(DataSocket);
LeaveCriticalSection(&LinkListSession);
if (FTPDSessionInfo == NULL)
{
return ;
}
SendReadyInfo(FTPDSessionInfo);
}
void OnWrite(const SOCKET DataSocket)
{
LPFTPDSession FTPDSessionInfo = NULL;
EnterCriticalSection(&LinkListSession);
FTPDSessionInfo = GetInfo(DataSocket);
LeaveCriticalSection(&LinkListSession);
if (FTPDSessionInfo == NULL)
{
return ;
}
switch(FTPDSessionInfo->TransferAction)
{
case DOWNLOADFILE:
printf("Send File\n");
SendFileContent(DataSocket,FTPDSessionInfo);
break;
case UPLOADFILE:
break;
case LISTFILE:
printf("Send List\n");
SendListContent(FTPDSessionInfo);
default:
break;
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_LISTENSOCKET)
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
if (!WSAGETSELECTERROR(lParam))
{
HandleIncomingConnection(wParam,hwnd);
}
break;
case FD_CLOSE:
break;
}
return 0;
}
if (uMsg == WM_CONTROLSOCKET)
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
HandleCommand(wParam);
break;
case FD_WRITE:
break;
case FD_CLOSE:
FreeClientSession(wParam);
DecrementClientCount();
printf("ControlSocket %d Disconnected\n",wParam);
break;
}
return 0;
}
if (uMsg == WM_DATASOCKET)
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
printf("DataSocket %d Accepted\n",wParam);
OnAccept((SOCKET)wParam);
break;
case FD_CONNECT:
printf("Data Socket %d Connected\n",wParam);
break;
case FD_READ:
break;
case FD_WRITE:
printf("Data Socket %d Is Writable\n",wParam);
OnWrite((SOCKET)wParam);
printf("OnWrite Complete\n");
break;
case FD_CLOSE:
printf("DataSocket %d Disconnected\n",wParam);
break;
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
BOOL SendFileContent(const SOCKET DataSocket,LPFTPDSession FTPDSessionInfo)
{
if (FTPDSessionInfo->FileHandle == INVALID_HANDLE_VALUE || FTPDSessionInfo->FileBuffer == NULL)
{
return FALSE;
}
BOOL Flag = FALSE;
DWORD ByteToRead = 0;
if (FTPDSessionInfo->TransferBytes > 0)
{
if (myFileSeek(FTPDSessionInfo->FileHandle, FTPDSessionInfo->TransferBytes,FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
return FALSE;
}
printf("Reset %d\n",FTPDSessionInfo->TransferBytes);
}
while(TRUE)
{
if (FTPDSessionInfo->PostedBytes == READYSEND)
{
if (!ReadFile(FTPDSessionInfo->FileHandle,FTPDSessionInfo->FileBuffer,1024,&ByteToRead,NULL))
{
printf("POut Here\n");
break;
}
if (ByteToRead == 0)
{
printf("Out Here %d\n",GetLastError());
break;
}
}
int nByteSent = send(DataSocket,FTPDSessionInfo->FileBuffer,ByteToRead,0);
if (nByteSent == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("Error\n");
break;
}
nByteSent = 0;
}
FTPDSessionInfo->TransferBytes += nByteSent;
if (FTPDSessionInfo->FileSize > 0)
{
FTPDSessionInfo->Completion = (float)FTPDSessionInfo->TransferBytes / FTPDSessionInfo->FileSize * 100;
}
if (nByteSent < (int)ByteToRead)
{
printf("Buffer Full\n");
return FALSE;
}
printf("%d:Post %d Bytes\n",DataSocket,FTPDSessionInfo->TransferBytes);
}
CleanUP Stuff
}
I try to code a ftpd with asynchronous functions,but I found a problem.whenevev the ftpd sends about 400K data thru data socket,the buffer is full,so I return(see the code SendFileContent()),but for my surprise,the ftpd won't generate a new FD_WRITE event for that data socket again even there is room for the buffer.
Can you take a glance and tell me what's wrong?
thanks a lot