List of all members.
Public Member Functions |
virtual void | disconnect ()=0 |
virtual bool | fetch_cbk (inlib::uint64) |
| base_socket (std::ostream &a_out, bool a_verbose) |
virtual | ~base_socket () |
bool | verbose () const |
bool | is_connected () const |
int | socket () const |
std::ostream & | out () const |
bool | send_buffer (const char *a_buffer, inlib::uint64 a_length, inlib::uint64 a_BLOCK=65536) |
bool | fetch_buffer (char *a_buffer, inlib::uint64 a_length, inlib::uint64 a_BLOCK=65536) |
bool | fetch_upto_char (char a_char, char *&a_buffer, inlib::uint64 &a_length, unsigned int a_BLOCK=65536) |
bool | fetch_upto_end (char *&a_buffer, inlib::uint64 &a_length, unsigned int a_BLOCK=65536) |
bool | fetch_upto_end (FILE *a_FILE, inlib::uint64 &a_length, unsigned int a_BLOCK=65536) |
template<class T > |
bool | send (const T &a_value) |
template<class T > |
bool | fetch (T &a_value) |
bool | send_uchar (unsigned char a_value) |
bool | fetch_uchar (unsigned char &a_value) |
bool | send_bool (bool a_value) |
bool | fetch_bool (bool &a_value) |
bool | send_string (const std::string &a_string) |
bool | fetch_string (std::string &a_string) |
bool | send_text (const std::vector< std::string > &a_text) |
bool | fetch_text (std::vector< std::string > &a_text) |
bool | send_file (const std::string &a_file) |
bool | fetch_file (const std::string &a_file) |
bool | is_there_input (bool &a_is) |
Protected Member Functions |
| base_socket (const base_socket &aFrom) |
base_socket & | operator= (const base_socket &aFrom) |
void | close () |
Protected Attributes |
std::ostream & | m_out |
bool | m_verbose |
int | m_socket |
bool | m_is_LE |
Detailed Description
Definition at line 138 of file base_socket.
Constructor & Destructor Documentation
inlib::net::base_socket::base_socket |
( |
std::ostream & |
a_out, |
|
|
bool |
a_verbose |
|
) |
| [inline] |
virtual inlib::net::base_socket::~base_socket |
( |
| ) |
[inline, virtual] |
inlib::net::base_socket::base_socket |
( |
const base_socket & |
aFrom | ) |
[inline, protected] |
Member Function Documentation
void inlib::net::base_socket::close |
( |
| ) |
[inline, protected] |
Definition at line 171 of file base_socket.
{
if(m_socket!=(-1)) {
#ifdef WIN32
if(::shutdown(m_socket,SD_BOTH)<0) {
if(WSAGetLastError()==WSAENOTCONN) {
#else
if(::shutdown(m_socket,SHUT_RDWR)<0) {
if(errno==ENOTCONN) {
#endif
} else {
m_out << "inlib::base_socket::close :"
<< " for socket : " << m_socket
<< ", shutdown : " << serror()
<< std::endl;
}
} else {
if(m_verbose) {
m_out << "inlib::base_socket::close :"
<< " close ok."
<< " Socket was " << m_socket << "."
<< std::endl;
}
}
::close(m_socket);
m_socket = -1;
}
}
virtual void inlib::net::base_socket::disconnect |
( |
| ) |
[pure virtual] |
template<class T >
bool inlib::net::base_socket::fetch |
( |
T & |
a_value | ) |
[inline] |
Definition at line 496 of file base_socket.
{
char buffer[sizeof(T)];
if(!fetch_buffer(buffer,sizeof(T))) return false;
inlib::read_buffer<T>(buffer,a_value);
return true;
}
bool inlib::net::base_socket::fetch_bool |
( |
bool & |
a_value | ) |
[inline] |
Definition at line 521 of file base_socket.
{
unsigned char c;
if(!fetch_uchar(c)) {a_value = false;return false;}
a_value = (c==1?true:false);
return true;
}
Definition at line 234 of file base_socket.
{
if(!a_length) return true;
if(m_socket==(-1)) return false;
char* buf = a_buffer;
inlib::uint64 got = 0;
while(true) {
inlib::uint64 to_get = mn(a_BLOCK,a_length-got);
if(!to_get) break;
#ifdef WIN32
int
#else
ssize_t
#endif
num_char = ::recv(m_socket,buf,(size_t)to_get,0);
if(num_char<0) {
m_out << "inlib::base_socket::fetch_buffer :"
<< " recv : " << serror()
<< std::endl;
disconnect();
return false;
}
if(num_char==0) {
m_out << "inlib::base_socket::fetch_buffer :"
<< " recv : returned 0."
<< std::endl;
return false;
}
buf += num_char;
got += num_char;
}
return true;
}
virtual bool inlib::net::base_socket::fetch_cbk |
( |
inlib::uint64 |
| ) |
[inline, virtual] |
bool inlib::net::base_socket::fetch_file |
( |
const std::string & |
a_file | ) |
[inline] |
Definition at line 588 of file base_socket.
{
inlib::uint64 length;
if(!fetch<inlib::uint64>(length)) return false;
char* buffer = 0;
if(length) {
buffer = new char[(size_t)length];
if(!buffer) return false;
}
if(!fetch_buffer(buffer,length)) {
delete [] buffer;
return false;
}
bool stat = inlib::file::write_bytes(a_file,buffer,(size_t)length);
if(!stat) {
m_out << "inlib::base_socket::send_file : "
<< " can't write file \"" << a_file << "\"."
<< std::endl;
}
delete [] buffer;
return stat;
}
bool inlib::net::base_socket::fetch_string |
( |
std::string & |
a_string | ) |
[inline] |
Definition at line 533 of file base_socket.
{
inlib::uint64 length;
if(!fetch<inlib::uint64>(length)) {
a_string.clear();
return false;
}
a_string.resize((std::string::size_type)length);
if(!fetch_buffer((char*)a_string.c_str(),length)) {
a_string.clear();
return false;
}
return true;
}
bool inlib::net::base_socket::fetch_text |
( |
std::vector< std::string > & |
a_text | ) |
[inline] |
Definition at line 556 of file base_socket.
{
inlib::uint64 number;
if(!fetch<inlib::uint64>(number)) {
a_text.clear();
return false;
}
for(unsigned int index=0;index<number;index++) {
std::string s;
if(!fetch_string(s)) {
a_text.clear();
return false;
}
a_text.push_back(s);
}
return true;
}
bool inlib::net::base_socket::fetch_uchar |
( |
unsigned char & |
a_value | ) |
[inline] |
Definition at line 509 of file base_socket.
{
char buffer[1];
if(!fetch_buffer(buffer,1)) return false;
::memcpy(&a_value,buffer,1);
return true;
}
bool inlib::net::base_socket::fetch_upto_char |
( |
char |
a_char, |
|
|
char *& |
a_buffer, |
|
|
inlib::uint64 & |
a_length, |
|
|
unsigned int |
a_BLOCK = 65536 |
|
) |
| [inline] |
Definition at line 270 of file base_socket.
{
a_buffer = 0;
a_length = 0;
if(m_socket==(-1)) return false;
char* buf = new char[a_BLOCK];
if(!buf) return false;
while(true) {
#ifdef WIN32
int
#else
ssize_t
#endif
num_char = ::recv(m_socket,buf,(size_t)a_BLOCK,0);
if(num_char<0) {
m_out << "inlib::base_socket::fetch_upto_char :"
<< " recv : " << serror()
<< std::endl;
disconnect();
delete [] buf;
delete [] a_buffer;
a_buffer = 0;
a_length = 0;
return false;
}
if(num_char==0) {
m_out << "inlib::base_socket::fetch_upto_char :"
<< " recv : returned 0."
<< std::endl;
delete [] buf;
delete [] a_buffer;
a_buffer = 0;
a_length = 0;
return false;
}
if(m_verbose) {
m_out << "inlib::base_socket::fetch_upto_char :"
<< " recv : " << (int)num_char
<< std::endl;
}
if(!a_buffer) {
a_buffer = new char[num_char];
if(!a_buffer) {
delete [] buf;
a_length = 0;
return false;
}
::memcpy(a_buffer,buf,num_char);
a_length = num_char;
} else {
char* b = new char[(size_t)(a_length+num_char)];
if(!b) {
delete [] buf;
delete [] a_buffer;
a_buffer = 0;
a_length = 0;
return false;
}
::memcpy(b,a_buffer,(size_t)a_length);
::memcpy(b+a_length,buf,num_char);
delete [] a_buffer;
a_buffer = b;
a_length += num_char;
}
{char* pos = buf;
for(unsigned int index=0;index<(unsigned int)num_char;index++,pos++) {
if(*pos==a_char) {
a_length -= (num_char-(index+1));
delete [] buf;
return true;
}
}}
}
}
bool inlib::net::base_socket::fetch_upto_end |
( |
char *& |
a_buffer, |
|
|
inlib::uint64 & |
a_length, |
|
|
unsigned int |
a_BLOCK = 65536 |
|
) |
| [inline] |
Definition at line 356 of file base_socket.
{
a_buffer = 0;
a_length = 0;
if(m_socket==(-1)) return false;
char* buf = new char[a_BLOCK];
if(!buf) return false;
while(true) {
#ifdef WIN32
int
#else
ssize_t
#endif
num_char = ::recv(m_socket,buf,(size_t)a_BLOCK,0);
if(num_char<0) {
m_out << "inlib::base_socket::fetch_upto_end :"
<< " recv : " << serror()
<< std::endl;
disconnect();
delete [] buf;
delete [] a_buffer;
a_buffer = 0;
a_length = 0;
return false;
}
if(num_char==0) {
if(m_verbose) {
m_out << "inlib::base_socket::fetch_upto_end :"
<< " end."
<< std::endl;
}
delete [] buf;
return true;
}
if(m_verbose) {
m_out << "inlib::base_socket::fetch_upto_end :"
<< " recv : " << (int)num_char
<< std::endl;
}
if(!a_buffer) {
a_buffer = new char[num_char];
if(!a_buffer) {
delete [] buf;
a_length = 0;
return false;
}
::memcpy(a_buffer,buf,num_char);
a_length = num_char;
} else {
char* b = new char[(size_t)(a_length+num_char)];
if(!b) {
delete [] buf;
delete [] a_buffer;
a_buffer = 0;
a_length = 0;
return false;
}
::memcpy(b,a_buffer,(size_t)a_length);
::memcpy(b+a_length,buf,num_char);
delete [] a_buffer;
a_buffer = b;
a_length += num_char;
}
}
}
bool inlib::net::base_socket::fetch_upto_end |
( |
FILE * |
a_FILE, |
|
|
inlib::uint64 & |
a_length, |
|
|
unsigned int |
a_BLOCK = 65536 |
|
) |
| [inline] |
Definition at line 428 of file base_socket.
{
a_length = 0;
if(m_socket==(-1)) return false;
char* buf = new char[a_BLOCK];
if(!buf) return false;
while(true) {
#ifdef WIN32
int
#else
ssize_t
#endif
num_char = ::recv(m_socket,buf,(size_t)a_BLOCK,0);
if(num_char<0) {
m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
<< " recv : " << serror()
<< std::endl;
disconnect();
delete [] buf;
a_length = 0;
return false;
}
if(num_char==0) {
if(m_verbose) {
m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
<< " end."
<< std::endl;
}
delete [] buf;
return true;
}
if(m_verbose) {
m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
<< " recv : " << (int)num_char
<< std::endl;
}
if(::fwrite((char*)buf,num_char,(size_t)1,a_FILE)!=1) {
delete [] buf;
a_length = 0;
return false;
}
a_length += num_char;
if(!fetch_cbk(a_length)) {
m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
<< " fetch_cbk returns false, stop recv data."
<< std::endl;
return false;
}
}
}
bool inlib::net::base_socket::is_connected |
( |
| ) |
const [inline] |
bool inlib::net::base_socket::is_there_input |
( |
bool & |
a_is | ) |
[inline] |
Definition at line 610 of file base_socket.
{
a_is = false;
if(m_socket==(-1)) return false;
fd_set mask;
FD_ZERO(&mask);
FD_SET(m_socket,&mask);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 10;
int nfds = 0;
nfds = mx(nfds,m_socket);
nfds++;
if(::select(nfds,&mask,0,0,&timeout)==(-1)) {
a_is = false;
return false;
}
a_is = FD_ISSET(m_socket,&mask)?true:false;
return true;
}
std::ostream& inlib::net::base_socket::out |
( |
| ) |
const [inline] |
template<class T >
bool inlib::net::base_socket::send |
( |
const T & |
a_value | ) |
[inline] |
Definition at line 489 of file base_socket.
{
char buffer[sizeof(T)];
inlib::write_buffer<T>(a_value,buffer);
return send_buffer(buffer,sizeof(T));
}
bool inlib::net::base_socket::send_bool |
( |
bool |
a_value | ) |
[inline] |
bool inlib::net::base_socket::send_buffer |
( |
const char * |
a_buffer, |
|
|
inlib::uint64 |
a_length, |
|
|
inlib::uint64 |
a_BLOCK = 65536 |
|
) |
| [inline] |
Definition at line 200 of file base_socket.
{
if(!a_length) return true;
if(m_socket==(-1)) return false;
char* buf = (char*)a_buffer;
inlib::uint64 sent = 0;
while(true) {
inlib::uint64 to_send = mn(a_BLOCK,a_length-sent);
if(!to_send) break;
#ifdef WIN32
int
#else
ssize_t
#endif
num_char = ::send(m_socket,(char*)buf,(size_t)to_send,0);
if(num_char<0) {
m_out << "inlib::base_socket::send_buffer :"
<< " send : " << serror()
<< std::endl;
disconnect();
return false;
}
if(num_char==0) {
m_out << "inlib::base_socket::send_buffer :"
<< " send : returned 0."
<< std::endl;
}
buf += num_char;
sent += num_char;
}
return true;
}
bool inlib::net::base_socket::send_file |
( |
const std::string & |
a_file | ) |
[inline] |
Definition at line 573 of file base_socket.
{
char* buffer;
long length;
if(!inlib::file::read_bytes(a_file,buffer,length)) {
m_out << "inlib::base_socket::send_file : "
<< " can't read file \"" << a_file << "\"."
<< std::endl;
return false;
}
if(!send<inlib::uint64>((inlib::uint64)length)) return false;
bool stat = send_buffer(buffer,length);
delete [] buffer;
return stat;
}
bool inlib::net::base_socket::send_string |
( |
const std::string & |
a_string | ) |
[inline] |
Definition at line 528 of file base_socket.
{
if(!send<inlib::uint64>(a_string.size())) return false;
return send_buffer(a_string.c_str(),a_string.size());
}
bool inlib::net::base_socket::send_text |
( |
const std::vector< std::string > & |
a_text | ) |
[inline] |
Definition at line 547 of file base_socket.
{
unsigned int number = a_text.size();
if(!send<inlib::uint64>(number)) return false;
for(unsigned int index=0;index<number;index++) {
if(!send_string(a_text[index])) return false;
}
return true;
}
bool inlib::net::base_socket::send_uchar |
( |
unsigned char |
a_value | ) |
[inline] |
int inlib::net::base_socket::socket |
( |
| ) |
const [inline] |
bool inlib::net::base_socket::verbose |
( |
| ) |
const [inline] |
Member Data Documentation
The documentation for this class was generated from the following file:
- /Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/net/base_socket