Add bjdwp tool to Barry project.
[barry.git] / bjdwp / src / jdwp.cc
blobc6d983e69cd0ebe980492bc2a6ba52f3047f111f
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <pthread.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <netdb.h>
13 #include <barry/barry.h>
15 #include "jdwp.h"
18 namespace JDWP {
21 ///////////////////////////////////////////////////////////////////////////////
22 // JDWP::Error exception class
24 static std::string GetErrorString(int errcode, const std::string &str)
26 std::ostringstream oss;
27 oss << "(";
29 if( errcode ) {
30 oss << std::setbase(10) << errcode << ", ";
33 // oss << strerror(-libusb_errno) << "): "
34 oss << "): ";
35 oss << str;
36 return oss.str();
40 Error::Error(const std::string &str)
41 : Barry::Error(GetErrorString(0, str))
42 , m_errcode(0)
47 Error::Error(int errcode, const std::string &str)
48 : Barry::Error(GetErrorString(errcode, str))
49 , m_errcode(errcode)
55 ///////////////////////////////////////////////////////////////////////////////
56 // JDWP::JDWP communication class
58 JDWP::JDWP()
63 JDWP::~JDWP()
68 bool JDWP::Read(int socket, Barry::Data &data, int timeout)
70 int ret;
72 do {
73 data.QuickZap();
75 ret = read(socket, (char*) data.GetBuffer(), data.GetBufSize());
77 if( ret < 0 ) {
78 ret = -errno;
79 if (ret != -EINTR && ret != -EAGAIN ) {
80 m_lasterror = ret;
81 if( ret == -ETIMEDOUT )
82 throw Timeout(ret, "Timeout in read");
83 else
84 throw Error(ret, "Error in read");
87 else
88 data.ReleaseBuffer(ret);
89 } while( ret == -EINTR );
91 return ret >= 0;
95 bool JDWP::Write(int socket, const Barry::Data &data, int timeout)
97 int ret;
99 do {
100 ret = write(socket, (char*) data.GetData(), data.GetSize());
102 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
103 m_lasterror = ret;
104 if( ret == -ETIMEDOUT )
105 throw Timeout(ret, "Timeout in write (1)");
106 else
107 throw Error(ret, "Error in write (1)");
109 } while( ret == -EINTR || ret == -EAGAIN );
111 return ret >= 0;
115 bool JDWP::Write(int socket, const void *data, size_t size, int timeout)
117 int ret;
119 do {
120 ret = write(socket, (char*) data, size);
122 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
123 m_lasterror = ret;
124 if( ret == -ETIMEDOUT )
125 throw Timeout(ret, "Timeout in write (2)");
126 else
127 throw Error(ret, "Error in write (2)");
129 } while( ret == -EINTR || ret == -EAGAIN );
131 return ret >= 0;
134 } // namespace JDWP