Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JNET::JControlHost Class Reference

ControlHost class. More...

#include <JControlHost.hh>

Inheritance diagram for JNET::JControlHost:
JNET::JSocketBlocking JLANG::JThrow< JControlHost > JNET::JTCPSocket JNET::JSocket JLANG::JFile JNET::JSocketAddress JLANG::JAbstractFile JLANG::JBinaryInput JLANG::JBinaryOutput JLANG::JComparable< JAbstractFile > JNET::JControlHostObjectOutput< JHead_t > JNET::JControlHostObjectOutput< JTail_t > JLOGGER::JControlHostLogger JNET::JControlHostObjectIterator< T > JNET::JControlHostObjectOutput< T > JNET::JControlHostObjectOutput< JTypeList< JHead_t, JNullType > > JNET::JControlHostObjectOutput< JTypeList< JHead_t, JTail_t > > JNET::JControlHostObjectOutput< JTypeList< JHead_t, JTail_t > >

Public Member Functions

 JControlHost (const JHostname &server)
 Constructor.
 
 JControlHost (const std::string &server, const int port)
 Constructor.
 
 JControlHost (const int ip_number, const int port=DISPATCH_PORT)
 Constructor.
 
 JControlHost (const JTCPSocket &socket)
 Constructor.
 
 ~JControlHost ()
 Destructor.
 
int Subscribe (const JSubscription &subscription)
 Subscribe to single tag.
 
int Subscribe (const JSubscriptionList &subscription)
 Subscribe to list of tags.
 
int MyId (const std::string &nick_name)
 Identify.
 
int SendMeNext ()
 Tell server to send next message.
 
int SendMeAlways ()
 Tell server to send messages forever.
 
int PutFullData (const JTag &tag, const void *buffer, const long long int length)
 Send data.
 
int PutFullData (const std::string &tag, const void *buffer, const long long int length)
 Send data.
 
int PutFullString (const JTag &tag, const std::string &buffer)
 Send string.
 
int PutFullString (const std::string &tag, const std::string &buffer)
 Send string.
 
int Connected ()
 Send version.
 
int WaitHead (JPrefix &prefix)
 Wait for header.
 
int WaitHead (std::string &tag, long long int &length)
 Wait for header.
 
int CheckHead (JPrefix &prefix, JTimeval timeout=JTimeval::min())
 Check for header, without waiting.
 
int CheckHead (std::string &tag, long long int &length, const int timeout_us=0)
 Check for header, without waiting.
 
int GetFullData (void *buffer, long long int length)
 Receive data.
 
int GetFullString (std::string &buffer)
 Receive string.
 
int configure ()
 Configure socket (factory reset).
 
int read (char *buffer, const int length) override
 Read data from socket.
 
int write (const char *buffer, const int length) override
 Write data to socket.
 
void setNonBlocking (const bool on)
 Set non-blocking of I/O.
 
bool getNonBlocking () const
 Get non-blocking of I/O.
 
void setKeepIdle (const int t_s)
 Set the TCP idle time.
 
void setKeepCnt (const int count)
 Set the TCP idle count.
 
void setKeepIntvl (const int t_s)
 Set the TCP interval time.
 
void setTcpNoDelay (const bool on)
 Set TCP no-delay.
 
bool getTcpNoDelay () const
 Get TCP no-delay.
 
void accept (const int server)
 Accept connection from a server.
 
void connect (const int port)
 Connect to port on local host.
 
void connect (const JHostname &hostname)
 Connect to port on specified host.
 
void connect (const std::string &hostname, const int port)
 Connect to port on specified host.
 
void connect (const int ip_number, const int port)
 Connect to port on specified host.
 
int shutdown ()
 Shut down socket.
 
void setKeepAlive (const bool on)
 Set keep alive of socket.
 
bool getKeepAlive () const
 Get keep alive of socket.
 
void setReuseAddress (const bool on)
 Set reuse address.
 
bool getReuseAddress () const
 Get reuse address.
 
void setReceiveBufferSize (const int size)
 Set receive buffer size.
 
int getReceiveBufferSize () const
 Set receive buffer size.
 
void setSendBufferSize (const int size)
 Set send buffer size.
 
int getSendBufferSize () const
 Get send buffer size.
 
int close ()
 Close file.
 
bool in_avail (JTimeval timeout=JTimeval::min()) const
 Check availability of input.
 
bool out_avail (JTimeval timeout=JTimeval::min()) const
 Check availability of output.
 
virtual bool good () const
 Check status.
 
virtual bool fail () const
 Check status.
 
virtual bool bad () const
 Check status.
 
virtual bool eof () const
 Check end of file.
 
bool less (const JAbstractFile &file) const
 Less than operation.
 
int getFileDescriptor () const
 Get file descriptor.
 
void setFileDescriptor (const int file)
 Set file descriptor.
 
bool is_open () const
 Get open status.
 
const sockaddr * getSockaddr () const
 Get sockaddr.
 
