@@ -139,17 +139,20 @@ namespace MonetExplorer {
139139 return ;
140140 }
141141
142- // Close the outgoing channel
143- shutdown (this ->clientSocket , SHUT_WR);
142+ if (clientSocket > -1 ) {
143+ // Close the outgoing channel
144+ shutdown (this ->clientSocket , SHUT_WR);
144145
145- /*
146- After the server noticed that the client
147- closed its outgoing channel, it will also
148- do so. Read until that is detected.
149- */
150- while (this ->ReadExact (BUFFER_SIZE, false ) > 0 );
151- close (this ->clientSocket );
146+ /*
147+ After the server noticed that the client
148+ closed its outgoing channel, it will also
149+ do so. Read until that is detected.
150+ */
151+ while (this ->ReadExact (BUFFER_SIZE, false ) > 0 );
152+ close (this ->clientSocket );
153+ }
152154
155+ this ->clientSocket = -1 ;
153156 this ->connected = false ;
154157 }
155158
@@ -193,11 +196,11 @@ namespace MonetExplorer {
193196 * @brief Connect to the server though Unix
194197 * domain socket.
195198 *
196- * @param port The port of the server is part of the
197- * name of the files which represent these sockets.
198- * Therefore it's required for finding the files.
199+ * @param socketFilePath The path to the socket file.
200+ * It ends with the port of the server. Typical value
201+ * is: "/tmp/.s.monetdb.50000"
199202 */
200- void ConnectUnix (int port ) {
203+ void ConnectUnix (std::string socketFilePath ) {
201204 if (this ->connected ) {
202205 throw std::runtime_error (" Connection::ConnectUnix(): Already connected to the server. "
203206 " (Method is called twice.)" );
@@ -209,31 +212,31 @@ namespace MonetExplorer {
209212 + " ' (" + std::to_string (errno) + " )" );
210213 }
211214
212- std::vector<std::string> paths ({
213- " /tmp/.s.monetdb." + std::to_string (port)
214- });
215-
216- for (const std::string &path : paths) {
217- std::cout << " \033 [32mTrying: " << path << " \033 [0m\n " ;
218-
219- struct sockaddr_un serverAddress;
220- bzero (&serverAddress, sizeof (serverAddress));
221- serverAddress.sun_family = AF_UNIX;
222- std::memcpy (serverAddress.sun_path , path.c_str (), path.length () + 1 );
215+ struct sockaddr_un serverAddress;
216+ bzero (&serverAddress, sizeof (serverAddress));
217+ serverAddress.sun_family = AF_UNIX;
218+ std::memcpy (serverAddress.sun_path , socketFilePath.c_str (), socketFilePath.length () + 1 );
223219
224- if (connect (this ->clientSocket , (sockaddr*)&serverAddress, sizeof (serverAddress)) == 0 ) {
225- // See: https://github.com/MonetDB/MonetDB/blob/1f1bbdbd3340fdb74345723e8c98c120dcaf2ead/clients/mapilib/mapi.c#L2416
226- this ->buffer [0 ] = ' 0' ;
227- this ->WriteExact (1 );
228- this ->connected = true ;
229- return ;
230- }
220+ if (connect (this ->clientSocket , (sockaddr*)&serverAddress, sizeof (serverAddress)) == 0 ) {
221+ this ->connected = true ;
222+ return ;
231223 }
232224
233225 throw std::runtime_error (" Failed to connect to the server. Error: '"
234226 + std::string (strerror (errno)) + " ' (" + std::to_string (errno) + " )" );
235227 }
236228
229+ /* *
230+ * @brief The unix domain socket connection requires
231+ * that the client sends a single byte (without packet frame)
232+ * first with content 0x30 or '0'.
233+ * See: https://github.com/MonetDB/MonetDB/blob/1f1bbdbd3340fdb74345723e8c98c120dcaf2ead/clients/mapilib/mapi.c#L2416
234+ */
235+ void SendUnixDomainSocketInitByte () {
236+ this ->buffer [0 ] = ' 0' ;
237+ this ->WriteExact (1 );
238+ }
239+
237240 /* *
238241 * @brief Returns true if the client is connected
239242 * to the MonetDB server, false otherwise.
0 commit comments