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 @@ | @@ -0,0 +1 @@ | ||
1 | +./build*/ |
src/lib/socket.cpp
@@ -11,7 +11,6 @@ | @@ -11,7 +11,6 @@ | ||
11 | #include "socket.h" | 11 | #include "socket.h" |
12 | 12 | ||
13 | // TODO Remover Message Buffer | 13 | // TODO Remover Message Buffer |
14 | -const int lemoce::ClientSocket::MESSAGE_BUFFER = 8192; | ||
15 | int lemoce::ServerSocket::BACKLOG = 512; | 14 | int lemoce::ServerSocket::BACKLOG = 512; |
16 | 15 | ||
17 | lemoce::Socket::Socket() | 16 | lemoce::Socket::Socket() |
@@ -50,12 +49,17 @@ void lemoce::Socket::close() | @@ -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 | void lemoce::ClientSocket::connect() | 64 | void lemoce::ClientSocket::connect() |
61 | { | 65 | { |
@@ -88,30 +92,14 @@ void lemoce::ClientSocket::connect() | @@ -88,30 +92,14 @@ void lemoce::ClientSocket::connect() | ||
88 | localPort = lPort; | 92 | localPort = lPort; |
89 | } | 93 | } |
90 | 94 | ||
91 | -// TODO deletar essa ou de baixo | ||
92 | std::unique_ptr<std::string> lemoce::ClientSocket::read() | 95 | std::unique_ptr<std::string> lemoce::ClientSocket::read() |
93 | { | 96 | { |
94 | - char chararray[MESSAGE_BUFFER + 1]; | 97 | + char chararray[bufferSize]; |
95 | std::ostringstream buffer; | 98 | std::ostringstream buffer; |
96 | int nread; | 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 | buffer << chararray; | 103 | buffer << chararray; |
116 | 104 | ||
117 | std::unique_ptr<std::string> result{new std::string{buffer.str()}}; | 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,49 +107,52 @@ std::unique_ptr<std::string> lemoce::ClientSocket::read() | ||
119 | return result; | 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 | ssize_t nread; | 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 | if ((nread = ::recv(getFd(), ptr, n, 0)) < 0) | 121 | if ((nread = ::recv(getFd(), ptr, n, 0)) < 0) |
136 | { | 122 | { |
137 | if (errno == EINTR) | 123 | if (errno == EINTR) |
138 | - nread = 0; | 124 | + { |
125 | + nread = 0; | ||
126 | + continue; | ||
127 | + } | ||
139 | else | 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 | ptr = ptr + nread; | 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 | // TODO Modificar para array de bytes | 145 | // TODO Modificar para array de bytes |
158 | void lemoce::ClientSocket::write(const std::string& message) | 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 | ssize_t nwritten = nleft; | 154 | ssize_t nwritten = nleft; |
155 | + const char * ptr = msg; | ||
165 | 156 | ||
166 | while (nleft > 0) | 157 | while (nleft > 0) |
167 | { | 158 | { |
@@ -170,11 +161,11 @@ void lemoce::ClientSocket::write(const std::string& message) | @@ -170,11 +161,11 @@ void lemoce::ClientSocket::write(const std::string& message) | ||
170 | if (nwritten < 0 && errno == EINTR) | 161 | if (nwritten < 0 && errno == EINTR) |
171 | nwritten = 0; | 162 | nwritten = 0; |
172 | else | 163 | else |
173 | - throw new SocketException{errno}; | 164 | + treatSocketError(false); |
174 | } | 165 | } |
175 | nleft -= nwritten; | 166 | nleft -= nwritten; |
176 | ptr += nwritten; | 167 | ptr += nwritten; |
177 | - } | 168 | + } |
178 | } | 169 | } |
179 | 170 | ||
180 | void lemoce::ClientSocket::setRemoteHost(std::string rHost) | 171 | void lemoce::ClientSocket::setRemoteHost(std::string rHost) |
@@ -217,6 +208,17 @@ int lemoce::ClientSocket::getLocalPort() const | @@ -217,6 +208,17 @@ int lemoce::ClientSocket::getLocalPort() const | ||
217 | return localPort; | 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 | lemoce::ServerSocket::ServerSocket(int lPort): | 222 | lemoce::ServerSocket::ServerSocket(int lPort): |
221 | lemoce::Socket{}, localPort(lPort) { } | 223 | lemoce::Socket{}, localPort(lPort) { } |
222 | 224 |
src/lib/socket.h
@@ -42,15 +42,18 @@ namespace lemoce { | @@ -42,15 +42,18 @@ namespace lemoce { | ||
42 | std::string getLocalHost() const; | 42 | std::string getLocalHost() const; |
43 | void setLocalPort(int); | 43 | void setLocalPort(int); |
44 | int getLocalPort() const; | 44 | int getLocalPort() const; |
45 | + void setBufferSize(int); | ||
46 | + int getBufferSize() const; | ||
45 | 47 | ||
46 | void connect(); | 48 | void connect(); |
47 | std::unique_ptr<std::string> read(); | 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 | void write(const std::string& message); | 51 | void write(const std::string& message); |
52 | + void write(const char * msg, const size_t n); | ||
50 | 53 | ||
51 | private: | 54 | private: |
52 | 55 | ||
53 | - static const int MESSAGE_BUFFER; | 56 | + int bufferSize; |
54 | void fillLocalHostPort(); | 57 | void fillLocalHostPort(); |
55 | 58 | ||
56 | std::string remoteHost; //!< Brief host do servidor | 59 | std::string remoteHost; //!< Brief host do servidor |
src/utils/tcptunnel.cpp
@@ -13,6 +13,9 @@ | @@ -13,6 +13,9 @@ | ||
13 | #include "socket.h" | 13 | #include "socket.h" |
14 | 14 | ||
15 | 15 | ||
16 | +const int MESSAGE_BUFFER = 8096; | ||
17 | + | ||
18 | + | ||
16 | void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std::unique_ptr<std::thread>>> & threads) | 19 | void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std::unique_ptr<std::thread>>> & threads) |
17 | { | 20 | { |
18 | std::cout << "iniciei gerenciador de threads" << std::endl; | 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,7 +33,7 @@ void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std:: | ||
30 | (it->second)->join(); | 33 | (it->second)->join(); |
31 | (it->second).reset(); | 34 | (it->second).reset(); |
32 | it = threads.erase(it); | 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 | else | 38 | else |
36 | { | 39 | { |
@@ -43,11 +46,14 @@ void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std:: | @@ -43,11 +46,14 @@ void threadManager(std::list<std::pair<std::shared_ptr<std::atomic<bool>>, std:: | ||
43 | 46 | ||
44 | void tunnelingTask(std::unique_ptr<lemoce::ClientSocket> server_ptr, | 47 | void tunnelingTask(std::unique_ptr<lemoce::ClientSocket> server_ptr, |
45 | std::unique_ptr<lemoce::ClientSocket> client_ptr, | 48 | std::unique_ptr<lemoce::ClientSocket> client_ptr, |
49 | + std::unique_ptr<char []> buffer, | ||
46 | std::shared_ptr<std::atomic<bool>> done) | 50 | std::shared_ptr<std::atomic<bool>> done) |
47 | { | 51 | { |
48 | 52 | ||
49 | lemoce::ClientSocket server = *server_ptr; | 53 | lemoce::ClientSocket server = *server_ptr; |
50 | lemoce::ClientSocket client = *client_ptr; | 54 | lemoce::ClientSocket client = *client_ptr; |
55 | + char * charbuf; | ||
56 | + charbuf = static_cast<char *>(buffer.get()); | ||
51 | 57 | ||
52 | try | 58 | try |
53 | { | 59 | { |
@@ -55,31 +61,27 @@ void tunnelingTask(std::unique_ptr<lemoce::ClientSocket> server_ptr, | @@ -55,31 +61,27 @@ void tunnelingTask(std::unique_ptr<lemoce::ClientSocket> server_ptr, | ||
55 | while(!server.isClosed()) | 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 | server.close(); | 67 | server.close(); |
62 | client.close(); | 68 | client.close(); |
63 | break; | 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 | server.close(); | 78 | server.close(); |
75 | client.close(); | 79 | client.close(); |
76 | break; | 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 | catch (lemoce::SocketException & ex) | 87 | catch (lemoce::SocketException & ex) |
@@ -128,6 +130,8 @@ int main(int argc, char *argv[]) | @@ -128,6 +130,8 @@ int main(int argc, char *argv[]) | ||
128 | std::unique_ptr<lemoce::ClientSocket> client_ptr{new lemoce::ClientSocket{}}; | 130 | std::unique_ptr<lemoce::ClientSocket> client_ptr{new lemoce::ClientSocket{}}; |
129 | std::unique_ptr<lemoce::ClientSocket> remoteClient_ptr; | 131 | std::unique_ptr<lemoce::ClientSocket> remoteClient_ptr; |
130 | 132 | ||
133 | + std::unique_ptr<char []> buffer{new char[MESSAGE_BUFFER]}; | ||
134 | + | ||
131 | try | 135 | try |
132 | { | 136 | { |
133 | server.accept((*client_ptr)); | 137 | server.accept((*client_ptr)); |
@@ -154,6 +158,7 @@ int main(int argc, char *argv[]) | @@ -154,6 +158,7 @@ int main(int argc, char *argv[]) | ||
154 | { | 158 | { |
155 | tunnelingTask, std::move(remoteClient_ptr), | 159 | tunnelingTask, std::move(remoteClient_ptr), |
156 | std::move(client_ptr), | 160 | std::move(client_ptr), |
161 | + std::move(buffer), | ||
157 | done | 162 | done |
158 | }}; | 163 | }}; |
159 | 164 |