sockaddr * getSockaddr ()
 Get sockaddr.
 
int getFamily () const
 Get family.
 
void setFamily (const int family)
 Set family.
 
std::string getHostname () const
 Get host name.
 
int getIPnumber () const
 Get IP number.
 
void setIPnumber (const int ip_number)
 Set IP number.
 
void setIPnumber ()
 Set any IP number.
 
int getPort () const
 Get port number.
 
void setPort (const int port)
 Set port number.
 

Static Public Member Functions

static bool maybe_special (const JTag &tag)
 Check special ControlHost tags.
 
static bool is_valid (const char c)
 Check validity of subscription specifier.
 
static int WhereIs (const std::string &host_name, const std::string &nick_name, std::string &answer)
 Locate ControlHost client(s).
 
static const int getDefaultBufferSize ()
 Default socket buffer size to be used on this system.
 
static int sizeOf ()
 Get size of object.
 
static void Throw (const bool option)
 Enable/disable throw option.
 
static int Throw (const JException_t &error, const int value=-1)
 Throw exception or return error.
 

Static Public Attributes

static const int FILE_CLOSED = -1
 

Protected Member Functions

 JControlHost ()
 Default constructor.
 
template<class T >
void setOption (const int level, const int option, const T value)
 Set socket option.
 
template<class T >
getOption (const int level, const int option) const
 Get socket option.
 

Protected Attributes

int fileDescriptor
 

Static Protected Attributes

static bool do_throw
 throw option
 

Private Member Functions

 JControlHost (const JControlHost &)
 
 JControlHost (JControlHost &&)
 
JControlHostoperator= (const JControlHost &)
 
JControlHostoperator= (JControlHost &&)
 

Private Attributes

JPrefix prefix
 
int result
 

Detailed Description

ControlHost class.

Definition at line 248 of file JControlHost.hh.

Constructor & Destructor Documentation

◆ JControlHost() [1/7]

JNET::JControlHost::JControlHost ( )
inlineprotected

Default constructor.

Definition at line 256 of file JControlHost.hh.

257 {
258 configure();
259 }
int configure()
Configure socket (factory reset).

◆ JControlHost() [2/7]

JNET::JControlHost::JControlHost ( const JHostname & server)
inline

Constructor.

Parameters
serverhost name and optional port number

Definition at line 294 of file JControlHost.hh.

295 {
296 if (server.hostname != "")
297 connect(server.hostname, server.port);
298 else
299 connect(server.port);
300
301 configure();
302 }
void connect(const int port)
Connect to port on local host.

◆ JControlHost() [3/7]

JNET::JControlHost::JControlHost ( const std::string & server,
const int port )
inline

Constructor.

Parameters
serverhost name
portport

Definition at line 311 of file JControlHost.hh.

313 {
314 connect(server, port);
315
316 configure();
317 }

◆ JControlHost() [4/7]

JNET::JControlHost::JControlHost ( const int ip_number,
const int port = DISPATCH_PORT )
inline

Constructor.

Parameters
ip_numberIP number
portport

Definition at line 326 of file JControlHost.hh.

328 {
329 connect(ip_number, port);
330
331 configure();
332 }

◆ JControlHost() [5/7]

JNET::JControlHost::JControlHost ( const JTCPSocket & socket)
inline

Constructor.

Parameters
socketsocket

Definition at line 340 of file JControlHost.hh.

340 :
341 JSocketBlocking(socket)
342 {}
JSocketBlocking()
Default constructor.

◆ ~JControlHost()

JNET::JControlHost::~JControlHost ( )
inline

Destructor.

Definition at line 348 of file JControlHost.hh.

349 {
350 shutdown();
351 }
int shutdown()
Shut down socket.
Definition JSocket.hh:89

◆ JControlHost() [6/7]

JNET::JControlHost::JControlHost ( const JControlHost & )
private

◆ JControlHost() [7/7]

JNET::JControlHost::JControlHost ( JControlHost && )
private

Member Function Documentation

◆ maybe_special()

static bool JNET::JControlHost::maybe_special ( const JTag & tag)
inlinestatic

Check special ControlHost tags.

Parameters
tagtag
Returns
true if possibly special tag; else false

Definition at line 269 of file JControlHost.hh.

270 {
271 return tag[0] == '_';
272 }

◆ is_valid()

static bool JNET::JControlHost::is_valid ( const char c)
inlinestatic

Check validity of subscription specifier.

Parameters
csubscription specifier
Returns
true if valid; else false

Definition at line 281 of file JControlHost.hh.

282 {
283 return (c == SUBSCRIBE_ALL ||
284 c == SUBSCRIBE_ANY);// ||
285 //c == SUBSCRIBE_SHARED_MEMORY);
286 }
@ SUBSCRIBE_ANY
@ SUBSCRIBE_ALL

◆ Subscribe() [1/2]

