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.
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
;
41 oss
<< std::setbase(10) << errcode
<< ", ";
44 // oss << strerror(-libusb_errno) << "): "
51 Error::Error(const std::string
&str
)
52 : Barry::Error(GetErrorString(0, str
))
58 Error::Error(int errcode
, const std::string
&str
)
59 : Barry::Error(GetErrorString(errcode
, str
))
66 ///////////////////////////////////////////////////////////////////////////////
67 // JDWP::JDWP communication class
79 bool JDWP::Read(int socket
, Barry::Data
&data
, int timeout
)
86 ret
= read(socket
, (char*) data
.GetBuffer(), data
.GetBufSize());
90 if (ret
!= -EINTR
&& ret
!= -EAGAIN
) {
92 if( ret
== -ETIMEDOUT
)
93 throw Timeout(ret
, "Timeout in read");
95 throw Error(ret
, "Error in read");
99 data
.ReleaseBuffer(ret
);
100 } while( ret
== -EINTR
);
106 bool JDWP::Write(int socket
, const Barry::Data
&data
, int timeout
)
111 ret
= write(socket
, (char*) data
.GetData(), data
.GetSize());
113 if( ret
< 0 && ret
!= -EINTR
&& ret
!= -EAGAIN
) {
115 if( ret
== -ETIMEDOUT
)
116 throw Timeout(ret
, "Timeout in write (1)");
118 throw Error(ret
, "Error in write (1)");
120 } while( ret
== -EINTR
|| ret
== -EAGAIN
);
126 bool JDWP::Write(int socket
, const void *data
, size_t size
, int timeout
)
131 ret
= write(socket
, (char*) data
, size
);
133 if( ret
< 0 && ret
!= -EINTR
&& ret
!= -EAGAIN
) {
135 if( ret
== -ETIMEDOUT
)
136 throw Timeout(ret
, "Timeout in write (2)");
138 throw Error(ret
, "Error in write (2)");
140 } while( ret
== -EINTR
|| ret
== -EAGAIN
);