lib: show offset and rectype in HexDumpParser
[barry.git] / src / j_jdwp.cc
blob875f226378d02604719724d4ad670a4e39cf0d95
1 ///
2 /// \file j_jdwp.cc
3 /// JDWP socket communication implementation
4 ///
6 /*
7 Copyright (C) 2009, Nicolas VIVIEN
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "j_jdwp.h"
23 #include "data.h"
24 #include <sstream>
25 #include <iomanip>
26 #include <errno.h>
29 namespace Barry { namespace JDWP {
32 ///////////////////////////////////////////////////////////////////////////////
33 // JDWP::Error exception class
35 static std::string GetErrorString(int errcode, const std::string &str)
37 std::ostringstream oss;
38 oss << "(";
40 if( errcode ) {
41 oss << std::setbase(10) << errcode << ", ";
44 // oss << strerror(-libusb_errno) << "): "
45 oss << "): ";
46 oss << str;
47 return oss.str();
51 Error::Error(const std::string &str)
52 : Barry::Error(GetErrorString(0, str))
53 , m_errcode(0)
58 Error::Error(int errcode, const std::string &str)
59 : Barry::Error(GetErrorString(errcode, str))
60 , m_errcode(errcode)
66 ///////////////////////////////////////////////////////////////////////////////
67 // JDWP::JDWP communication class
69 JDWP::JDWP()
74 JDWP::~JDWP()
79 bool JDWP::Read(int socket, Barry::Data &data, int timeout)
81 int ret;
83 do {
84 data.QuickZap();
86 ret = read(socket, (char*) data.GetBuffer(), data.GetBufSize());
88 if( ret < 0 ) {
89 ret = -errno;
90 if (ret != -EINTR && ret != -EAGAIN ) {
91 m_lasterror = ret;
92 if( ret == -ETIMEDOUT )
93 throw Timeout(ret, "Timeout in read");
94 else
95 throw Error(ret, "Error in read");
98 else
99 data.ReleaseBuffer(ret);
100 } while( ret == -EINTR );
102 return ret >= 0;
106 bool JDWP::Write(int socket, const Barry::Data &data, int timeout)
108 int ret;
110 do {
111 ret = write(socket, (char*) data.GetData(), data.GetSize());
113 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
114 m_lasterror = ret;
115 if( ret == -ETIMEDOUT )
116 throw Timeout(ret, "Timeout in write (1)");
117 else
118 throw Error(ret, "Error in write (1)");
120 } while( ret == -EINTR || ret == -EAGAIN );
122 return ret >= 0;
126 bool JDWP::Write(int socket, const void *data, size_t size, int timeout)
128 int ret;
130 do {
131 ret = write(socket, (char*) data, size);
133 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
134 m_lasterror = ret;
135 if( ret == -ETIMEDOUT )
136 throw Timeout(ret, "Timeout in write (2)");
137 else
138 throw Error(ret, "Error in write (2)");
140 } while( ret == -EINTR || ret == -EAGAIN );
142 return ret >= 0;
145 }} // namespace JDWP