ij-16.png   inspectorJ -- JavaTM Profiler
sf project site browse source checkout source
SourceForge.net Logo



src/inspectorj/agent/agentsocket.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   inspectorJ - java profiler                                            *
00003  *   Copyright (C) 2007 by James May
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00027 #include "inspectorj/agent/agentsocket.h"
00028         
00029 using inspectorj::jdwp::JDWPReply;
00030 using inspectorj::jdwp::JDWP_HEADER_LENGTH;
00031         
00032 namespace inspectorj {
00033 namespace agent {
00034         
00040 AgentSocket::AgentSocket(tcp::socket* socket, boost::system::error_code* error)
00041     : socket(socket), error(error)
00042 {       
00043 }
00044 
00051 bool AgentSocket::read(jint &i)
00052 {    
00053     size_t bytesRead = 0;
00054     char *p = (char *)(&i);
00055     char b[sizeof(jint)];
00056     bytesRead = boost::asio::read(*socket, 
00057                                     boost::asio::buffer(b,sizeof(jint)),
00058                                     boost::asio::transfer_all(), 
00059                                     *error);
00060     int sz = (int) sizeof(jint);
00061     sz--;
00062 
00063     while (sz > 0) {
00064         *p++ = b[sz];
00065         sz--;
00066     }
00067     *p   = b[0];
00068         
00069     return (bytesRead == sizeof(jint) && !(*error));       
00070 }
00071 
00078 bool AgentSocket::read(jbyte &j)
00079 {
00080     size_t bytesRead = 0;
00081     char *p = (char *)(&j);
00082     char b[1];
00083     bytesRead = boost::asio::read(*socket, 
00084                                     boost::asio::buffer(b,1),
00085                                     boost::asio::transfer_all(), 
00086                                     *error);
00087     *p = b[0];    
00088     
00089     return (bytesRead == 1 && !(*error));
00090 }    
00091 
00099 bool AgentSocket::read(char *ch, int len)
00100 {
00101     size_t bytesRead = 0;
00102     bytesRead = boost::asio::read(*socket, 
00103                                     boost::asio::buffer(ch, len),
00104                                     boost::asio::transfer_all(), *error); 
00105     
00106     return (bytesRead == len && !(*error));
00107 }
00108 
00115 bool AgentSocket::write(jint i)
00116 {    
00117     size_t bytesWritten = 0;
00118     char *p = (char *)(&i);    
00119     char b[sizeof(jint)];
00120 
00121     int sz = (int) sizeof(jint);
00122     sz--;
00123 
00124     while (sz > 0) {
00125         b[sz] = *p++;
00126         sz--;
00127     }
00128     b[0] = *p;
00129 
00130     bytesWritten = boost::asio::write(*socket, 
00131                                     boost::asio::buffer(b, sizeof(jint)),
00132                                     boost::asio::transfer_all(), 
00133                                     *error);
00134        
00135     return (bytesWritten == sizeof(jint) && !(*error));       
00136 }
00137 
00144 bool AgentSocket::write(short s)
00145 {    
00146     size_t bytesWritten = 0;
00147     char *p = (char *)(&s);    
00148     char b[2];
00149     b[1] = *p++;
00150     b[0] = *p;    
00151     bytesWritten = boost::asio::write(*socket, 
00152                                     boost::asio::buffer(b,2),
00153                                     boost::asio::transfer_all(), 
00154                                     *error);
00155        
00156     return (bytesWritten == 2 && !(*error));       
00157 }
00158 
00165 bool AgentSocket::write(jbyte j)
00166 {
00167     size_t bytesWritten = 0;
00168     char *p = (char *)(&j);
00169     char b[1];
00170     b[0] = *p;
00171     bytesWritten = boost::asio::write(*socket, 
00172                                     boost::asio::buffer(b,1),
00173                                     boost::asio::transfer_all(), 
00174                                     *error);
00175     
00176     return (bytesWritten == 1 && !(*error));
00177 }    
00178 
00179 
00180 bool AgentSocket::writeReply(JDWPReply *reply)
00181 {
00182     if (reply) {
00183         // send the reply           
00184         size_t bytesWritten = 0; 
00185         write(reply->getLength());
00186         write(reply->getId());
00187         write(reply->getPacketType());
00188         write(reply->getErrorCode());
00189     
00190         bytesWritten = boost::asio::write(*socket, 
00191                                         boost::asio::buffer(reply->getDataBytes(),
00192                                                             reply->getLength() - JDWP_HEADER_LENGTH),
00193                                         boost::asio::transfer_all(), 
00194                                         *error);    
00195         delete reply;
00196         reply = 0;
00197         return (*error); 
00198     }
00199     
00200     return false;
00201    
00202 }
00203   
00204 } // end namespace agent
00205 } // end namespace inspectorj

Generated on Sun Aug 19 17:07:52 2007 for inspectorJ by  doxygen 1.5.1