int JNET::JControlHost::Subscribe ( const JSubscription & subscription)
inline

Subscribe to single tag.

Parameters
subscriptionsubscription
Returns
0 if OK; -1 if socket error

Definition at line 360 of file JControlHost.hh.

361 {
362 return PutFullString(DISPTAG_Subscribe, subscription.toString());
363 }
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
std::string toString() const
Convert tag to string.
Definition JTag.hh:171
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.

◆ Subscribe() [2/2]

int JNET::JControlHost::Subscribe ( const JSubscriptionList & subscription)
inline

Subscribe to list of tags.

Parameters
subscriptionsubscription
Returns
0 if OK; -1 if socket error

Definition at line 372 of file JControlHost.hh.

373 {
374 return PutFullString(DISPTAG_Subscribe, subscription.toString());
375 }

◆ MyId()

int JNET::JControlHost::MyId ( const std::string & nick_name)
inline

Identify.

Parameters
nick_namenick name
Returns
0 if OK; -1 if socket error

Definition at line 384 of file JControlHost.hh.

385 {
386 return PutFullString(DISPTAG_MyId, nick_name);
387 }
static const JTag DISPTAG_MyId("_MyId")

◆ SendMeNext()

int JNET::JControlHost::SendMeNext ( )
inline

Tell server to send next message.

Returns
0 if OK; -1 if socket error

Definition at line 395 of file JControlHost.hh.

396 {
397 return PutFullData(DISPTAG_Gime, NULL, 0);
398 }
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
static const JTag DISPTAG_Gime("_Gime")

◆ SendMeAlways()

int JNET::JControlHost::SendMeAlways ( )
inline

Tell server to send messages forever.

Returns
0 if OK; -1 if socket error

Definition at line 406 of file JControlHost.hh.

407 {
408 return PutFullData(DISPTAG_Always, NULL, 0);
409 }
static const JTag DISPTAG_Always("_Always")

◆ PutFullData() [1/2]

int JNET::JControlHost::PutFullData ( const JTag & tag,
const void * buffer,
const long long int length )
inline

Send data.

Parameters
tagtag
bufferdata
lengthnumber of bytes
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 420 of file JControlHost.hh.

421 {
422 try {
423
424 JPrefix __prefix__(tag, length);
425
426 write((char*) &__prefix__, sizeof(JPrefix));
427
428 if (length != 0) {
429 write((char*) buffer, (int) length);
430 }
431
432 return 0;
433 }
434 catch (const JSocketException& error) {
435 return Throw(error, -1);
436 }
437 }
static void Throw(const bool option)
Definition JThrow.hh:37
int write(const char *buffer, const int length) override
Write data to socket.

◆ PutFullData() [2/2]

int JNET::JControlHost::PutFullData ( const std::string & tag,
const void * buffer,
const long long int length )
inline

Send data.

Parameters
tagtag
bufferdata
lengthnumber of bytes
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 448 of file JControlHost.hh.

449 {
450 try {
451 return PutFullData(JTag(tag), buffer, length);
452 }
453 catch (const JSocketException& error) {
454 return Throw(error, -1);
455 }
456 catch (const JControlHostException& error) {
457 return Throw(error, -2);
458 }
459 }

◆ PutFullString() [1/2]

int JNET::JControlHost::PutFullString ( const JTag & tag,
const std::string & buffer )
inline

Send string.

Parameters
tagtag
bufferdata
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 469 of file JControlHost.hh.

471 {
472 return PutFullData(tag, buffer.c_str(), buffer.size());
473 }

◆ PutFullString() [2/2]

int JNET::JControlHost::PutFullString ( const std::string & tag,
const std::string & buffer )
inline

Send string.

Parameters
tagtag
bufferdata
Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 483 of file JControlHost.hh.

485 {
486 return PutFullData(tag, buffer.c_str(), buffer.size());
487 }

◆ Connected()

int JNET::JControlHost::Connected ( )
inline

Send version.

Returns
0 if OK; -1 if socket error; -2 if other error

Definition at line 495 of file JControlHost.hh.

496 {
498 }
static const JTag DISPTAG_Version("_Version")
static const std::string CHOO_VERSION

◆ WaitHead() [1/2]

int JNET::JControlHost::WaitHead ( JPrefix & prefix)
inline

Wait for header.

Parameters
prefixprefix
Returns
0 if OK; -1 if socket error

Definition at line 507 of file JControlHost.hh.

508 {
509 try {
510
511 read((char*) &this->prefix, sizeof(JPrefix));
512
513 prefix = this->prefix;
514
515 return 0;
516 }
517 catch (const JSocketException& error) {
518 return Throw(error, -1);
519 }
520 }
int read(char *buffer, const int length) override
Read data from socket.

◆ WaitHead() [2/2]

int JNET::JControlHost::WaitHead ( std::string & tag,
long long int & length )
inline

Wait for header.

Parameters
tagtag
lengthnumber of bytes
Returns
0 if OK; -1 if socket error

