Implemented the socket in unix

This commit is contained in:
Micke Prag 2010-10-12 13:17:06 +00:00
parent 4865a63f0c
commit 549a146ce9

View file

@ -1,18 +1,31 @@
#include "Socket.h" #include "Socket.h"
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#define BUFSIZE 512
using namespace TelldusCore; using namespace TelldusCore;
int connectWrapper(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
connect(sockfd, addr, addrlen);
}
class Socket::PrivateData { class Socket::PrivateData {
public: public:
SOCKET_T socket;
}; };
Socket::Socket() { Socket::Socket() {
d = new PrivateData; d = new PrivateData;
d->socket = 0;
} }
Socket::Socket(SOCKET_T hPipe) Socket::Socket(SOCKET_T socket)
{ {
d = new PrivateData; d = new PrivateData;
d->socket = socket;
} }
Socket::~Socket(void) { Socket::~Socket(void) {
@ -20,12 +33,33 @@ Socket::~Socket(void) {
} }
void Socket::connect(const std::wstring &server) { void Socket::connect(const std::wstring &server) {
struct sockaddr_un remote;
socklen_t len;
if ((d->socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
return;
}
std::string name = "/tmp/" + std::string(server.begin(), server.end());
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, name.c_str());
len = SUN_LEN(&remote);
if (connectWrapper(d->socket, (struct sockaddr *)&remote, len) == -1) {
return;
}
} }
std::wstring Socket::read() { std::wstring Socket::read() {
return L""; char inbuf[BUFSIZE];
memset(inbuf, '\0', sizeof(inbuf));
size_t inlen = recv(d->socket, inbuf, BUFSIZE - 1, 0);
std::string msg(inbuf);
return std::wstring(msg.begin(), msg.end());
} }
void Socket::write(const std::wstring &msg) { void Socket::write(const std::wstring &msg) {
std::string newMsg(msg.begin(), msg.end());
send(d->socket, newMsg.c_str(), newMsg.length(), 0);
} }