示例#1
0
DWORD WINAPI ClientWriteToFileThread(LPVOID lpParameter)
{
    DWORD byteswrittenfile = 0;
    char sizeBuf[SERVER_PACKET_SIZE];
    char writeBuf[SERVER_PACKET_SIZE];
    char delim[4] = {4, 4, 4, '\0'}, *ptrEnd, *ptrBegin = writeBuf;
    int packetSize;
    bool lastPacket = false;
    hReceiveFile = (HANDLE) lpParameter;

    while(!lastPacket)
    {
        if (circularBufferRecv->length > 0 && (circularBufferRecv->length % 2) == 0)
        {
            circularBufferRecv->pop(sizeBuf);
            sizeBuf[5] = '\0';
            packetSize = strtol(sizeBuf, NULL, 10);
            circularBufferRecv->pop(writeBuf);
            if ((ptrEnd = strstr(writeBuf, delim)))
            {
                lastPacket = true;
                packetSize = ptrEnd - ptrBegin;
            }
            if (WriteFile(hReceiveFile, writeBuf, packetSize, &byteswrittenfile, NULL) == FALSE)
            {
                qDebug() << "Couldn't write to server file\n";
                ShowLastErr(false);
                return FALSE;
            }
        }
    }
    return TRUE;
}
/*---------------------------------------------------------------------------------------
--	FUNCTION:   ClientSend
--
--
--	DATE:			April 7, 2016
--
--	REVISIONS:
--
--	DESIGNERS:		Micah Willems
--
--	PROGRAMMER:		Micah Willems
--
--  INTERFACE:      int ClientSend(HANDLE hFile)
--
--                      HANDLE hFile: a handle to the file to be sent
--
--  RETURNS:        Returns 0 on success, -1 on failure
--
--	NOTES:
--      This is the function that starts the thread of the control channel connection
---------------------------------------------------------------------------------------*/
int ClientSendRequest(int flag)
{
    HANDLE hThread;
    DWORD ThreadId;

    if ((hThread = CreateThread(NULL, 0, ClientControlThreadSend, (LPVOID)flag, 0, &ThreadId)) == NULL)
    {
        ShowLastErr(false);
        qDebug() << "Create Control Channel Thread failed";
        return -1;
    }
    return 0;
}
DWORD WINAPI ClientControlThreadSend(LPVOID lpParameter)
{
    char *sendbuff = (char *)calloc(CONTROL_PACKET_SIZE + 1, sizeof(char));
    char *recvbuff = (char *)calloc(CONTROL_PACKET_SIZE + 1, sizeof(char));
    char *message = (char *)calloc(CONTROL_PACKET_SIZE + 1, sizeof(char));
    int flag = (int)lpParameter;
    int sentb = 0, recvb = 1, totalb = 0, i = 0;
    wchar_t path[260];

    switch (flag)
    {
    case GET_UPDATE_SONG_LIST:
        memset(recvbuff, 0, sizeof(recvbuff));
        memset(message, 0, sizeof(message));
        sendbuff[0] = flag;
        sentb = send(controlSock, sendbuff, CONTROL_PACKET_SIZE, 0);
        numSongs = 0;
        songRequestDone = 0;
        while(recvb != SOCKET_ERROR)
        {
            recvb = recv(controlSock, recvbuff, CONTROL_PACKET_SIZE, 0);
            strcat(message, recvbuff);
            totalb += recvb;
            if (totalb >= CONTROL_PACKET_SIZE)
            {
                if (message[0] == 0)
                {
                    break;
                }
                songList[i] = new char[100];
                strcpy(songList[i], message);
                i++;
                numSongs++;
                totalb -= CONTROL_PACKET_SIZE;
                memset(message, 0, sizeof(message));
            }
        }
        songRequestDone = 1;
        break;
    case SEND_SONG_TO_SERVER:
        sendbuff[0] = flag;
        strcat(sendbuff, sendFileName);
        sentb = send(controlSock, sendbuff, CONTROL_PACKET_SIZE, 0);
        while(totalb < CONTROL_PACKET_SIZE && recvb != SOCKET_ERROR)
        {
            recvb = recv(controlSock, recvbuff, CONTROL_PACKET_SIZE, 0);
            totalb += recvb;
        }
        if (recvbuff[0] == 0)
        {
            sendSockClosed = ClientSendSetup(address, sendSock, SERVER_DEFAULT_PORT);
            ClientSend(hSendFile);
        }
        break;
    case GET_SONG_FROM_SERVER:
        sendbuff[0] = flag;
        strcat(sendbuff, recvFileName);
        mbstowcs(&path[0], recvFileName, strlen(recvFileName));
        qDebug() << recvFileName;
        qDebug() << QString::fromWCharArray(path);
        DeleteFile(path);
        hReceiveFile = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
        ShowLastErr(false);
        hReceiveClosed = 1;
        ClientReceiveSetup(listenSock, CLIENT_DEFAULT_PORT, acceptEvent);
        ClientListen();
        sentb = send(controlSock, sendbuff, CONTROL_PACKET_SIZE, 0);
        break;
    default:
        qDebug() << "Invalid request";
        break;
    }
    free(sendbuff);
    free(recvbuff);
    free(message);
    return TRUE;
}