Definition at line 530 of file JControlHost.hh.

531 {
532 const int rvalue = WaitHead(this->prefix);
533
534 if (rvalue == 0) {
535 tag = this->prefix.getTag();
536 length = this->prefix.getSize();
537 }
538
539 return rvalue;
540 }
int WaitHead(JPrefix &prefix)
Wait for header.
int getSize() const
Get size.
Definition JPrefix.hh:62
const JTag & getTag() const
Get tag.
Definition JTag.hh:86

◆ CheckHead() [1/2]

int JNET::JControlHost::CheckHead ( JPrefix & prefix,
JTimeval timeout = JTimeval::min() )
inline

Check for header, without waiting.

Parameters
prefixprefix
timeouttimeout
Returns
1 if header; 0 if no header; -1 if socket error

Definition at line 550 of file JControlHost.hh.

551 {
552 try {
553
554 if (in_avail(timeout)) {
555
557
558 return 1;
559
560 } else {
561
562 return 0;
563 }
564 }
565 catch (const JSocketException& error) {
566 return Throw(error, -1);
567 }
568 }
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition JFile.hh:106

◆ CheckHead() [2/2]

int JNET::JControlHost::CheckHead ( std::string & tag,
long long int & length,
const int timeout_us = 0 )
inline

Check for header, without waiting.

Parameters
tagtag
lengthnumber of bytes
timeout_ustimeout [us]
Returns
1 if header; 0 if no header; -1 if socket error

Definition at line 579 of file JControlHost.hh.

580 {
581 const int rvalue = CheckHead(this->prefix, timeout_us);
582
583 if (rvalue == 1) {
584 tag = this->prefix.getTag();
585 length = this->prefix.getSize();
586 }
587
588 return rvalue;
589 }
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.

◆ GetFullData()

int JNET::JControlHost::GetFullData ( void * buffer,
long long int length )
inline

Receive data.

Parameters
bufferdata
lengthnumber of bytes
Returns
0 if OK; -1 if socket error

Definition at line 599 of file JControlHost.hh.

600 {
601 try {
602
603 read((char*) buffer, (int) length);
604
605 return 0;
606 }
607 catch (const JSocketException& error) {
608 return Throw(error, -1);
609 }
610 }

◆ GetFullString()

int JNET::JControlHost::GetFullString ( std::string & buffer)
inline

Receive string.

Parameters
bufferdata
Returns
0 if OK; -1 if socket error

Definition at line 619 of file JControlHost.hh.

620 {
621 buffer.resize(this->prefix.getSize());
622
623 return GetFullData((char*) buffer.data(), buffer.size());
624 }
int GetFullData(void *buffer, long long int length)
Receive data.

◆ WhereIs()

static int JNET::JControlHost::WhereIs ( const std::string & host_name,
const std::string & nick_name,
std::string & answer )
inlinestatic

Locate ControlHost client(s).

Parameters
host_namehost name
nick_namenick name
answerlist of host names
Returns
0 if OK; -1 if socket error

Definition at line 635 of file JControlHost.hh.

638 {
639 try {
640
641 using namespace std;
642
643 JControlHost socket(host_name);
644
645 socket.PutFullString(DISPTAG_WhereIs, nick_name);
646
647 string tag;
648 long long int length;
649
650 socket.WaitHead(tag, length);
651 socket.GetFullString(answer);
652
653 return 0;
654 }
655 catch (const JSocketException& error) {
656 return Throw(error, -1);
657 }
658 }
JControlHost()
Default constructor.
static const JTag DISPTAG_WhereIs("_WhereIs")

◆ configure()

int JNET::JControlHost::configure ( )
inline

Configure socket (factory reset).

Returns
0 if OK; -1 if socket error

Definition at line 666 of file JControlHost.hh.

667 {
668 try {
669
670 setTcpNoDelay (true);
671 setReuseAddress(true);
672 setKeepAlive (true);
673 setSendBufferSize (128*1024);
674 setReceiveBufferSize(128*1024);
675 setNonBlocking (false);
676
677 return 0;
678 }
679 catch (const JSocketException& error) {
680 return Throw(error, -1);
681 }
682 }
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition JSocket.hh:148
void setReuseAddress(const bool on)
Set reuse address.
Definition JSocket.hh:126
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition JSocket.hh:104
void setSendBufferSize(const int size)
Set send buffer size.
Definition JSocket.hh:170
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition JTCPSocket.hh:56
void setTcpNoDelay(const bool on)
Set TCP no-delay.

◆ operator=() [1/2]

JControlHost & JNET::JControlHost::operator= ( const JControlHost & )
private

◆ operator=() [2/2]

JControlHost & JNET::JControlHost::operator= ( JControlHost && )
private

◆ read()

int JNET::JSocketBlocking::read ( char * buffer,
const int length )
inlineoverridevirtualinherited

Read data from socket.

