Move the communication to Socket.
This commit is contained in:
parent
9100a0bdcf
commit
56ae5048aa
3 changed files with 41 additions and 87 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "telldus-core.h"
|
#include "telldus-core.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "Socket.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
@ -185,95 +186,12 @@ char * WINAPI tdLastSentValue( int intDeviceId ) {
|
||||||
* @returns an integer of the total number of devices configured
|
* @returns an integer of the total number of devices configured
|
||||||
*/
|
*/
|
||||||
int WINAPI tdGetNumberOfDevices(void){
|
int WINAPI tdGetNumberOfDevices(void){
|
||||||
DWORD dwMode;
|
TelldusCore::Socket s(L"TelldusClient");
|
||||||
bool fSuccess = false;
|
|
||||||
|
|
||||||
//Convert our std::string to std::wstring since we build agains win32 with unicode support
|
|
||||||
//std::string strName = "\\\\.\\pipe\\" + server;
|
|
||||||
std::wstring name(L"\\\\.\\pipe\\TelldusClient");
|
|
||||||
HANDLE hPipe = CreateFile(
|
|
||||||
(const wchar_t *)name.c_str(), // pipe name
|
|
||||||
GENERIC_READ | // read and write access
|
|
||||||
GENERIC_WRITE,
|
|
||||||
0, // no sharing
|
|
||||||
NULL, // default security attributes
|
|
||||||
OPEN_EXISTING, // opens existing pipe
|
|
||||||
FILE_FLAG_OVERLAPPED, // default attributes
|
|
||||||
NULL); // no template file
|
|
||||||
|
|
||||||
if (hPipe == INVALID_HANDLE_VALUE) {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
dwMode = PIPE_READMODE_MESSAGE;
|
|
||||||
fSuccess = SetNamedPipeHandleState(
|
|
||||||
hPipe, // pipe handle
|
|
||||||
&dwMode, // new pipe mode
|
|
||||||
NULL, // don't set maximum bytes
|
|
||||||
NULL); // don't set maximum time
|
|
||||||
|
|
||||||
if (!fSuccess) {
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
OVERLAPPED oOverlap;
|
|
||||||
DWORD bytesWritten = 0;
|
|
||||||
int result;
|
|
||||||
char buf[512]; //TODO #define BUFSIZE 512
|
|
||||||
DWORD cbBytesRead = 0;
|
|
||||||
|
|
||||||
oOverlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
|
|
||||||
std::wstring msg = L"20:tdGetNumberOfDevices";
|
std::wstring msg = L"20:tdGetNumberOfDevices";
|
||||||
WriteFile(hPipe, msg.data(), (DWORD)msg.length()*sizeof(wchar_t), &bytesWritten, &oOverlap);
|
s.write(msg);
|
||||||
|
|
||||||
result = WaitForSingleObject(oOverlap.hEvent, 10000);
|
|
||||||
if (result == WAIT_TIMEOUT) {
|
|
||||||
CloseHandle(oOverlap.hEvent);
|
|
||||||
return 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
fSuccess = GetOverlappedResult(hPipe, &oOverlap, &bytesWritten, false);
|
|
||||||
CloseHandle(oOverlap.hEvent);
|
|
||||||
if (!fSuccess) {
|
|
||||||
return 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
std::wstring response = s.read();
|
||||||
//READ //debug
|
return 1;
|
||||||
HANDLE writeEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
|
|
||||||
oOverlap.hEvent = writeEvent; //TODO: correct?
|
|
||||||
fSuccess = false;
|
|
||||||
|
|
||||||
memset(&buf, 0, 512); //#define BUFSIZE 512
|
|
||||||
|
|
||||||
/*
|
|
||||||
if (!d->connected) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
ReadFile(hPipe, &buf, sizeof(char)*512, &cbBytesRead, &oOverlap); //#define BUFSIZE 512
|
|
||||||
|
|
||||||
result = WaitForSingleObject(writeEvent, 10000); //TODO timeout
|
|
||||||
if (result == WAIT_TIMEOUT) {
|
|
||||||
//CloseHandle(d->readEvent);
|
|
||||||
//d->readEvent = INVALID_HANDLE_VALUE;
|
|
||||||
return 9;
|
|
||||||
}
|
|
||||||
fSuccess = GetOverlappedResult(hPipe, &oOverlap, &cbBytesRead, false);
|
|
||||||
if (!fSuccess) {
|
|
||||||
DWORD err = GetLastError();
|
|
||||||
if (err == ERROR_BROKEN_PIPE) {
|
|
||||||
//d->connected = false;
|
|
||||||
}
|
|
||||||
buf[0] = 0;
|
|
||||||
}
|
|
||||||
//CloseHandle(d->readEvent);
|
|
||||||
//d->readEvent = INVALID_HANDLE_VALUE;
|
|
||||||
//return buf;
|
|
||||||
|
|
||||||
return 2; //TODO, decode:a buf-en
|
|
||||||
//return buf[0]; //TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -14,6 +14,7 @@ namespace TelldusCore {
|
||||||
class Socket
|
class Socket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Socket(const std::wstring &server);
|
||||||
Socket(SOCKET_T hPipe);
|
Socket(SOCKET_T hPipe);
|
||||||
virtual ~Socket(void);
|
virtual ~Socket(void);
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,43 @@ using namespace TelldusCore;
|
||||||
class Socket::PrivateData {
|
class Socket::PrivateData {
|
||||||
public:
|
public:
|
||||||
HANDLE hPipe;
|
HANDLE hPipe;
|
||||||
|
bool connected;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Socket::Socket(const std::wstring &server) {
|
||||||
|
d = new PrivateData;
|
||||||
|
d->connected = false;
|
||||||
|
|
||||||
|
bool fSuccess = false;
|
||||||
|
|
||||||
|
std::wstring name(L"\\\\.\\pipe\\" + server);
|
||||||
|
d->hPipe = CreateFile(
|
||||||
|
(const wchar_t *)name.c_str(), // pipe name
|
||||||
|
GENERIC_READ | // read and write access
|
||||||
|
GENERIC_WRITE,
|
||||||
|
0, // no sharing
|
||||||
|
NULL, // default security attributes
|
||||||
|
OPEN_EXISTING, // opens existing pipe
|
||||||
|
FILE_FLAG_OVERLAPPED, // default attributes
|
||||||
|
NULL); // no template file
|
||||||
|
|
||||||
|
if (d->hPipe == INVALID_HANDLE_VALUE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD dwMode = PIPE_READMODE_MESSAGE;
|
||||||
|
fSuccess = SetNamedPipeHandleState(
|
||||||
|
d->hPipe, // pipe handle
|
||||||
|
&dwMode, // new pipe mode
|
||||||
|
NULL, // don't set maximum bytes
|
||||||
|
NULL); // don't set maximum time
|
||||||
|
|
||||||
|
if (!fSuccess) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
d->connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
Socket::Socket(SOCKET_T hPipe)
|
Socket::Socket(SOCKET_T hPipe)
|
||||||
{
|
{
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue