Cleaning up drivers/Cpp, Cpp/Lifxtest, virtuals, and iotrmi/C++ (revisiting the C...
[iot2.git] / iotjava / iotrmi / C++ / IoTSocket.hpp
index d39eba78329747ff0e99dd7ef066c08c0ba97ade..d967ded098860167ed9e97d94b93dd803efbc9f2 100644 (file)
@@ -32,6 +32,7 @@ static const int MSG_LEN_SIZE = 4;
 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#include <mutex>
 
 #include "IoTRMIUtil.hpp"
 
@@ -50,19 +51,19 @@ class IoTSocket {
                IoTSocket(int iPort, bool* pResult);
                ~IoTSocket();
 
-               bool                            close();                                                                // Close the socket
-               bool                            sendBytes(char* pVals, int _iLen);              // Send a set of bytes
-               char*                           receiveBytes(char* pVals, int* len);    // Receive a set of bytes
+               bool                    close();                                // Close the socket
+               bool                    sendBytes(char* pVals, int _iLen);      // Send a set of bytes
+               char*                   receiveBytes(char* pVals, int* len);    // Receive a set of bytes
 
        protected:              
-               int                                     m_iPort;                                                        // Port I'm listening on
-               int                                     m_iSock;                                                        // Socket connection
-               struct sockaddr_in      m_addrRemote;                                           // Connector's address information
-               double*                         m_pBuffer;                                                      // Reuse the same memory for buffer
+               int                     m_iPort;                                // Port I'm listening on
+               int                     m_iSock;                                // Socket connection
+               struct sockaddr_in      m_addrRemote;                           // Connector's address information
+               double*                 m_pBuffer;                              // Reuse the same memory for buffer
 
        private:
-               bool                            receiveAck();
-               bool                            sendAck();
+               bool                    receiveAck();
+               bool                    sendAck();
 };
 
 
@@ -109,8 +110,6 @@ bool IoTSocket::sendBytes(char* pVals, int iLen) {
                return false;
        }
 
-       //IoTRMIUtil::printBytes(size, 4, false);
-
        if (send(m_iSock, (char *) pVals, iLen, 0) == -1) {
                perror("IoTSocket: Send bytes error!");
                return false;
@@ -134,12 +133,12 @@ char* IoTSocket::receiveBytes(char* pVals, int* len)
        // Critical section that is used by different objects
        lock_guard<mutex> guard(recvBytesMutex);
 
-       int                     i                               = 0;
-       int                     j                               = 0;
-       char*           pTemp                   = NULL;
+       int                     i                       = 0;
+       int                     j                       = 0;
+       char*                   pTemp                   = NULL;
        int                     iTotalBytes             = 0;
        int                     iNumBytes               = 0;
-       bool            bEnd                    = false;
+       bool                    bEnd                    = false;
 
        int iTotal = 0;
        int iResult = 0;
@@ -149,6 +148,7 @@ char* IoTSocket::receiveBytes(char* pVals, int* len)
                iResult = recv(m_iSock, size, MSG_LEN_SIZE, 0);         
                iTotal += iResult;
        }
+
        if (iResult == -1) {
                perror("IoTSocket: Receive size error!");
                return NULL;