Fixed C++ errors and warnings reported by cppcheck
[barry.git] / src / j_jdwp.cc
blobd0cf95e202a94073cae57035297ed5eb99677033
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 "i18n.h"
23 #include "j_jdwp.h"
24 #include "data.h"
25 #include <sstream>
26 #include <iomanip>
27 #include <errno.h>
28 #include <unistd.h>
30 namespace Barry { namespace JDWP {
33 ///////////////////////////////////////////////////////////////////////////////
34 // JDWP::Error exception class
36 static std::string GetErrorString(int errcode, const std::string &str)
38 std::ostringstream oss;
39 oss << "(";
41 if( errcode ) {
42 oss << std::setbase(10) << errcode << ", ";
45 // oss << strerror(-libusb_errno) << "): "
46 oss << "): ";
47 oss << str;
48 return oss.str();
52 Error::Error(const std::string &str)
53 : Barry::Error(GetErrorString(0, str))
54 , m_errcode(0)
59 Error::Error(int errcode, const std::string &str)
60 : Barry::Error(GetErrorString(errcode, str))
61 , m_errcode(errcode)
67 ///////////////////////////////////////////////////////////////////////////////
68 // JDWP::JDWP communication class
70 JDWP::JDWP()
71 : m_lasterror(0)
76 JDWP::~JDWP()
81 bool JDWP::Read(int socket, Barry::Data &data, int timeout)
83 int ret;
85 do {
86 data.QuickZap();
88 ret = read(socket, (char*) data.GetBuffer(), data.GetBufSize());
90 if( ret < 0 ) {
91 ret = -errno;
92 if (ret != -EINTR && ret != -EAGAIN ) {
93 m_lasterror = ret;
94 if( ret == -ETIMEDOUT )
95 throw Timeout(ret, _("Timeout in read"));
96 else
97 throw Error(ret, _("Error in read"));
100 else
101 data.ReleaseBuffer(ret);
102 } while( ret == -EINTR );
104 return ret >= 0;
108 bool JDWP::Write(int socket, const Barry::Data &data, int timeout)
110 int ret;
112 do {
113 ret = write(socket, (char*) data.GetData(), data.GetSize());
115 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
116 m_lasterror = ret;
117 if( ret == -ETIMEDOUT )
118 throw Timeout(ret, _("Timeout in write (1)"));
119 else
120 throw Error(ret, _("Error in write (1)"));
122 } while( ret == -EINTR || ret == -EAGAIN );
124 return ret >= 0;
128 bool JDWP::Write(int socket, const void *data, size_t size, int timeout)
130 int ret;
132 do {
133 ret = write(socket, (char*) data, size);
135 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
136 m_lasterror = ret;
137 if( ret == -ETIMEDOUT )
138 throw Timeout(ret, _("Timeout in write (2)"));
139 else
140 throw Error(ret, _("Error in write (2)"));
142 } while( ret == -EINTR || ret == -EAGAIN );
144 return ret >= 0;
147 }} // namespace JDWP