55 #if ETISS_USE_POSIX_SOCKET
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
63 #include <sys/socket.h>
64 #include <sys/types.h>
69 #define SOCK_NONBLOCK O_NONBLOCK
74 UnixTCPGDBConnection::UnixTCPGDBConnection(
unsigned port)
78 socket_ = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
79 active_valid_ =
false;
84 std::cout <<
"ERROR: failed to create TCP socket (ipv4)" << std::endl;
85 std::cout <<
"\t" << strerror(errno) << std::endl;
89 setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, (
char *)&
flag,
sizeof(
int));
91 setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, (
char *)&
flag,
96 struct sockaddr_in addr;
97 addr.sin_family = AF_INET;
98 addr.sin_addr.s_addr = INADDR_ANY;
99 addr.sin_port = htons(port);
100 if (bind(socket_, (
struct sockaddr *)&addr,
sizeof(addr)) < 0)
103 std::cout <<
"ERROR: failed to bind TCP socket (ipv4) to port " << port << std::endl;
104 std::cout <<
"\t" << strerror(errno) << std::endl;
113 UnixTCPGDBConnection::~UnixTCPGDBConnection()
120 bool UnixTCPGDBConnection::available()
122 return _available(
false);
124 bool UnixTCPGDBConnection::_available(
bool block)
128 if (!valid_ && !active_valid_)
131 if (!active_valid_ && valid_)
133 int cur = accept(socket_, 0, 0);
137 active_valid_ =
true;
140 setsockopt(cur, IPPROTO_TCP, TCP_NODELAY, (
char *)&
flag,
sizeof(
int));
147 int deny = accept(socket_, 0, 0);
153 ssize_t len = recv(active_, (
void *)(buffer_ + buffer_pos_), 1024 - buffer_pos_, block ? 0 : MSG_DONTWAIT);
156 for (
unsigned i = buffer_pos_; i < (unsigned)(buffer_pos_ + len); i++)
158 if (buffer_[i] == 243 || buffer_[i] == 3)
160 for (
int j = i + 1; j < buffer_pos_ + len; j++)
162 buffer_[j - 1] = buffer_[j];
165 pending_break_ =
true;
174 active_valid_ =
false;
176 else if (errno != EAGAIN && errno != EWOULDBLOCK)
178 std::cout <<
"ERROR: gdb socket failed" << std::endl;
179 std::cout <<
"\t" << strerror(errno) << std::endl;
181 active_valid_ =
false;
184 return buffer_pos_ > 0;
186 std::string UnixTCPGDBConnection::rcv()
188 if (buffer_pos_ == 0)
193 for (
unsigned i = 0; i < buffer_pos_; i++)
195 ret.push_back((
char)buffer_[i]);
203 bool UnixTCPGDBConnection::snd(std::string answer)
209 while (pos < answer.length())
211 ssize_t len = write(active_, answer.c_str() + pos, answer.length() - pos);
214 if (len != EAGAIN && len != EWOULDBLOCK)
217 active_valid_ =
false;