클라이언트가 연결을 시도 할 때까지 내 명명 된 파이프 서버가 ConnectNamedPipe 함수의 블록 대기를 건너 뛰려고합니다. 그래서 내 코드는 끝날 때까지 ConnetNamedPipe 라인을지나 계속하지만 backgroun에서 열린 파이프 연결을 유지해야합니다. 파이프를 만들 때 PIPE_NOWAIT 모드를 사용하면 imediatelly가 반환되고 클라이언트가 연결되기 전에 파이프가 닫힙니다.명명 된 파이프는 클라이언트가 연결할 때까지 백그라운드에서 클라이언트를 대기합니다.
스레드를 사용하여이 작업을 시도했지만 스레드를 생성하고 스레드 내에서 코드의 ConnectNamedPipe 부분을 실행하더라도 코드가 계속 실행되는 대신이 줄에서 계속 대기합니다. 일단 내 서버 cpp 파일 코드 끝에 도달하면 클라이언트와 파이프에 연결하려고합니다.
내 파이프 :
hPipe = CreateNamedPipe(
lpszPipename,
PIPE_ACCESS_OUTBOUND, // one way access, only send data
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
512,
512,
0,
NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
Out->msg(ERR,"Creating the pipe failed.");
}
// Create a thread for this client.
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
InstanceThread, // thread proc
(LPVOID) hPipe, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hThread == NULL)
{
Out->msg(ERR,"Creating the thread failed.");
}
else CloseHandle(hThread);
// Close the pipe.
CloseHandle(hPipe);
내 스레드 :
DWORD WINAPI InstanceThread(LPVOID lpvParam)
{
Out->msg(output::SEV_INFO,"Waiting for client to connect.");
fConnected = ConnectNamedPipe(hPipe, NULL) ? //This is where the execution hangs
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
HANDLE hHeap = GetProcessHeap();
TCHAR* pchReply = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(TCHAR));
DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0;
BOOL fSuccess = FALSE;
HANDLE hPipe = NULL;
// The thread's parameter is a handle to a pipe object instance.
hPipe = (HANDLE) lpvParam;
uint32_t startTime = time(NULL);
uint32_t elapsedTime;
// Loop until done reading
while ((elapsedTime < 60))
{
elapsedTime = difftime(time(NULL), startTime);
// Write to the pipe.
fSuccess = WriteFile(
hPipe, // handle to pipe
pchReply, // buffer to write from
cbReplyBytes, // number of bytes to write
&cbWritten, // number of bytes written
NULL); // not overlapped I/O
if (!fSuccess || cbReplyBytes != cbWritten)
{
Out->msg(ERR,"InstanceThread WriteFile failed.");
break;
}
}
// Flush the pipe to allow the client to read the pipe's contents
// before disconnecting. Then disconnect the pipe, and close the
// handle to this pipe instance.
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
HeapFree(hHeap, 0, pchReply);
Out->msg(output::SEV_INFO,"InstanceThread exitting.\n");
return 1;
}
[Named Pipe 서버, 클라이언트 연결 대기 및 들어오는 데이터에 대한 인터럽트 또는 시간 초과 방법] 가능한 복제본 (http://stackoverflow.com/questions/35828965/named-pipes-server-how-to-interrupt - 또는 - 시간 초과 - 클라이언트 - 연결 대기 -) 대답 –