debian: added giffgaff chatscripts
[barry.git] / src / j_jdwp.cc
blob071091705c8be214c6962e52a8d24b31f54a6a3e
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()
75 JDWP::~JDWP()
80 bool JDWP::Read(int socket, Barry::Data &data, int timeout)
82 int ret;
84 do {
85 data.QuickZap();
87 ret = read(socket, (char*) data.GetBuffer(), data.GetBufSize());
89 if( ret < 0 ) {
90 ret = -errno;
91 if (ret != -EINTR && ret != -EAGAIN ) {
92 m_lasterror = ret;
93 if( ret == -ETIMEDOUT )
94 throw Timeout(ret, _("Timeout in read"));
95 else
96 throw Error(ret, _("Error in read"));
99 else
100 data.ReleaseBuffer(ret);
101 } while( ret == -EINTR );
103 return ret >= 0;
107 bool JDWP::Write(int socket, const Barry::Data &data, int timeout)
109 int ret;
111 do {
112 ret = write(socket, (char*) data.GetData(), data.GetSize());
114 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
115 m_lasterror = ret;
116 if( ret == -ETIMEDOUT )
117 throw Timeout(ret, _("Timeout in write (1)"));
118 else
119 throw Error(ret, _("Error in write (1)"));
121 } while( ret == -EINTR || ret == -EAGAIN );
123 return ret >= 0;
127 bool JDWP::Write(int socket, const void *data, size_t size, int timeout)
129 int ret;
131 do {
132 ret = write(socket, (char*) data, size);
134 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
135 m_lasterror = ret;
136 if( ret == -ETIMEDOUT )
137 throw Timeout(ret, _("Timeout in write (2)"));
138 else
139 throw Error(ret, _("Error in write (2)"));
141 } while( ret == -EINTR || ret == -EAGAIN );
143 return ret >= 0;
146 }} // namespace JDWP