0

Pipe is being used, If server call WriteFile/ReadFile, client have to call ReadFile/WriteFile for synchronize each other.I implemented it like this.

Example :

**COMMON : **

BOOL RecvData(DWORD &dwScore)
{
    DWORD dwRead = 0;
    if(0 == ReadFile(g_hPipe, &dwScore, sizeof(DWORD), &dwRead, 0))
        return FALSE;
    if (sizeof(DWORD) != dwRead)
        return FALSE;
    return TRUE;
}
BOOL SendData(DWORD dwScore)
{
    DWORD dwSend = 0;
    if(0 == WriteFile(g_hPipe, &dwScore, sizeof(DWORD), &dwSend, 0))
        return FALSE;
    if (sizeof(DWORD) != dwSend)
        return FALSE;
    return TRUE;
}

**SERVER : **

VOID StartServerManager()
{
    DWORD dwScore = 0;
    while (TRUE)
    {
        if(FALSE == RecvData(dwScore))
            continue;
        //.... do something dwScore + dwSkill ... 
        if(FALSE == SendData(dwScore))
            continue;

    }
}

**CLIENT : **

#define VILLA_READ_MODE 0xDEDC
#define VILLA_WRITE_MODE 0xCDEC

VOID StartClientManager()
{
    DWORD dwScore = 4;
    DWORD dwMode = VILLA_WRITE_MODE;
    while(TRUE)
    {
        switch(dwMode)
        {
        case VILLA_WRITE_MODE:
            SendData(dwScore);
            dwMode = VILLA_READ_MODE;
            break;
        case VILLA_READ_MODE:
            RecvData(dwScore);
            dwMode = VILLA_WRITE_MODE;
            break;
        }
    }
}

If I change StartServerManager() into like this

VOID StartServerManager()
{
    DWORD dwScore = 0;
    while (TRUE)
    {
        if(FALSE == RecvData(dwScore))
            continue;
        //.... do something dwScore + dwSkill ... 

        if(FALSE == SendData(dwScore))
            continue;

        if(FALSE == RecvData(dwScore))
            continue;

        if(FALSE == RecvData(dwScore))
            continue;

    }
}

Function will block at ReadFile()

if(0 == ReadFile(g_hPipe, &dwScore, sizeof(DWORD), &dwRead, 0))

BOOL RecvData(DWORD &dwScore)
{
    DWORD dwRead = 0;
    if(0 == ReadFile(g_hPipe, &dwScore, sizeof(DWORD), &dwRead, 0))
        return FALSE;
    if (sizeof(DWORD) != dwRead)
        return FALSE;
    return TRUE;
}

Implemented with threads, too. Is there any method pass block on ReadFile? I thought about this...

#define VILLA_READ_MODE 0xDEDC
#define VILLA_WRITE_MODE 0xCDEC
VOID StartClientManager()
{
    HANDLE hThread[2];
    DWORD dwModeRead = VILLA_READ_MODE;
    DWORD dwModeWrite = VILLA_WRITE_MODE;
    hThread[1] = CreateThread(NULL, 0, SYNCTHREAD, &dwModeRead, 0, 0);
    hThread[2] = CreateThread(NULL, 0, SYNCTHREAD, &dwModeWrite, 0, 0);
    WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
}
DWORD WINAPI SYNCTHREAD(DWORD &dwMode)
{
    switch (dwMode)
    {
    case VILLA_READ_MODE:
        RecvData(dwScore);
        break;
    case VILLA_WRITE_MODE:
        SendData(dwScore);
        break;
    }
}
Bryant
  • 324
  • 2
  • 15
  • in `SendData` need `WriteFile` use instead `ReadFile` or this is typo ? – RbMm Jul 12 '17 at 15:29
  • thank you. RbMm. It was my mistake. – Bryant Jul 12 '17 at 15:34
  • The client always alternates between reading and writing, so it makes no sense for the server to try to read two values in a row. What are you actually trying to achieve here? – Harry Johnston Jul 13 '17 at 00:16
  • If server send data(WriteFile) to client, client function ReadFile will pass. – Bryant Jul 13 '17 at 03:11
  • If server send data(WriteFile) to client, 1. client function ReadFile will pass > then analysis the received data > 2. WriteFile() -> Data > 3. Wait for server data at ReadFile() again. But in step 3., if server doesn't send data, only want to ReadFile, it's blocked at Client&Server ReadFile. So I Want to know a method of how set ReadFile, WriteFile function time out. Even if I solve it, 30% is solved. Thankyou Harry Johnston. Have a good day. – Bryant Jul 13 '17 at 03:19
  • 1
    Well ... you can abort a synchronous ReadFile using threads and the CancelSynchronousIo function, or you could use asynchronous I/O instead. But note that timeouts are unlikely to resolve your problem. Also, the code in the latest edit does not appear to make any sense. I suspect that you are confused about what you are trying to achieve, and adding timeouts is only going to make things worse. (If your goal is to share a variable between two processes, consider using shared memory and a named mutex instead of a pipe.) – Harry Johnston Jul 13 '17 at 20:00

0 Answers0