Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Pipe.h"
- Pipe::~Pipe()
- {
- loopstop = true;
- DisconnectNamedPipe(hPipe);
- CloseHandle(hPipe);
- }
- Pipe::Pipe(wstring str_Pipe_Name,void* ReadProc)
- {
- ReadThisPipe = ReadProc;
- hPipe = CreateNamedPipe(str_Pipe_Name.c_str(),PIPE_ACCESS_DUPLEX,
- PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,
- PIPE_UNLIMITED_INSTANCES,BUFSIZE,BUFSIZE,0,0);
- startread = false;
- loopstop = false;
- while(hPipe == INVALID_HANDLE_VALUE)
- {
- Sleep(1000);
- }
- ConnectNamedPipe(hPipe, NULL);
- startread = true;
- for(;;)
- {
- if( ConnectNamedPipe(hPipe, NULL)==0)
- if(GetLastError()==ERROR_NO_DATA)
- {
- DisconnectNamedPipe(hPipe);
- ConnectNamedPipe(hPipe, NULL);
- }
- Sleep(1000);
- }
- }
- bool Pipe::SendToPipe(BYTE bID,BYTE bSetting,BYTE b3,BYTE b4,BYTE b5)
- {
- DWORD dwcbWritten;
- BYTE cbData[5];
- cbData[0] = bID;
- cbData[1] = bSetting;
- cbData[2] = b3;
- cbData[3] = b4;
- cbData[4] = b5;
- return (bool)WriteFile(hPipe, (LPCVOID)cbData, sizeof(cbData), &dwcbWritten, NULL);
- }
- bool Pipe::byteToBool(byte b)
- {
- return (b == 0 ? false : true);
- }
- void Pipe::StartReading()
- {
- DWORD ThreadID;
- CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)&ReadThisPipe,0, 0, &ThreadID);
- }
- void GetAnswerToRequest(__in char* szRequest)
- {
- }
- DWORD ReadClient(Pipe* cPipe)
- {
- char chRequest[4096];
- char chReply[4096];
- DWORD cbBytesRead, cbReplyBytes, cbWritten;
- while(!cPipe->loopstop)
- {
- if(cPipe->startread)
- {
- while(ReadFile(cPipe->hPipe, chRequest, BUFSIZE, &cbBytesRead, NULL) >0)
- {
- GetAnswerToRequest(chRequest);
- Sleep(20);
- }
- Sleep(100);
- cPipe->SendToPipe(0,0,0,0,0);
- }
- else
- Sleep(500);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment