Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

communication.cpp File Reference

Things related to network communication. More...

#include "xdprof.h"

Include dependency graph for communication.cpp:

Include dependency graph

Go to the source code of this file.

Functions

int Send (const char *msg, int len)
 Sends the msg of length len over the socket. More...

void Send (const string &s)
 Sends a string. More...

bool IsConnected ()
 Determines if the client is connected to the server. More...

int GetSocketDescriptor (const char *hostname, unsigned short port)
 Return a socket descriptor connect()ed to a hostname that is accept()ing information data on port. More...

bool InitializeCommunications (string hostname, int port)
 This initializes communications with the server. More...

void RecordPacket (unsigned long packetsize)
void ShutdownCommunications ()
 Shutdown the communications, close the sockets, etc. More...


Variables

int Server
 Socket connection (or < 0 if not connected). More...

int BytesSent = 0
 Number of bytes sent. More...

int SendCalls = 0
 Number of times Send was called. More...

int PacketCount = 0
unsigned long int PacketBytes = 0


Detailed Description

Things related to network communication.

Definition in file communication.cpp.


Function Documentation

int GetSocketDescriptor ( const char * hostname,
unsigned short port ) [static]
 

Return a socket descriptor connect()ed to a hostname that is accept()ing information data on port.

Return a value <= 0 if such a connection can't be made.

Parameters:
hostname   the hostname to connect to
port   the port number

Definition at line 91 of file communication.cpp.

Referenced by InitializeCommunications().

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     /* create a socket */
00110     fd = socket(AF_INET, SOCK_STREAM, 0);
00111     
00112     /* find remote host's addr from name */
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     /* set remote host's addr; its already in network byte order */
00123     memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list), 
00124         sizeof(s.sin_addr.s_addr));
00125     /* set remote host's port */
00126     s.sin_port = htons(port);
00127     s.sin_family = AF_INET;
00128     
00129     /* now try connecting */
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 }

bool InitializeCommunications ( string hostname,
int port )
 

This initializes communications with the server.

Parameters:
hostname   the hostname to connect to; either IP address or a machine name
port   the port number to use
selfinfo   a descriptive string describing this connection

Definition at line 148 of file communication.cpp.

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 }

bool IsConnected ( )
 

Determines if the client is connected to the server.

Definition at line 79 of file communication.cpp.

Referenced by BackgroundThread(), and InitializeCommunications().

00080 {
00081     return (Server > 0);
00082 }

void RecordPacket ( unsigned long packetsize )
 

Definition at line 176 of file communication.cpp.

00177 {
00178     ++PacketCount;
00179     PacketBytes += packetsize;
00180 }

void Send ( const string & s )
 

Sends a string.

Parameters:
s   the string to send

Definition at line 71 of file communication.cpp.

00072 {
00073     Send(s.c_str(), s.length() );
00074 }

int Send ( const char * msg,
int len )
 

Sends the msg of length len over the socket.

Parameters:
msg   the data to send
len   the length of the data

Definition at line 55 of file communication.cpp.

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 }

void ShutdownCommunications ( )
 

Shutdown the communications, close the sockets, etc.

Definition at line 185 of file communication.cpp.

Referenced by BackgroundThread(), and NotifyEvent().

00186 {
00187     Send("/*done*/");
00188     unsafe_debug("Bytes sent:", BytesSent);
00189     unsafe_debug("SendCalls: ", SendCalls);
00190     //fprintf(stderr, "bytes=%d calls=%d\n", BytesSent, SendCalls);
00191     fprintf(stdout, "\npackets\tbytes\trefresh\n");
00192     //fprintf(stderr, "packets\t%d \nbytes\t%d\nrefresh\t%d\n", PacketCount, PacketBytes,RefreshTime);
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 }


Variable Documentation

int BytesSent = 0 [static]
 

Number of bytes sent.

Definition at line 45 of file communication.cpp.

unsigned long int PacketBytes = 0 [static]
 

Definition at line 174 of file communication.cpp.

int PacketCount = 0 [static]
 

Definition at line 173 of file communication.cpp.

int SendCalls = 0 [static]
 

Number of times Send was called.

Definition at line 48 of file communication.cpp.

int Server [static]
 

Socket connection (or < 0 if not connected).

Definition at line 42 of file communication.cpp.


Generated at Sun Jun 24 20:57:31 2001 for xdprof by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001