3 /// JDWP socket communication implementation
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.
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
;
42 oss
<< std::setbase(10) << errcode
<< ", ";
45 // oss << strerror(-libusb_errno) << "): "
52 Error::Error(const std::string
&str
)
53 : Barry::Error(GetErrorString(0, str
))
59 Error::Error(int errcode
, const std::string
&str
)
60 : Barry::Error(GetErrorString(errcode
, str
))
67 ///////////////////////////////////////////////////////////////////////////////
68 // JDWP::JDWP communication class
81 bool JDWP::Read(int socket
, Barry::Data
&data
, int timeout
)
88 ret
= read(socket
, (char*) data
.GetBuffer(), data
.GetBufSize());
92 if (ret
!= -EINTR
&& ret
!= -EAGAIN
) {
94 if( ret
== -ETIMEDOUT
)
95 throw Timeout(ret
, _("Timeout in read"));
97 throw Error(ret
, _("Error in read"));
101 data
.ReleaseBuffer(ret
);
102 } while( ret
== -EINTR
);
108 bool JDWP::Write(int socket
, const Barry::Data
&data
, int timeout
)
113 ret
= write(socket
, (char*) data
.GetData(), data
.GetSize());
115 if( ret
< 0 && ret
!= -EINTR
&& ret
!= -EAGAIN
) {
117 if( ret
== -ETIMEDOUT
)
118 throw Timeout(ret
, _("Timeout in write (1)"));
120 throw Error(ret
, _("Error in write (1)"));
122 } while( ret
== -EINTR
|| ret
== -EAGAIN
);
128 bool JDWP::Write(int socket
, const void *data
, size_t size
, int timeout
)
133 ret
= write(socket
, (char*) data
, size
);
135 if( ret
< 0 && ret
!= -EINTR
&& ret
!= -EAGAIN
) {
137 if( ret
== -ETIMEDOUT
)
138 throw Timeout(ret
, _("Timeout in write (2)"));
140 throw Error(ret
, _("Error in write (2)"));
142 } while( ret
== -EINTR
|| ret
== -EAGAIN
);