Parameters
bufferbuffer
lengthnumber of bytes to read
Returns
number of bytes read

Reimplemented from JNET::JSocket.

Definition at line 52 of file JSocketBlocking.hh.

53 {
54 char* data = buffer;
55 int size = length;
56
57 while (size != 0) {
58
59 const int pos = JSocket::read(data, size);
60
61 if (pos != 0) {
62 data += pos;
63 size -= pos;
64 } else {
65 in_avail(1);
66 }
67 }
68
69 return length - size;
70 }
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition JSocket.hh:198

◆ write()

int JNET::JSocketBlocking::write ( const char * buffer,
const int length )
inlineoverridevirtualinherited

Write data to socket.

Parameters
bufferbuffer
lengthnumber of bytes to write
Returns
number of bytes written

Reimplemented from JNET::JSocket.

Definition at line 80 of file JSocketBlocking.hh.

81 {
82 const char* data = buffer;
83 int size = length;
84
85 while (size != 0) {
86
87 const int pos = JSocket::write(data, size);
88
89 if (pos != 0) {
90 data += pos;
91 size -= pos;
92 } else {
93 out_avail(1);
94 }
95 }
96
97 return length - size;
98 }
bool out_avail(JTimeval timeout=JTimeval::min()) const
Check availability of output.
Definition JFile.hh:119
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition JSocket.hh:237

◆ setNonBlocking()

void JNET::JTCPSocket::setNonBlocking ( const bool on)
inlineinherited

Set non-blocking of I/O.

Parameters
ontrue to enable non-blocking; false to disable

Definition at line 56 of file JTCPSocket.hh.

