Commit 6502b3463c04f0a86b098665ccd1511a668d495e
1 parent
95926262
added suport for C-Strings in core class
Showing
4 changed files
with
76 additions
and
65 deletions
Show diff stats
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +./build*/ | ... | ... |
src/lib/socket.cpp
| ... | ... | @@ -11,7 +11,6 @@ |
| 11 | 11 | #include "socket.h" |
| 12 | 12 | |
| 13 | 13 | // TODO Remover Message Buffer |
| 14 | -const int lemoce::ClientSocket::MESSAGE_BUFFER = 8192; | |
| 15 | 14 | int lemoce::ServerSocket::BACKLOG = 512; |
| 16 | 15 | |
| 17 | 16 | lemoce::Socket::Socket() |
| ... | ... | @@ -50,12 +49,17 @@ void lemoce::Socket::close() |
| 50 | 49 | } |
| 51 | 50 | |
| 52 | 51 | |
| 53 | -lemoce::ClientSocket::ClientSocket() { } | |
| 54 | - | |
| 52 | +lemoce::ClientSocket::ClientSocket() | |
| 53 | +{ | |
| 54 | + bufferSize = 8192; | |
| 55 | +} | |
| 55 | 56 | |
| 56 | -lemoce::ClientSocket::ClientSocket(std::string remote, int port): | |
| 57 | - lemoce::Socket{}, remotePort(port), remoteHost{remote} { } | |
| 58 | 57 | |
| 58 | +lemoce::ClientSocket::ClientSocket(std::string remote, int port): ClientSocket{} | |
| 59 | +{ | |
| 60 | + remotePort = port; | |
| 61 | + remoteHost = remote; | |
| 62 | +} | |
| 59 | 63 | |
| 60 | 64 | void lemoce::ClientSocket::connect() |
| 61 | 65 | { |
| ... | ... | @@ -88,30 +92,14 @@ void lemoce::ClientSocket::connect() |
| 88 | 92 | localPort = lPort; |
| 89 | 93 | } |
| 90 | 94 | |
| 91 | -// TODO deletar essa ou de baixo | |
| 92 | 95 | std::unique_ptr<std::string> lemoce::ClientSocket::read() |
| 93 | 96 | { |
| 94 | - char chararray[MESSAGE_BUFFER + 1]; | |
| 97 | + char chararray[bufferSize]; | |
| 95 | 98 | std::ostringstream buffer; |
| 96 | 99 | int nread; |
| 97 | 100 | |
| 98 | - while (((nread = ::recv(getFd(), chararray, MESSAGE_BUFFER, 0)) < 0)) | |
| 99 | - { | |
| 100 | - if (errno == EINTR) | |
| 101 | - { | |
| 102 | - continue; | |
| 103 | - } | |
| 104 | - else | |
| 105 | - { | |
| 106 | - throw new SocketException{errno}; | |
| 107 | - } | |
| 108 | - } | |
| 109 | - | |
| 110 | - if (nread < MESSAGE_BUFFER) | |
| 111 | - { | |
| 112 | - chararray[nread] = '\0'; | |
| 113 | - } | |
| 114 | - | |
| 101 | + read(chararray, bufferSize); | |
| 102 | + | |
| 115 | 103 | buffer << chararray; |
| 116 | 104 | |
| 117 | 105 | std::unique_ptr<std::string> result{new std::string{buffer.str()}}; |
| ... | ... | @@ -119,49 +107,52 @@ std::unique_ptr<std::string> lemoce::ClientSocket::read() |
| 119 | 107 | return result; |
| 120 | 108 | } |
| 121 | 109 | |
| 122 | -// TODO Modificar para aceitar array de bytes | |
| 123 | -std::unique_ptr<std::string> lemoce::ClientSocket::read(const size_t n) | |
| 110 | +int lemoce::ClientSocket::read(char msg[], const size_t n) | |
| 124 | 111 | { |
| 125 | - std::ostringstream buffer; | |
| 126 | - | |
| 112 | + size_t nleft; | |
| 127 | 113 | ssize_t nread; |
| 114 | + char *ptr; | |
| 128 | 115 | |
| 129 | - size_t left = n; | |
| 130 | - char chararray[n]; | |
| 131 | - char * ptr = chararray; | |
| 132 | - | |
| 133 | - while (left > 0) | |
| 116 | + ptr = msg; | |
| 117 | + nleft = n; | |
| 118 | + | |
| 119 | + while (nleft > 0) | |
| 134 | 120 | { |
| 135 | 121 | if ((nread = ::recv(getFd(), ptr, n, 0)) < 0) |
| 136 | 122 | { |
| 137 | 123 | if (errno == EINTR) |
| 138 | - nread = 0; | |
| 124 | + { | |
| 125 | + nread = 0; | |
| 126 | + continue; | |
| 127 | + } | |
| 139 | 128 | else |
| 140 | - throw new SocketException{errno}; | |
| 129 | + treatSocketError(false); | |
| 141 | 130 | } |
| 142 | - else if (nread == 0) | |
| 143 | - break; | |
| 144 | 131 | |
| 145 | - left -= nread; | |
| 132 | + nleft -= nread; | |
| 146 | 133 | ptr = ptr + nread; |
| 134 | + break; | |
| 147 | 135 | } |
| 148 | 136 | |
| 149 | - buffer << chararray; | |
| 137 | + if (nleft > 0) | |
| 138 | + { | |
| 139 | + msg[n - nleft] = '\0'; | |
| 140 | + } | |
| 150 | 141 | |
| 151 | - std::unique_ptr<std::string> result{new std::string{buffer.str()}}; | |
| 152 | - | |
| 153 | - return std::move(result); | |
| 142 | + return (n - nleft); | |
| 154 | 143 | } |
| 155 | 144 | |
| 156 | - | |
| 157 | 145 | // TODO Modificar para array de bytes |
| 158 | 146 | void lemoce::ClientSocket::write(const std::string& message) |
| 159 | 147 | { |
| 148 | + write(message.c_str(), message.size()); | |
| 149 | +} | |
| 160 | 150 | |
| 161 | - size_t nleft = message.size(); | |
| 162 | - const char * ptr = message.c_str(); | |
| 163 | - | |
| 151 | +void lemoce::ClientSocket::write(const char * msg, size_t n) | |
| 152 | +{ | |
| 153 | + size_t nleft = n; | |
| 164 | 154 | ssize_t nwritten = nleft; |
| 155 | + const char * ptr = msg; | |
| 165 | 156 | |
| 166 | 157 | while (nleft > 0) |
| 167 | 158 | { |
| ... | ... | @@ -170,11 +161,11 @@ void lemoce::ClientSocket::write(const std::string& message) |
| 170 | 161 | if (nwritten < 0 && errno == EINTR) |
| 171 | 162 | nwritten = 0; |
| 172 | 163 | else |
| 173 | - throw new SocketException{errno}; | |
| 164 | + treatSocketError(false); | |
| 174 | 165 | } |
| 175 | 166 | nleft -= nwritten; |
| 176 | 167 | ptr += nwritten; |
| 177 | - } | |
| 168 | + } | |
| 178 | 169 | } |
| 179 | 170 | |
| 180 | 171 | void lemoce::ClientSocket::setRemoteHost(std::string rHost) |
| ... | ... | @@ -217,6 +208,17 @@ int lemoce::ClientSocket::getLocalPort() const |
| 217 | 208 | return localPort; |
| 218 | 209 | } |
| 219 | 210 | |
| 211 | +void lemoce::ClientSocket::setBufferSize(int size) | |
| 212 | +{ | |
| 213 | + bufferSize = size; | |
| 214 | +} | |
| 215 | + | |
| 216 | +int lemoce::ClientSocket::getBufferSize() const | |
| 217 | +{ | |
| 218 | + return bufferSize; | |
| 219 | +} | |
| 220 | + | |
| 221 | + | |
| 220 | 222 | lemoce::ServerSocket::ServerSocket(int lPort): |
| 221 | 223 | lemoce::Socket{}, localPort(lPort) { } |
| 222 | 224 | ... | ... |
src/lib/socket.h
| ... | ... | @@ -42,15 +42,18 @@ namespace lemoce { |
| 42 | 42 | std::string getLocalHost() const; |
| 43 | 43 | void setLocalPort(int); |
| 44 | 44 | int getLocalPort() const; |
| 45 | + void setBufferSize(int); | |
| 46 | + int getBufferSize() const; | |
| 45 | 47 | |
| 46 | 48 | void connect(); |
| 47 | 49 | std::unique_ptr<std::string> read(); |
| 48 | - std::unique_ptr<std::string> read(const size_t n); | |
| 50 | + int read(char msg[], const size_t n); | |
| 49 | 51 | void write(const std::string& message); |
| 52 | + void write(const char * msg, const size_t n); | |
| 50 | 53 | |
| 51 | 54 | private: |
| 52 | 55 | |
| 53 | - static const int MESSAGE_BUFFER; | |
| 56 | + int bufferSize; | |
| 54 | 57 | void fillLocalHostPort(); |
| 55 | 58 | |
| 56 | 59 | std::string remoteHost; //!< Brief host do servidor | ... | ... |
src/utils/tcptunnel.cpp
| ... | ... | @@ -13,6 +13,9 @@ |
| 13 | 13 | #include "socket.h" |
| 14 | 14 | |
| 15 | 15 | |
| 16 | +const int MESSAGE_BUFFER = 8096; | |
| 17 | + | |
| 18 | + | |
| 16 | 19 | void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std::unique_ptr<std::thread>>> & threads) |
| 17 | 20 | { |
| 18 | 21 | std::cout << "iniciei gerenciador de threads" << std::endl; |
| ... | ... | @@ -30,7 +33,7 @@ void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std:: |
| 30 | 33 | (it->second)->join(); |
| 31 | 34 | (it->second).reset(); |
| 32 | 35 | it = threads.erase(it); |
| 33 | - std::cout << "removi thread inutilizada " << (i+1) << std::endl; | |
| 36 | + // std::cout << "removi thread inutilizada " << (i+1) << std::endl; | |
| 34 | 37 | } |
| 35 | 38 | else |
| 36 | 39 | { |
| ... | ... | @@ -43,11 +46,14 @@ void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std:: |
| 43 | 46 | |
| 44 | 47 | void tunnelingTask(std::unique_ptr<lemoce::ClientSocket> server_ptr, |
| 45 | 48 | std::unique_ptr<lemoce::ClientSocket> client_ptr, |
| 49 | + std::unique_ptr<char []> buffer, | |
| 46 | 50 | std::shared_ptr<std::atomic<bool>> done) |
| 47 | 51 | { |
| 48 | 52 | |
| 49 | 53 | lemoce::ClientSocket server = *server_ptr; |
| 50 | 54 | lemoce::ClientSocket client = *client_ptr; |
| 55 | + char * charbuf; | |
| 56 | + charbuf = static_cast<char *>(buffer.get()); | |
| 51 | 57 | |
| 52 | 58 | try |
| 53 | 59 | { |
| ... | ... | @@ -55,31 +61,27 @@ void tunnelingTask(std::unique_ptr<lemoce::ClientSocket> server_ptr, |
| 55 | 61 | while(!server.isClosed()) |
| 56 | 62 | { |
| 57 | 63 | |
| 58 | - std::unique_ptr<std::string> incomingMessage = client.read(); | |
| 59 | - if ((*incomingMessage).size() == 0) | |
| 64 | + int len = client.read(charbuf, MESSAGE_BUFFER); | |
| 65 | + if (len == 0) | |
| 60 | 66 | { |
| 61 | 67 | server.close(); |
| 62 | 68 | client.close(); |
| 63 | 69 | break; |
| 64 | 70 | } |
| 65 | - std::cout << " ==> " << (*incomingMessage) << std::endl; | |
| 66 | - | |
| 67 | - std::string * tmpMessage = incomingMessage.get(); | |
| 68 | - | |
| 69 | - server.write((*tmpMessage)); | |
| 70 | - std::cout << (*tmpMessage) << " ==>" << std::endl; | |
| 71 | - std::unique_ptr<std::string> responseMessage = server.read(); | |
| 72 | - if ((*responseMessage).size() == 0) | |
| 71 | + //std::cout << " ==> " << buffer << std::endl; | |
| 72 | + | |
| 73 | + server.write(charbuf, len); | |
| 74 | + // std::cout << (*tmpMessage) << " ==>" << std::endl; | |
| 75 | + len = server.read(charbuf, MESSAGE_BUFFER); | |
| 76 | + if (len == 0) | |
| 73 | 77 | { |
| 74 | 78 | server.close(); |
| 75 | 79 | client.close(); |
| 76 | 80 | break; |
| 77 | 81 | } |
| 78 | - std::cout << (*responseMessage) << " <==" << std::endl; | |
| 82 | + //std::cout << buffer << " <==" << std::endl; | |
| 79 | 83 | |
| 80 | - tmpMessage = responseMessage.get(); | |
| 81 | - client.write((*tmpMessage)); | |
| 82 | - std::cout << " <== " << (*tmpMessage) << std::endl; | |
| 84 | + client.write(charbuf, len); | |
| 83 | 85 | } |
| 84 | 86 | } |
| 85 | 87 | catch (lemoce::SocketException & ex) |
| ... | ... | @@ -128,6 +130,8 @@ int main(int argc, char *argv[]) |
| 128 | 130 | std::unique_ptr<lemoce::ClientSocket> client_ptr{new lemoce::ClientSocket{}}; |
| 129 | 131 | std::unique_ptr<lemoce::ClientSocket> remoteClient_ptr; |
| 130 | 132 | |
| 133 | + std::unique_ptr<char []> buffer{new char[MESSAGE_BUFFER]}; | |
| 134 | + | |
| 131 | 135 | try |
| 132 | 136 | { |
| 133 | 137 | server.accept((*client_ptr)); |
| ... | ... | @@ -154,6 +158,7 @@ int main(int argc, char *argv[]) |
| 154 | 158 | { |
| 155 | 159 | tunnelingTask, std::move(remoteClient_ptr), |
| 156 | 160 | std::move(client_ptr), |
| 161 | + std::move(buffer), | |
| 157 | 162 | done |
| 158 | 163 | }}; |
| 159 | 164 | ... | ... |