16 #ifndef DABC_SocketThread
17 #define DABC_SocketThread
79 ssize_t
DoRecvBufferHdr(
void* hdr, ssize_t hdrlen,
void* buf, ssize_t len,
void* srcaddr = 0,
unsigned srcaddrlen = 0);
81 ssize_t
DoSendBufferHdr(
void* hdr, ssize_t hdrlen,
void* buf, ssize_t len,
void* tgtaddr = 0,
unsigned tgtaddrlen = 0);
105 virtual const char*
ClassName()
const {
return "SocketAddon"; }
167 #ifdef SOCKET_PROFILING
176 virtual const char*
ClassName()
const {
return "SocketIOAddon"; }
210 SocketIOAddon(
int fd = 0,
bool isdatagram =
false,
bool usemsg =
true);
217 void SetSendAddr(
const std::string &host =
"",
int port = 0);
219 bool StartSend(
const void* buf,
unsigned size,
220 const void* buf2 = 0,
unsigned size2 = 0,
221 const void* buf3 = 0,
unsigned size3 = 0);
224 bool StartRecvHdr(
void* hdr,
unsigned hdrsize,
void* buf,
size_t size);
271 virtual const char*
ClassName()
const {
return "SocketConnectAddon"; }
294 struct sockaddr_storage
ai_addr;
301 SocketServerAddon(
int serversocket,
const char* hostname =
nullptr,
int portnum = -1,
struct addrinfo *info =
nullptr);
310 virtual const char*
ClassName()
const {
return "SocketServerAddon"; }
333 virtual const char*
ClassName()
const {
return "SocketClientAddon"; }
378 #ifdef SOCKET_PROFILING
407 static int StartClient(
const std::string &host,
int nport,
bool nonblocking =
true);
410 static int SendBuffer(
int fd,
void* buf,
int len);
413 static int RecvBuffer(
int fd,
void* buf,
int len);
425 static int BindUdp(
int fd,
int nport,
int portmin=-1,
int portmax=-1);
439 static int ConnectUdp(
int fd,
const std::string &remhost,
int remport);
#define DABC_REFERENCE(RefClass, ParentClass, T)
Reference on memory from memory pool.
Represents command with its arguments.
Reference on the arbitrary object
void Release()
Releases reference on the object.
Object * GetObject() const
Return pointer on the object.
Special addon class for handling of socket and socket events.
virtual void ProcessEvent(const EventId &)
bool fDoingOutput
true if data need to be send
bool IsDoingOutput() const
virtual void OnSocketError(int msg, const std::string &info)
Generic error handler.
bool IsDeliverEventsToWorker() const
void SetIOPriority(int prior=1)
Method defines priority level for socket IO events.
void SetDoingInput(bool on=true)
Call method to indicate that object wants to read data from the socket.
@ evntSocketLast
from this event number one can add more socket system events
@ evntSocketSendInfo
event delivered to worker when write is completed
@ evntSocketRecvInfo
event delivered to worker when read is completed
@ evntSocketLastInfo
last system event, used by sockets
@ evntSocketCloseInfo
event delivered to worker when socket is closed
@ evntSocketErrorInfo
event delivered to worker when error is detected
bool fDoingInput
true if input data are expected
virtual const char * ClassName() const
Returns class name of the object instance.
int fIOPriority
priority of socket I/O events, default 1
void SetDeliverEventsToWorker(bool on=true)
virtual std::string RequiredThrdClass() const
bool IsDeleteWorkerOnClose() const
ssize_t DoSendBuffer(void *buf, ssize_t len)
bool IsDoingInput() const
void SetDeleteWorkerOnClose(bool on=true)
ssize_t DoSendBufferHdr(void *hdr, ssize_t hdrlen, void *buf, ssize_t len, void *tgtaddr=0, unsigned tgtaddrlen=0)
bool fDeleteWorkerOnClose
if true, worker will be deleted when socket closed or socket in error
bool fDeliverEventsToWorker
if true, completion events will be delivered to the worker
ssize_t DoRecvBufferHdr(void *hdr, ssize_t hdrlen, void *buf, ssize_t len, void *srcaddr=0, unsigned srcaddrlen=0)
void SetDoingOutput(bool on=true)
Call method to indicate that worker wants to write data to the socket.
ssize_t DoRecvBuffer(void *buf, ssize_t len)
Socket addon for handling connection on client side.
virtual const char * ClassName() const
Returns class name of the object instance.
virtual void OnConnectionFailed()
struct addrinfo fServAddr
void SetRetryOpt(int nretry, double tmout=1.)
virtual void OnThreadAssigned()
SocketClientAddon(const struct addrinfo *serv_addr, int fd=-1)
virtual double ProcessTimeout(double)
virtual void OnConnectionEstablished(int fd)
virtual void ProcessEvent(const EventId &)
virtual ~SocketClientAddon()
Socket addon for handling connection events.
virtual const char * ClassName() const
Returns class name of the object instance.
virtual ~SocketConnectAddon()
SocketConnectAddon(int fd)
virtual void ObjectCleanup()
User method to cleanup object content before it will be destroyed Main motivation is to release any r...
void SetConnHandler(const WorkerRef &rcv, const std::string &connid)
Set connection handler.
Socket addon for handling I/O events.
unsigned fRecvIOVNumber
number of elements in current recv operation
unsigned fSendIOVSize
total number of elements in send vector
void AllocateSendIOV(unsigned size)
struct iovec * fRecvIOV
receive io vector for scatter list
unsigned fLastRecvSize
size of last recv operation
bool StartSend(const void *buf, unsigned size, const void *buf2=0, unsigned size2=0, const void *buf3=0, unsigned size3=0)
unsigned GetRecvSize() const
Method return size of last buffer read from socket.
struct iovec * fSendIOV
sending io vector for gather list
bool fUseMsgOper
indicate if sendmsg, recvmsg operations should be used, it is must for the datagram sockets
unsigned fSendIOVNumber
number of elements in current send operation
virtual ~SocketIOAddon()
Destructor of SocketIOAddon class.
void SetSendAddr(const std::string &host="", int port=0)
Set destination address for all send operations,.
unsigned fRecvIOVFirst
number of element in recv IOV where transfer is started
bool fDatagramSocket
indicate if socket is datagram and all operations should be finished with single call
struct sockaddr_in fSendAddr
optional send address for next send operation
bool IsDatagramSocket() const
unsigned fRecvIOVSize
number of elements in recv vector
bool fSendUseMsg
use sendmsg for transport
struct sockaddr_in fRecvAddr
source address of last receive operation
virtual void OnSendCompleted()
Method called when send operation is completed.
void AllocateRecvIOV(unsigned size)
unsigned fSendIOVFirst
number of element in send IOV where transfer is started
virtual const char * ClassName() const
Returns class name of the object instance.
virtual void OnRecvCompleted()
Method called when receive operation is completed.
struct sockaddr_in & GetRecvAddr()
Method provide address of last receive operation.
bool StartRecv(void *buf, size_t size)
bool StartNetSend(void *hdr, unsigned hdrsize, const Buffer &buf)
bool StartRecvHdr(void *hdr, unsigned hdrsize, void *buf, size_t size)
bool fSendUseAddr
if true, fSendAddr will be used
bool fRecvUseMsg
use recvmsg for transport
virtual void ProcessEvent(const EventId &)
SocketIOAddon(int fd=0, bool isdatagram=false, bool usemsg=true)
Constructor of SocketIOAddon class.
void CancelIOOperations()
Method should be used to cancel all running I/O operation of the socket.
bool StartNetRecv(void *hdr, unsigned hdrsize, Buffer &buf, BufferSize_t datasize)
Socket addon for handling connection requests on server side.
std::string fServerHostName
virtual const char * ClassName() const
Returns class name of the object instance.
struct sockaddr_storage ai_addr
std::string ServerHostName()
virtual void OnClientConnected(int fd)
SocketServerAddon(int serversocket, const char *hostname=nullptr, int portnum=-1, struct addrinfo *info=nullptr)
virtual void ProcessEvent(const EventId &)
int ServerPortNumber() const
virtual ~SocketServerAddon()
Special thread class for handling sockets.
bool fWaitFire
indicates if pipe firing is awaited
static std::string DefineHostName(bool force=true)
Return current host name.
int fBalanceCnt
counter for balancing of input events
int fScalerCounter
variable used to test time to time sockets even if there are events in the queue
virtual bool WaitEvent(EventId &, double tmout)
int fPipe[2]
array with i/o pipes handles
static void CloseUdp(int fd)
Close datagram (udp) socket.
virtual void ProcessExtraThreadEvent(const EventId &evid)
Method to process events which are not processed by Thread class itself Should be used in derived cla...
static SocketClientAddon * CreateClientAddon(const std::string &servid, int dflt_port=-1)
virtual void _Fire(const EventId &evnt, int nq)
static void DettachMulticast(int handle, const std::string &addr)
Detach datagram socket from multicast group.
virtual void WorkersSetChanged()
Virtual method, called from thread context to inform that number of workers are changed.
pollfd * f_ufds
list of file descriptors for poll call
bool fCheckNewEvents
flag indicate if sockets should be checked for new events even if there are already events in the que...
static int SendBuffer(int fd, void *buf, int len)
Wrapper for send method, should be used for blocking sockets.
static int StartClient(const std::string &host, int nport, bool nonblocking=true)
static int CreateUdp()
Create datagram (udp) socket.
virtual bool CompatibleClass(const std::string &clname) const
bool fIsAnySocket
indicates that at least one socket processors in the list
static bool AttachMulticast(int handle, const std::string &addr)
Attach datagram socket to multicast group to make receiving.
static int RecvBuffer(int fd, void *buf, int len)
Wrapper for recv method, should be used for blocking sockets.
static int BindUdp(int fd, int nport, int portmin=-1, int portmax=-1)
Bind UDP socket to specified port.
@ evntEnableCheck
event to enable again checking sockets for new events
@ evntLastSocketThrdEvent
last event, which can be used by socket
static int ConnectUdp(int fd, const std::string &remhost, int remport)
unsigned f_sizeufds
size of the structure, which was allocated
SocketThread(Reference parent, const std::string &name, Command cmd)
ProcRec * f_recs
identify used processors
virtual const char * ClassName() const
Returns class name of the object instance.
static bool SetNonBlockSocket(int fd)
static SocketServerAddon * CreateServerAddon(const std::string &host, int nport, int portmin=-1, int portmax=-1)
Create handle for server-side connection If hostname == 0, any available address will be selected If ...
long fPipeFired
indicate if something was written in pipe
static bool SetNoDelaySocket(int fd)
Represent thread functionality.
Reference on dabc::WorkerAddon object
Generic addon for dabc::Worker.
void FireWorkerEvent(unsigned evid)
virtual void ObjectCleanup()
User method to cleanup object content before it will be destroyed Main motivation is to release any r...
Reference on dabc::Worker
const char * typeSocketThread
std::string format(const char *fmt,...)
Event structure, exchanged between DABC threads.
uint32_t indx
index for dereference of processor from ufds structure
bool use
indicates if processor is used for poll