57 {
58 const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
59 const int mask = FNDELAY;
60
61 if (flags == -1) {
62 THROW(JSocketException, "Get socket option failed " << errno);
63 }
64
65 if (((flags & mask) != mask && on) ||
66 ((flags & mask) != 0 && !on) ) {
67
68 if (fcntl(getFileDescriptor(), F_SETFL, flags ^ mask) < 0) {
69 THROW(JSocketException, "Set socket option failed " << errno);
70 }
71 }
72 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
int getFileDescriptor() const
Get file descriptor.

◆ getNonBlocking()

bool JNET::JTCPSocket::getNonBlocking ( ) const
inlineinherited

Get non-blocking of I/O.

Returns
true if enabled non-blocking; else false

Definition at line 80 of file JTCPSocket.hh.

81 {
82 const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
83 const int mask = FNDELAY;
84
85 if (flags == -1) {
86 THROW(JSocketException, "Get socket option failed " << errno);
87 }
88
89 return ((flags & mask) != 0);
90 }

◆ setKeepIdle()

void JNET::JTCPSocket::setKeepIdle ( const int t_s)
inlineinherited

Set the TCP idle time.

Parameters
t_stime [s]

Definition at line 98 of file JTCPSocket.hh.

99 {
100 setOption(IPROTO_TCP, TCP_KEEPIDLE, t_s);
101 }
#define IPROTO_TCP
Definition JTCPSocket.hh:11
void setOption(const int level, const int option, const T value)
Set socket option.
Definition JSocket.hh:274

◆ setKeepCnt()

void JNET::JTCPSocket::setKeepCnt ( const int count)
inlineinherited

Set the TCP idle count.

Parameters
countcount

Definition at line 109 of file JTCPSocket.hh.

110 {
111 setOption(IPROTO_TCP, TCP_KEEPCNT, count);
112 }

◆ setKeepIntvl()

void JNET::JTCPSocket::setKeepIntvl ( const int t_s)
inlineinherited

Set the TCP interval time.

Parameters
t_stime [s]

Definition at line 120 of file JTCPSocket.hh.

121 {
122 setOption(IPROTO_TCP, TCP_KEEPINTVL, t_s);
123 }

◆ setTcpNoDelay()

void JNET::JTCPSocket::setTcpNoDelay ( const bool on)
inlineinherited

Set TCP no-delay.

Parameters
ontrue to set TCP no-delay; false to disable

Definition at line 131 of file JTCPSocket.hh.

132 {
133 setOption(IPPROTO_TCP, TCP_NODELAY, int(on ? 1 : 0));
134 }

◆ getTcpNoDelay()

bool JNET::JTCPSocket::getTcpNoDelay ( ) const
inlineinherited

Get TCP no-delay.

Returns
true if TCP no-delay; else false

Definition at line 142 of file JTCPSocket.hh.

143 {
144 return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
145 }

◆ accept()

void JNET::JTCPSocket::accept ( const int server)
inlineinherited

Accept connection from a server.

Parameters
serverfile descriptor of TCP server socket

Definition at line 153 of file JTCPSocket.hh.

154 {
155 socklen_t size = sizeof(sockaddr_in);
156
157 fileDescriptor = ::accept(server, getSockaddr(), &size);
158 }
const sockaddr * getSockaddr() const
Get sockaddr.
void accept(const int server)
Accept connection from a server.

◆ connect() [1/4]

void JNET::JTCPSocket::connect ( const int port)
inlineinherited

Connect to port on local host.

Parameters
portport number

Definition at line 166 of file JTCPSocket.hh.

167 {
168 connect(INADDR_ANY, port);
169 }

◆ connect() [2/4]

void JNET::JTCPSocket::connect ( const JHostname & hostname)
inlineinherited

Connect to port on specified host.

Parameters
hostnamehost name

Definition at line 177 of file JTCPSocket.hh.

178 {
179 connect(hostname.hostname, hostname.port);
180 }

◆ connect() [3/4]

void JNET::JTCPSocket::connect ( const std::string & hostname,
const int port )
inlineinherited

Connect to port on specified host.

Parameters
hostnamehost name
portport number

Definition at line 189 of file JTCPSocket.hh.

190 {
191 connect(JSYSTEM::getIPnumber(hostname), port);
192 }
int getIPnumber()
Get IP number.
Definition JNetwork.hh:142

◆ connect() [4/4]

void JNET::JTCPSocket::connect ( const int ip_number,
const int port )
inlineinherited

Connect to port on specified host.

Parameters
ip_numberIP number
portport number

Definition at line 201 of file JTCPSocket.hh.

202 {
203 setIPnumber(ip_number);
204 setPort(port);
205
206 if (::connect(getFileDescriptor(), getSockaddr(), sizeof(sockaddr_in)) < 0) {
207 THROW(JSocketException, "Socket connection failed " << JSYSTEM::getIPaddress(ip_number) << ":" << port << " / " << getFileDescriptor() << " " << errno);
208 }
209 }
void setIPnumber()
Set any IP number.
void setPort(const int port)
Set port number.
std::string getIPaddress()
Get IP address (decimal-dot notation).
Definition JNetwork.hh:178

◆ getDefaultBufferSize()

static const int JNET::JSocket::getDefaultBufferSize ( )
inlinestaticinherited

Default socket buffer size to be used on this system.

Returns
number of bytes

Definition at line 75 of file JSocket.hh.

76 {
77#if __APPLE__
78 return JMATH::MEGABYTE;
79#else
80 return JMATH::GIGABYTE;
81#endif
82 }
static const long long int GIGABYTE
Number of bytes in a mega-byte.
static const long long int MEGABYTE
Number of bytes in a kilo-byte.

◆ shutdown()

int JNET::JSocket::shutdown ( )
inlineinherited

Shut down socket.

Returns
return value

Definition at line 89 of file JSocket.hh.

90 {
91 const int value = ::shutdown(getFileDescriptor(), SHUT_RDWR);
92
93 close();
94
95 return value;
96 }
int close()
Close file.
Definition JFile.hh:57

◆ setKeepAlive()

void JNET::JSocket::setKeepAlive ( const bool on)
inlineinherited

Set keep alive of socket.

Parameters
ontrue to enable keep alive; false to disable

Definition at line 104 of file JSocket.hh.

105 {
106 setOption(SOL_SOCKET, SO_KEEPALIVE, int(on ? 1 : 0));
107 }

◆ getKeepAlive()

bool JNET::JSocket::getKeepAlive ( ) const
inlineinherited

Get keep alive of socket.

Returns
true if keep alive; else false

Definition at line 115 of file JSocket.hh.

116 {
117 return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
118 }

◆ setReuseAddress()

void JNET::JSocket::setReuseAddress ( const bool on)
inlineinherited

Set reuse address.

Parameters
ontrue to enable reuse address; false to disable

Definition at line 126 of file JSocket.hh.

127 {
128 setOption(SOL_SOCKET, SO_REUSEADDR, int(on ? 1 : 0));
129 }

◆ getReuseAddress()

bool JNET::JSocket::getReuseAddress ( ) const
inlineinherited

Get reuse address.

Returns
true if enable reuse address; else false

Definition at line 137 of file JSocket.hh.

138 {
139 return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
140 }

◆ setReceiveBufferSize()

void JNET::JSocket::setReceiveBufferSize ( const int size)
inlineinherited

Set receive buffer size.

Parameters
sizenumber of bytes

Definition at line 148 of file JSocket.hh.

149 {
150 setOption(SOL_SOCKET, SO_RCVBUF, int(size));
151 }

◆ getReceiveBufferSize()

int JNET::JSocket::getReceiveBufferSize ( ) const
inlineinherited

Set receive buffer size.

Returns
number of bytes

Definition at line 159 of file JSocket.hh.

160 {
161 return getOption<int>(SOL_SOCKET, SO_RCVBUF);
162 }
T getOption(const int level, const int option) const
Get socket option.
Definition JSocket.hh:292

◆ setSendBufferSize()

void JNET::JSocket::setSendBufferSize ( const int size)
inlineinherited

Set send buffer size.

Parameters
sizenumber of bytes

Definition at line 170 of file JSocket.hh.

171 {
172 setOption(SOL_SOCKET, SO_SNDBUF, int(size));
173 }

◆ getSendBufferSize()

int JNET::JSocket::getSendBufferSize ( ) const
inlineinherited

Get send buffer size.

Returns
number of bytes

Definition at line 181 of file JSocket.hh.

182 {
183 return getOption<int>(SOL_SOCKET, SO_SNDBUF);
184 }

◆ setOption()

template<class T >
void JNET::JSocket::setOption ( const int level,
const int option,
const T value )
inlineprotectedinherited

Set socket option.

Parameters
levellevel
optionoption
valuevalue

Definition at line 274 of file JSocket.hh.

275 {
276 socklen_t size = sizeof(T);
277
278 if (setsockopt(getFileDescriptor(), level, option, &value, size) < 0) {
279 THROW(JSocketException, "Set socket option failed " << errno);
280 }
281 }

◆ getOption()

template<class T >
T JNET::JSocket::getOption ( const int level,
const int option ) const
inlineprotectedinherited

Get socket option.

Parameters
levellevel
optionoption
Returns
value

Definition at line 292 of file JSocket.hh.

293 {
294 T value;
295 socklen_t size = sizeof(T);
296
297 if (getsockopt(getFileDescriptor(), level, option, &value, &size) < 0) {
298 THROW(JSocketException, "Get socket option failed " << errno);
299 }
300
301 return value;
302 }

◆ close()

int JLANG::JFile::close ( )
inlineinherited

Close file.

Returns
return value

Definition at line 57 of file JFile.hh.

58 {
59 int value = -1;
60
62
63 value = ::close(fileDescriptor);
64
66 result = 0;
67 }
68
69 return value;
70 }
static const int FILE_CLOSED

