25 #define SocketServerTmout 0.2
29 class SocketProtocolAddon;
132 memcpy(
fOutBuf, &header,
sizeof(header));
176 DOUT5(
"Client job finished");
222 while (fProtocols.size()>0) {
224 fProtocols.remove_at(0);
233 if (!fCmdChannelId.empty())
return fCmdChannelId;
239 int port0 = fBindPort, portmin(7000), portmax(9000);
240 if (port0 > 0) portmin = portmax = 0;
244 DOUT0(
"SocketDevice creates server with ID %s", serv->
ServerId().c_str());
249 if (!serv)
return std::string();
259 bool firetmout =
false;
262 fConnRecs.push_back(rec);
263 firetmout = (fConnRecs.size() == 1);
265 if (firetmout) ActivateTimeout(0.);
281 for (
unsigned n=0; n<fConnRecs.size();n++) {
284 if (rec->
IsConnId(connid))
return rec;
294 bool more_timeout =
false;
301 while (n<fConnRecs.size()) {
305 if ((rec->
fTmOut<0) || (tmout<0)) {
306 if (tmout>0)
EOUT(
"Record %u timedout", n);
307 fConnRecs.remove_at(n);
308 del_recs.push_back(rec);
313 more_timeout = fConnRecs.size() > 0;
316 for (
unsigned n=0;n<del_recs.size();n++)
340 std::string serverid = StartServerAddon();
374 thread().MakeWorkerFor(client);
390 EOUT(
"Request from connection manager in undefined situation progress = %d ???", req.
progress());
402 if (cmd.
IsName(CmdConnectionManagerHandle::CmdName())) {
404 cmd_res = HandleManagerConnectionRequest(cmd);
408 if (cmd.
IsName(
"SocketConnect")) {
409 std::string typ = cmd.
GetStr(
"Type");
410 std::string connid = cmd.
GetStr(
"ConnId");
411 int fd = cmd.
GetInt(
"fd", -1);
413 if (typ ==
"Server") {
414 DOUT2(
"SocketDevice:: create server protocol for socket %d connid %s", fd, connid.c_str());
418 thread().MakeWorkerFor(proto, connid);
421 fProtocols.push_back(proto);
423 if (typ ==
"Client") {
430 EOUT(
"Client connected for not exiting rec %s", connid.c_str());
434 DOUT2(
"SocketDevice:: create client protocol for socket %d connid:%s", fd, connid.c_str());
441 if (proto) thread().MakeWorkerFor(proto, connid);
443 if (typ ==
"Error") {
447 rec = _FindRec(connid.c_str());
449 EOUT(
"Client error for not existing rec %s", connid.c_str());
452 EOUT(
"Client error for connid %s", connid.c_str());
454 fConnRecs.remove(rec);
458 if (rec) DestroyRec(rec,
false);
462 if (cmd.
IsName(
"RedirectConnect")) {
463 int fd = cmd.
GetInt(
"Socket");
468 thread().MakeWorkerFor(proto, fCmdChannelId);
471 fProtocols.push_back(proto);
484 strcpy(outmsg,
"denied");
486 uint32_t *header = (uint32_t*) inmsg;
488 EOUT(
"Wrong header identifier in the SOCKET connect");
496 rec = _FindRec(inmsg+
sizeof(uint32_t));
500 strcpy(outmsg,
"accepted");
503 fProtocols.remove(proc);
516 if ((rec==0) || !fConnRecs.has_ptr(rec)) {
517 EOUT(
"Protocol completed without rec %p", rec);
518 fProtocols.remove(proc);
523 if (destr)
return true;
526 if (inmsg) res = (strcmp(inmsg,
"accepted")==0);
528 if (inmsg)
DOUT3(
"Reply from server: %s", inmsg);
540 DOUT0(
"Create socket transport for fd %d res %s", fd,
DBOOL(res));
543 RemoveProtocolAddon(proc, res);
557 fConnRecs.remove(rec);
560 fProtocols.remove(proc);
563 DestroyRec(rec, res);
#define SocketServerTmout
Reference on memory from memory pool.
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
static const char * ReqArg()
Represents command with its arguments.
void ReplyBool(bool res)
Reply on the command with true or false value.
bool SetStr(const std::string &name, const char *value)
std::string GetStr(const std::string &name, const std::string &dflt="") const
int GetInt(const std::string &name, int dflt=0) const
Buffer GetRawData()
Returns reference on raw data Can be called only once - raw data reference will be cleaned.
static std::string GetLocalHost()
@ progrDoingConnect
at this state device should drive connection itself and inform about completion or failure
@ progrDoingInit
state when record should be prepared by device
Full description of connection request.
std::string GetConnId() const
std::string GetConnInfo()
std::string GetServerId() const
void ReplyRemoteCommand(bool res)
void SetClientId(const std::string &id)
void SetServerId(const std::string &id)
double GetConnTimeout() const
time required to establish connection, if expired connection will be switched to "failed" state
bool IsServerSide() const
Indicates if local node in connection is server or client.
Base class for device implementation.
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Lock guard for posix mutex.
Parameter FindPar(const std::string &parname)
WorkerRef GetCommandChannel()
static bool Make(const ConnectionRequest &req, WorkerAddon *addon, const std::string &devthrdname="")
bool IsConnId(const char *id)
const char * ConnId() const
double fTmOut
used by device to process connection timeouts
std::string fReqItem
reference in connection request
SocketClientAddon * fClient
client-side processor, to establish connection
NewConnectRec(const std::string &item, ConnectionRequestFull &req, SocketClientAddon *clnt)
SocketProtocolAddon * fProtocol
protocol processor, to verify connection id
Command fLocalCmd
connection id
std::string ItemName(bool compact=true) const
Produce string, which can be used as name argument in dabc::mgr.FindItem(name) call.
const char * GetName() const
Returns name of the object, thread safe
static void Destroy(Object *obj)
User method for object destroyment.
Specialized vector with pointers.
std::string AsStr(const std::string &dflt="") const
int64_t AsInt(int64_t dflt=0) const
bool IsName(const char *name) const
Returns true if object name is the same as specified one.
bool null() const
Returns true if reference contains nullptr.
@ evntSocketLast
from this event number one can add more socket system events
Socket addon for handling connection on client side.
void SetRetryOpt(int nretry, double tmout=1.)
void SetConnHandler(const WorkerRef &rcv, const std::string &connid)
Set connection handler.
Device for establishing socket connections
void ServerProtocolRequest(SocketProtocolAddon *proc, const char *inmsg, char *outmsg)
bool ProtocolCompleted(SocketProtocolAddon *proc, const char *inmsg)
void DestroyRec(NewConnectRec *rec, bool res)
std::string StartServerAddon()
int HandleManagerConnectionRequest(Command cmd)
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
virtual double ProcessTimeout(double last_diff)
bool CleanupRecs(double tmout)
void RemoveProtocolAddon(SocketProtocolAddon *proc, bool res)
SocketDevice(const std::string &name, Command cmd)
NewConnectRec * _FindRec(const char *connid)
std::string fCmdChannelId
void AddRec(NewConnectRec *rec)
Socket addon for handling I/O events.
bool StartSend(const void *buf, unsigned size, const void *buf2=0, unsigned size2=0, const void *buf3=0, unsigned size3=0)
bool StartRecv(void *buf, size_t size)
Specific implementation of network transport for socket.
virtual void OnSendCompleted()
Method called when send operation is completed.
virtual void OnRecvCompleted()
Method called when receive operation is completed.
char fInBuf[SocketDevice::ProtocolMsgSize]
void FinishWork(bool res)
virtual ~SocketProtocolAddon()
SocketProtocolAddon(int connfd, SocketDevice *dev, NewConnectRec *rec, void *redirect=0)
virtual void OnThreadAssigned()
char fOutBuf[SocketDevice::ProtocolMsgSize]
virtual void OnSocketError(int, const std::string &)
Generic error handler.
Socket addon for handling connection requests on server side.
static SocketClientAddon * CreateClientAddon(const std::string &servid, int dflt_port=-1)
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 ...
void DeleteWorker()
This is way to delete worker with addon inclusive.
virtual void OnThreadAssigned()
Reference on dabc::Worker
bool Execute(Command cmd, double tmout=-1.)
RecordField Cfg(const std::string &name, Command cmd=nullptr) const
Returns configuration field of specified name Configuration value of specified name searched in follo...