00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "xdprof.h"
00033
00037
00038
00042 static int Server;
00043
00045 static int BytesSent = 0;
00046
00048 static int SendCalls = 0;
00049
00055 int Send(const char* msg, int len)
00056 {
00057 if (Server > 0)
00058 {
00059 BytesSent += len;
00060 ++SendCalls;
00061 return send(Server, msg, len, 0);
00062 }
00063 else
00064 return 0;
00065 }
00066
00071 void Send(const string & s)
00072 {
00073 Send(s.c_str(), s.length() );
00074 }
00075
00079 bool IsConnected()
00080 {
00081 return (Server > 0);
00082 }
00083
00091 static int GetSocketDescriptor(const char *hostname, unsigned short port)
00092 {
00093 struct hostent *hentry;
00094 struct sockaddr_in s;
00095 int fd;
00096
00097 if (port <= 0 || port > 65535)
00098 {
00099 fprintf(stderr, "DPROF ERROR: bad port number\n");
00100 return -1;
00101 }
00102
00103 if (hostname == NULL)
00104 {
00105 fprintf(stderr, "DPROF ERROR: hostname is NULL\n");
00106 return -1;
00107 }
00108
00109
00110 fd = socket(AF_INET, SOCK_STREAM, 0);
00111
00112
00113 if ((hentry = gethostbyname(hostname)) == NULL)
00114 {
00115 #ifdef WIN32
00116 debug("Bad gethostbyname value.", WSAGetLastError());
00117 #endif
00118 return -1;
00119 }
00120
00121 memset((char *)&s, 0, sizeof(s));
00122
00123 memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
00124 sizeof(s.sin_addr.s_addr));
00125
00126 s.sin_port = htons(port);
00127 s.sin_family = AF_INET;
00128
00129
00130 if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s)))
00131 {
00132 debug("Unable to connect!");
00133 return -1;
00134 }
00135 else
00136 {
00137 return fd;
00138 }
00139
00140 }
00141
00148 bool InitializeCommunications(string hostname, int port)
00149 {
00150 #ifdef WIN32
00151 WORD wVersionRequested;
00152 WSADATA wsaData;
00153 int err;
00154
00155 wVersionRequested = MAKEWORD( 1, 1 );
00156
00157 err = WSAStartup( wVersionRequested, &wsaData );
00158 if ( err != 0 ) {
00159 debug("WSA error", WSAGetLastError());
00160 Server = -1;
00161 return false;
00162 }
00163 #endif
00164
00165 Server = GetSocketDescriptor(hostname.c_str(), static_cast<unsigned short>(port));
00166 if (IsConnected())
00167 debug("connected!");
00168 else
00169 debug("couldn't connect");
00170 return IsConnected();
00171 }
00172
00173 static int PacketCount = 0;
00174 static unsigned long int PacketBytes = 0;
00175
00176 void RecordPacket(unsigned long packetsize)
00177 {
00178 ++PacketCount;
00179 PacketBytes += packetsize;
00180 }
00181
00185 void ShutdownCommunications()
00186 {
00187 Send("/*done*/");
00188 unsafe_debug("Bytes sent:", BytesSent);
00189 unsafe_debug("SendCalls: ", SendCalls);
00190
00191 fprintf(stdout, "\npackets\tbytes\trefresh\n");
00192
00193 fprintf(stdout, "%d\t%d\t%d\n", PacketCount, PacketBytes,RefreshTime);
00194
00195 fprintf(stdout, "Methods=%d, Classes=%d, MethodCache=%d,ClassCache=%d\n", Methods.size(), Classes.size(), MethodCache.size(), ClassCache.size());
00196
00197 fflush(stdout);
00198 unsafe_debug("Average: ", BytesSent / static_cast<double>(SendCalls));
00199 if (Server > 0)
00200 closesocket(Server);
00201 Server = -1;
00202 #ifdef WIN32
00203 WSACleanup();
00204 #endif
00205 }