www.pudn.com > server_client.rar > Logging_Client.h, change:2007-10-18,size:2513b


/* 
** Logging_Client.cpp,v 1.11 2003/11/10 01:48:03 dhinton Exp 
** 
** Copyright 2001 Addison Wesley. All Rights Reserved. 
*/ 
 
#include "ace/OS_NS_sys_time.h" 
#include "ace/CDR_Stream.h" 
#include "ace/INET_Addr.h" 
//#include "ace/SOCK_Connector.h" 
//#include "ace/SOCK_Stream.h" 
 
// FUZZ: disable check_for_streams_include 
//#include "ace/streams.h" 
 
#if defined (ACE_WIN32) && (!defined (ACE_HAS_STANDARD_CPP_LIBRARY) || \ 
                            (ACE_HAS_STANDARD_CPP_LIBRARY == 0)) 
#  include <stdio.h> 
#else 
#  include <string> 
#endif 
 
#define MAX_NAME_LENGTH 255 
 
class Logging_Client { 
private: 
  ACE_SOCK_Stream logging_peer_; 
 
public: 
  ACE_SOCK_Stream &peer () { return logging_peer_; } 
 
  int send_msg (const char* msg_record,const int msg_length) { 
 
    ACE_OutputCDR payload (ACE_CDR::MAX_ALIGNMENT + msg_length); 
    payload.write_char_array(msg_record,msg_length); 
    ACE_CDR::ULong length = payload.total_length (); 
 
    ACE_OutputCDR header (ACE_CDR::MAX_ALIGNMENT + 8); 
    header << ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER); 
 
    // Store the size of the payload that follows 
    header << ACE_CDR::ULong (msg_length); 
    // Use an iovec to send both buffer and payload simultaneously. 
    iovec iov[2]; 
    iov[0].iov_base = header.begin ()->rd_ptr (); 
    iov[0].iov_len  = 8; 
    iov[1].iov_base = payload.begin ()->rd_ptr (); 
    iov[1].iov_len  = length; 
 
    // Send header and payload efficiently using "gather-write". 
    return logging_peer_.sendv_n (iov, 2); 
  } 
 
  int send_file (const char* filename,const char* msg_record,const int msg_length)  
  { 
	//ÄÚÈÝ 
    ACE_OutputCDR payload (ACE_CDR::MAX_ALIGNMENT + MAX_NAME_LENGTH + msg_length); 
    payload.write_char_array(filename,MAX_NAME_LENGTH); 
	payload.write_char_array(msg_record,msg_length); 
     
	//head 
    ACE_OutputCDR header(ACE_CDR::MAX_ALIGNMENT + 8); 
    header<< ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER); 
    header<< ACE_CDR::ULong (MAX_NAME_LENGTH+msg_length); 
	 
    // Use an iovec to send both buffer and payload simultaneously. 
    iovec iov[2]; 
	iov[0].iov_base = header.begin ()->rd_ptr (); 
    iov[0].iov_len  = 8; 
    iov[1].iov_base = payload.begin ()->rd_ptr (); 
    iov[1].iov_len  = MAX_NAME_LENGTH+msg_length; 
 
    // Send header and payload efficiently using "gather-write". 
    return logging_peer_.sendv_n (iov, 2); 
  } 
 
  ~Logging_Client () { logging_peer_.close (); } 
}; 
Logging_Client logging_client;