◆ in_avail()

bool JLANG::JFile::in_avail ( JTimeval timeout = JTimeval::min()) const
inlineinherited

Check availability of input.

This method returns true if at least one byte can be read without blocking.

Parameters
timeouttimeout
Returns
true if ready to read; else false

Definition at line 106 of file JFile.hh.

107 {
108 return JFileDescriptorMask(*this).in_avail(timeout);
109 }

◆ out_avail()

bool JLANG::JFile::out_avail ( JTimeval timeout = JTimeval::min()) const
inlineinherited

Check availability of output.

This method returns true if at least one byte can be written without blocking.

Parameters
timeouttimeout
Returns
true if ready to write; else false

Definition at line 119 of file JFile.hh.

120 {
121 return JFileDescriptorMask(*this).out_avail(timeout);
122 }

◆ good()

virtual bool JLANG::JFile::good ( ) const
inlinevirtualinherited

Check status.

Returns
true if last I/O operation successful; else false

Definition at line 130 of file JFile.hh.

131 {
132 return is_open() && !eof() && !bad();
133 }
bool is_open() const
Get open status.
virtual bool bad() const
Check status.
Definition JFile.hh:152
virtual bool eof() const
Check end of file.
Definition JFile.hh:163

◆ fail()

virtual bool JLANG::JFile::fail ( ) const
inlinevirtualinherited

Check status.

Returns
true if last I/O operation caused logical error; else false

Definition at line 141 of file JFile.hh.

142 {
143 return result == 0;
144 }

◆ bad()

virtual bool JLANG::JFile::bad ( ) const
inlinevirtualinherited

Check status.

Returns
true if last I/O operation caused read/write error; else false

Definition at line 152 of file JFile.hh.

153 {
154 return fail();
155 }
virtual bool fail() const
Check status.
Definition JFile.hh:141

◆ eof()

virtual bool JLANG::JFile::eof ( ) const
inlinevirtualinherited

Check end of file.

Returns
true if end of file; else false

Definition at line 163 of file JFile.hh.

164 {
165 return result == EOF;
166 }

◆ less()

bool JLANG::JAbstractFile::less ( const JAbstractFile & file) const
inlineinherited

Less than operation.

Parameters
fileJAbstractFile to be compared
Returns
true if this file descriptor is less; else false

Definition at line 64 of file JAbstractFile.hh.

65 {
66 return getFileDescriptor() < file.getFileDescriptor();
67 }

◆ getFileDescriptor()

int JLANG::JAbstractFile::getFileDescriptor ( ) const
inlineinherited

Get file descriptor.

Returns
file descriptor

Definition at line 75 of file JAbstractFile.hh.

76 {
77 return fileDescriptor;
78 }

◆ setFileDescriptor()

void JLANG::JAbstractFile::setFileDescriptor ( const int file)
inlineinherited

Set file descriptor.

Parameters
filefile descriptor

Definition at line 86 of file JAbstractFile.hh.

87 {
88 fileDescriptor = file;
89 }

◆ is_open()

bool JLANG::JAbstractFile::is_open ( ) const
inlineinherited

Get open status.

Definition at line 95 of file JAbstractFile.hh.

