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
80 bool JDWP::Read(int socket
, Barry::Data
&data
, int timeout
)
87 ret
= read(socket
, (char*) data
.GetBuffer(), data
.GetBufSize());
91 if (ret
!= -EINTR
&& ret
!= -EAGAIN
) {
93 if( ret
== -ETIMEDOUT
)
94 throw Timeout(ret
, _("Timeout in read"));
96 throw Error(ret
, _("Error in read"));
100 data
.ReleaseBuffer(ret
);
101 } while( ret
== -EINTR
);
107 bool JDWP::Write(int socket
, const Barry::Data
&data
, int timeout
)
112 ret
= write(socket
, (char*) data
.GetData(), data
.GetSize());
114 if( ret
< 0 && ret
!= -EINTR
&& ret
!= -EAGAIN
) {
116 if( ret
== -ETIMEDOUT
)
117 throw Timeout(ret
, _("Timeout in write (1)"));
119 throw Error(ret
, _("Error in write (1)"));
121 } while( ret
== -EINTR
|| ret
== -EAGAIN
);
127 bool JDWP::Write(int socket
, const void *data
, size_t size
, int timeout
)
132 ret
= write(socket
, (char*) data
, size
);
134 if( ret
< 0 && ret
!= -EINTR
&& ret
!= -EAGAIN
) {
136 if( ret
== -ETIMEDOUT
)
137 throw Timeout(ret
, _("Timeout in write (2)"));
139 throw Error(ret
, _("Error in write (2)"));
141 } while( ret
== -EINTR
|| ret
== -EAGAIN
);