1 #include "SocketThread.h"
2 #include "IException.h"
3 #include "Application.h"
8 #include <sys/socket.h>
37 struct sockaddr_un p_socketName;
38 p_socketName.sun_family = AF_UNIX;
39 strcpy(p_socketName.sun_path, p_socketFile.c_str());
43 if((p_socket = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
44 std::string msg =
"Unable to create socket";
45 std::cerr << msg << std::endl;
46 remove(p_socketFile.c_str());
52 fcntl(p_socket, F_SETFL, O_NONBLOCK);
55 int status = bind(p_socket, (
struct sockaddr *)&p_socketName,
sizeof(p_socketName));
57 std::string msg =
"Unable to bind to socket [" + p_socketFile +
"]";
58 std::cerr << msg << std::endl;
59 remove(p_socketFile.c_str());
64 if(listen(p_socket, 5) < 0) {
65 std::string msg =
"Unable to listen to socket [" + p_socketFile +
"]";
66 std::cerr << msg << std::endl;
67 remove(p_socketFile.c_str());
75 socklen_t len =
sizeof(&p_socketName);
76 int childSocket = accept(p_socket, (
struct sockaddr *)&p_socketName, &len);
78 if (errno == EWOULDBLOCK) {
88 QScopedPointer< char, QScopedPointerArrayDeleter<char> > buf(
90 if((bytes = recv(childSocket, buf.data(), 1024 * 1024, 0)) < 0) {
91 std::string msg =
"Unable to read from socket [" + p_socketFile +
"]";
92 std::cerr << msg << std::endl;
93 remove(p_socketFile.c_str());
99 for(
int i = 0; i < bytes; i++) buffer += buf.data()[i];
101 IString escape2 = QString(escape);
102 while(buffer.size() > 0) {
104 if(token ==
"raise") {