96 {
98 }

◆ getSockaddr() [1/2]

const sockaddr * JNET::JSocketAddress::getSockaddr ( ) const
inlineinherited

Get sockaddr.

Returns
pointer to sockaddr structure

Definition at line 44 of file JSocketAddress.hh.

45 {
46 return (const sockaddr*) static_cast<const sockaddr_in*>(this);
47 }

◆ getSockaddr() [2/2]

sockaddr * JNET::JSocketAddress::getSockaddr ( )
inlineinherited

Get sockaddr.

Returns
pointer to sockaddr structure

Definition at line 55 of file JSocketAddress.hh.

56 {
57 return (sockaddr*) static_cast<sockaddr_in*>(this);
58 }

◆ getFamily()

int JNET::JSocketAddress::getFamily ( ) const
inlineinherited

Get family.

Returns
family

Definition at line 66 of file JSocketAddress.hh.

67 {
68 return sin_family;
69 }

◆ setFamily()

void JNET::JSocketAddress::setFamily ( const int family)
inlineinherited

Set family.

Parameters
familyfamily

Definition at line 77 of file JSocketAddress.hh.

78 {
79 sin_family = family;
80 }

◆ getHostname()

std::string JNET::JSocketAddress::getHostname ( ) const
inlineinherited

Get host name.

Returns
host name

Definition at line 88 of file JSocketAddress.hh.

89 {
90 return JSYSTEM::getHostname(this->getIPnumber());
91 }
int getIPnumber() const
Get IP number.
std::string getHostname()
Get host name.
Definition JNetwork.hh:77

◆ getIPnumber()

int JNET::JSocketAddress::getIPnumber ( ) const
inlineinherited

Get IP number.

Returns
IP number

Definition at line 99 of file JSocketAddress.hh.

100 {
101 return sin_addr.s_addr;
102 }

◆ setIPnumber() [1/2]

void JNET::JSocketAddress::setIPnumber ( const int ip_number)
inlineinherited

Set IP number.

Parameters
ip_numberIP number

Definition at line 110 of file JSocketAddress.hh.

111 {
112 sin_addr.s_addr = ip_number;
113 }

◆ setIPnumber() [2/2]

void JNET::JSocketAddress::setIPnumber ( )
inlineinherited

Set any IP number.

Definition at line 119 of file JSocketAddress.hh.

120 {
121 sin_addr.s_addr = htonl(INADDR_ANY);
122 }

◆ getPort()

int JNET::JSocketAddress::getPort ( ) const
inlineinherited

Get port number.

Returns
port number

Definition at line 130 of file JSocketAddress.hh.

131 {
132 return ntohs(this->sin_port);
133 }

◆ setPort()

void JNET::JSocketAddress::setPort ( const int port)
inlineinherited

Set port number.

Parameters
portport number

Definition at line 141 of file JSocketAddress.hh.

142 {
143 if (port >= 0 && port <= std::numeric_limits<u_short>::max())
144 sin_port = htons((u_short) port);
145 else
146 THROW(JCastException, "JSocketAddress::setPort() illegal value.");
147 }

◆ sizeOf()

static int JNET::JSocketAddress::sizeOf ( )
inlinestaticinherited

Get size of object.

Returns
number of bytes

Definition at line 155 of file JSocketAddress.hh.

156 {
157 return sizeof(sockaddr_in);
158 }

◆ Throw() [1/2]

static void JLANG::JThrow< JControlHost >::Throw ( const bool option)
inlinestaticinherited

Enable/disable throw option.

Parameters
optiontrue enable; false disable

Definition at line 37 of file JThrow.hh.

38 {
39 do_throw = option;
40 }

◆ Throw() [2/2]

static int JLANG::JThrow< JControlHost >::Throw ( const JException_t & error,
const int value = -1 )
inlinestaticinherited

Throw exception or return error.

Parameters
errorexception
valuereturn code
Returns
return code

Definition at line 51 of file JThrow.hh.

52 {
53 using namespace std;
54
55 if (do_throw) {
56 throw error;
57 }
58
59 cerr << error.what() << endl;
60
61 return value;
62 }

Member Data Documentation

◆ prefix

JPrefix JNET::JControlHost::prefix
mutableprivate

Definition at line 686 of file JControlHost.hh.

◆ result

int JLANG::JFile::result
privateinherited

Definition at line 170 of file JFile.hh.

◆ FILE_CLOSED

const int JLANG::JAbstractFile::FILE_CLOSED = -1
staticinherited

Definition at line 27 of file JAbstractFile.hh.

◆ fileDescriptor

int JLANG::JAbstractFile::fileDescriptor
protectedinherited

Definition at line 102 of file JAbstractFile.hh.

◆ do_throw

bool JLANG::JThrow< JControlHost >::do_throw
staticprotectedinherited

throw option

Set default throw option to true.

Definition at line 28 of file JThrow.hh.


The documentation for this class was generated from the following file: