j_jdwp.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       j_jdwp.cc
00003 ///             JDWP socket communication implementation
00004 ///
00005 
00006 /*
00007     Copyright (C) 2009, Nicolas VIVIEN
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "j_jdwp.h"
00023 #include "data.h"
00024 #include <sstream>
00025 #include <iomanip>
00026 #include <errno.h>
00027 #include <unistd.h>
00028 
00029 namespace Barry { namespace JDWP {
00030 
00031 
00032 ///////////////////////////////////////////////////////////////////////////////
00033 // JDWP::Error exception class
00034 
00035 static std::string GetErrorString(int errcode, const std::string &str)
00036 {
00037         std::ostringstream oss;
00038         oss << "(";
00039 
00040         if( errcode ) {
00041                 oss << std::setbase(10) << errcode << ", ";
00042         }
00043 
00044 //      oss << strerror(-libusb_errno) << "): "
00045         oss << "): ";
00046         oss << str;
00047         return oss.str();
00048 }
00049 
00050 
00051 Error::Error(const std::string &str)
00052         : Barry::Error(GetErrorString(0, str))
00053         , m_errcode(0)
00054 {
00055 }
00056 
00057 
00058 Error::Error(int errcode, const std::string &str)
00059         : Barry::Error(GetErrorString(errcode, str))
00060         , m_errcode(errcode)
00061 {
00062 }
00063 
00064 
00065 
00066 ///////////////////////////////////////////////////////////////////////////////
00067 // JDWP::JDWP communication class
00068 
00069 JDWP::JDWP()
00070 {
00071 }
00072 
00073 
00074 JDWP::~JDWP()
00075 {
00076 }
00077 
00078 
00079 bool JDWP::Read(int socket, Barry::Data &data, int timeout)
00080 {
00081         int ret;
00082 
00083         do {
00084                 data.QuickZap();
00085 
00086                 ret = read(socket, (char*) data.GetBuffer(), data.GetBufSize());
00087 
00088                 if( ret < 0 ) {
00089                         ret = -errno;
00090                         if (ret != -EINTR && ret != -EAGAIN ) {
00091                                 m_lasterror = ret;
00092                                 if( ret == -ETIMEDOUT )
00093                                         throw Timeout(ret, "Timeout in read");
00094                                 else
00095                                         throw Error(ret, "Error in read");
00096                         }
00097                 }
00098                 else
00099                         data.ReleaseBuffer(ret);
00100         } while( ret == -EINTR );
00101 
00102         return ret >= 0;
00103 }
00104 
00105 
00106 bool JDWP::Write(int socket, const Barry::Data &data, int timeout)
00107 {
00108         int ret;
00109 
00110         do {
00111                 ret = write(socket, (char*) data.GetData(), data.GetSize());
00112 
00113                 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
00114                         m_lasterror = ret;
00115                         if( ret == -ETIMEDOUT )
00116                                 throw Timeout(ret, "Timeout in write (1)");
00117                         else
00118                                 throw Error(ret, "Error in write (1)");
00119                 }
00120         } while( ret == -EINTR || ret == -EAGAIN );
00121 
00122         return ret >= 0;
00123 }
00124 
00125 
00126 bool JDWP::Write(int socket, const void *data, size_t size, int timeout)
00127 {
00128         int ret;
00129 
00130         do {
00131                 ret = write(socket, (char*) data, size);
00132 
00133                 if( ret < 0 && ret != -EINTR && ret != -EAGAIN ) {
00134                         m_lasterror = ret;
00135                         if( ret == -ETIMEDOUT )
00136                                 throw Timeout(ret, "Timeout in write (2)");
00137                         else
00138                                 throw Error(ret, "Error in write (2)");
00139                 }
00140         } while( ret == -EINTR || ret == -EAGAIN );
00141 
00142         return ret >= 0;
00143 }
00144 
00145 }} // namespace JDWP
00146