diff options
Diffstat (limited to 'daemon/OlySocket.cpp')
-rwxr-xr-x[-rw-r--r--] | daemon/OlySocket.cpp | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/daemon/OlySocket.cpp b/daemon/OlySocket.cpp index 8a9ca97..a3bf746 100644..100755 --- a/daemon/OlySocket.cpp +++ b/daemon/OlySocket.cpp | |||
@@ -46,7 +46,7 @@ OlySocket::OlySocket(int port, bool multiple) { | |||
46 | } | 46 | } |
47 | 47 | ||
48 | OlySocket::OlySocket(int port, char* host) { | 48 | OlySocket::OlySocket(int port, char* host) { |
49 | fdServer = 0; | 49 | mFDServer = 0; |
50 | createClientSocket(host, port); | 50 | createClientSocket(host, port); |
51 | } | 51 | } |
52 | 52 | ||
@@ -70,11 +70,11 @@ void OlySocket::closeSocket() { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | void OlySocket::closeServerSocket() { | 72 | void OlySocket::closeServerSocket() { |
73 | if (CLOSE_SOCKET(fdServer) != 0) { | 73 | if (CLOSE_SOCKET(mFDServer) != 0) { |
74 | logg->logError(__FILE__, __LINE__, "Failed to close server socket."); | 74 | logg->logError(__FILE__, __LINE__, "Failed to close server socket."); |
75 | handleException(); | 75 | handleException(); |
76 | } | 76 | } |
77 | fdServer = 0; | 77 | mFDServer = 0; |
78 | } | 78 | } |
79 | 79 | ||
80 | void OlySocket::createClientSocket(char* hostname, int portno) { | 80 | void OlySocket::createClientSocket(char* hostname, int portno) { |
@@ -95,8 +95,9 @@ void OlySocket::createClientSocket(char* hostname, int portno) { | |||
95 | handleException(); | 95 | handleException(); |
96 | } | 96 | } |
97 | for (res=res0; res!=NULL; res = res->ai_next) { | 97 | for (res=res0; res!=NULL; res = res->ai_next) { |
98 | if ( res->ai_family != PF_INET || res->ai_socktype != SOCK_STREAM ) | 98 | if ( res->ai_family != PF_INET || res->ai_socktype != SOCK_STREAM ) { |
99 | continue; | 99 | continue; |
100 | } | ||
100 | mSocketID = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | 101 | mSocketID = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
101 | if (mSocketID < 0) { | 102 | if (mSocketID < 0) { |
102 | continue; | 103 | continue; |
@@ -105,7 +106,9 @@ void OlySocket::createClientSocket(char* hostname, int portno) { | |||
105 | close(mSocketID); | 106 | close(mSocketID); |
106 | mSocketID = -1; | 107 | mSocketID = -1; |
107 | } | 108 | } |
108 | if (mSocketID > 0) break; | 109 | if (mSocketID > 0) { |
110 | break; | ||
111 | } | ||
109 | } | 112 | } |
110 | freeaddrinfo(res0); | 113 | freeaddrinfo(res0); |
111 | if (mSocketID <= 0) { | 114 | if (mSocketID <= 0) { |
@@ -124,15 +127,15 @@ void OlySocket::createSingleServerConnection(int port) { | |||
124 | 127 | ||
125 | void OlySocket::createServerSocket(int port) { | 128 | void OlySocket::createServerSocket(int port) { |
126 | // Create socket | 129 | // Create socket |
127 | fdServer = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | 130 | mFDServer = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
128 | if (fdServer < 0) { | 131 | if (mFDServer < 0) { |
129 | logg->logError(__FILE__, __LINE__, "Error creating server socket"); | 132 | logg->logError(__FILE__, __LINE__, "Error creating server socket"); |
130 | handleException(); | 133 | handleException(); |
131 | } | 134 | } |
132 | 135 | ||
133 | // Enable address reuse, another solution would be to create the server socket once and only close it when the object exits | 136 | // Enable address reuse, another solution would be to create the server socket once and only close it when the object exits |
134 | int on = 1; | 137 | int on = 1; |
135 | if (setsockopt(fdServer, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) != 0) { | 138 | if (setsockopt(mFDServer, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) != 0) { |
136 | logg->logError(__FILE__, __LINE__, "Setting server socket options failed"); | 139 | logg->logError(__FILE__, __LINE__, "Setting server socket options failed"); |
137 | handleException(); | 140 | handleException(); |
138 | } | 141 | } |
@@ -145,13 +148,13 @@ void OlySocket::createServerSocket(int port) { | |||
145 | sockaddr.sin_addr.s_addr = INADDR_ANY; | 148 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
146 | 149 | ||
147 | // Bind the socket to an address | 150 | // Bind the socket to an address |
148 | if (bind(fdServer, (const struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) { | 151 | if (bind(mFDServer, (const struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) { |
149 | logg->logError(__FILE__, __LINE__, "Binding of server socket failed.\nIs an instance already running?"); | 152 | logg->logError(__FILE__, __LINE__, "Binding of server socket failed.\nIs an instance already running?"); |
150 | handleException(); | 153 | handleException(); |
151 | } | 154 | } |
152 | 155 | ||
153 | // Listen for connections on this socket | 156 | // Listen for connections on this socket |
154 | if (listen(fdServer, 1) < 0) { | 157 | if (listen(mFDServer, 1) < 0) { |
155 | logg->logError(__FILE__, __LINE__, "Listening of server socket failed"); | 158 | logg->logError(__FILE__, __LINE__, "Listening of server socket failed"); |
156 | handleException(); | 159 | handleException(); |
157 | } | 160 | } |
@@ -160,13 +163,13 @@ void OlySocket::createServerSocket(int port) { | |||
160 | // mSocketID is always set to the most recently accepted connection | 163 | // mSocketID is always set to the most recently accepted connection |
161 | // The user of this class should maintain the different socket connections, e.g. by forking the process | 164 | // The user of this class should maintain the different socket connections, e.g. by forking the process |
162 | int OlySocket::acceptConnection() { | 165 | int OlySocket::acceptConnection() { |
163 | if (fdServer <= 0) { | 166 | if (mFDServer <= 0) { |
164 | logg->logError(__FILE__, __LINE__, "Attempting multiple connections on a single connection server socket or attempting to accept on a client socket"); | 167 | logg->logError(__FILE__, __LINE__, "Attempting multiple connections on a single connection server socket or attempting to accept on a client socket"); |
165 | handleException(); | 168 | handleException(); |
166 | } | 169 | } |
167 | 170 | ||
168 | // Accept a connection, note that this call blocks until a client connects | 171 | // Accept a connection, note that this call blocks until a client connects |
169 | mSocketID = accept(fdServer, NULL, NULL); | 172 | mSocketID = accept(mFDServer, NULL, NULL); |
170 | if (mSocketID < 0) { | 173 | if (mSocketID < 0) { |
171 | logg->logError(__FILE__, __LINE__, "Socket acceptance failed"); | 174 | logg->logError(__FILE__, __LINE__, "Socket acceptance failed"); |
172 | handleException(); | 175 | handleException(); |
@@ -230,8 +233,9 @@ int OlySocket::receiveString(char* buffer, int size) { | |||
230 | int bytes_received = 0; | 233 | int bytes_received = 0; |
231 | bool found = false; | 234 | bool found = false; |
232 | 235 | ||
233 | if (buffer == 0) | 236 | if (buffer == 0) { |
234 | return 0; | 237 | return 0; |
238 | } | ||
235 | 239 | ||
236 | while (!found && bytes_received < size) { | 240 | while (!found && bytes_received < size) { |
237 | // Receive a single character | 241 | // Receive a single character |