cosmetics+prepare change to shared_ptr+import unix socket support

This commit is contained in:
Jean-Francois Dockes 2015-05-21 18:02:27 +02:00
parent 45f37cfffb
commit efa5882a79
3 changed files with 781 additions and 571 deletions

1
src/common/config.h Symbolic link
View File

@ -0,0 +1 @@
autoconfig.h

File diff suppressed because it is too large Load Diff

View File

@ -18,9 +18,9 @@
*/ */
#include <sys/time.h> #include <sys/time.h>
#include <map> #include <map>
#include "refcntr.h" #include <string>
using std::map; #include "refcntr.h"
/// A set of classes to manage client-server communication over a /// A set of classes to manage client-server communication over a
/// connection-oriented network, or a pipe. /// connection-oriented network, or a pipe.
@ -45,8 +45,8 @@ public:
enum Event {NETCONPOLL_READ = 0x1, NETCONPOLL_WRITE=0x2}; enum Event {NETCONPOLL_READ = 0x1, NETCONPOLL_WRITE=0x2};
Netcon() Netcon()
: m_peer(0), m_fd(-1), m_ownfd(true), m_didtimo(0), m_wantedEvents(0), : m_peer(0), m_fd(-1), m_ownfd(true), m_didtimo(0), m_wantedEvents(0),
m_loop(0) m_loop(0) {
{} }
virtual ~Netcon(); virtual ~Netcon();
/// Remember whom we're talking to. We let external code do this because /// Remember whom we're talking to. We let external code do this because
/// the application may have a non-dns method to find the peer name. /// the application may have a non-dns method to find the peer name.
@ -58,11 +58,17 @@ public:
/// Set or reset the TCP_NODELAY option. /// Set or reset the TCP_NODELAY option.
virtual int settcpnodelay(int on = 1); virtual int settcpnodelay(int on = 1);
/// Did the last receive() call time out ? Resets the flag. /// Did the last receive() call time out ? Resets the flag.
virtual int timedout() {int s = m_didtimo; m_didtimo = 0; return s;} virtual int timedout() {
int s = m_didtimo;
m_didtimo = 0;
return s;
}
/// Return string version of last syscall error /// Return string version of last syscall error
virtual char *sterror(); virtual char *sterror();
/// Return the socket descriptor /// Return the socket descriptor
virtual int getfd() {return m_fd;} virtual int getfd() {
return m_fd;
}
/// Close the current connection if it is open /// Close the current connection if it is open
virtual void closeconn(); virtual void closeconn();
/// Set/reset the non-blocking flag on the underlying fd. Returns /// Set/reset the non-blocking flag on the underlying fd. Returns
@ -73,16 +79,26 @@ public:
/// Decide what events the connection will be looking for /// Decide what events the connection will be looking for
/// (NETCONPOLL_READ, NETCONPOLL_WRITE) /// (NETCONPOLL_READ, NETCONPOLL_WRITE)
int setselevents(int evs) {return m_wantedEvents = evs;} int setselevents(int evs) {
return m_wantedEvents = evs;
}
/// Retrieve the connection's currently monitored set of events /// Retrieve the connection's currently monitored set of events
int getselevents() {return m_wantedEvents;} int getselevents() {
return m_wantedEvents;
}
/// Add events to current set /// Add events to current set
int addselevents(int evs) {return m_wantedEvents |= evs;} int addselevents(int evs) {
return m_wantedEvents |= evs;
}
/// Clear events from current set /// Clear events from current set
int clearselevents(int evs) {return m_wantedEvents &= ~evs;} int clearselevents(int evs) {
return m_wantedEvents &= ~evs;
}
friend class SelectLoop; friend class SelectLoop;
SelectLoop *getloop() {return m_loop;} SelectLoop *getloop() {
return m_loop;
}
/// Utility function for a simplified select() interface: check one fd /// Utility function for a simplified select() interface: check one fd
/// for reading or writing, for a specified maximum number of seconds. /// for reading or writing, for a specified maximum number of seconds.
@ -99,7 +115,9 @@ protected:
// Method called by the selectloop when something can be done with a netcon // Method called by the selectloop when something can be done with a netcon
virtual int cando(Netcon::Event reason) = 0; virtual int cando(Netcon::Event reason) = 0;
// Called when added to loop // Called when added to loop
virtual void setloop(SelectLoop *loop) {m_loop = loop;} virtual void setloop(SelectLoop *loop) {
m_loop = loop;
}
}; };
@ -114,8 +132,8 @@ public:
SelectLoop() SelectLoop()
: m_selectloopDoReturn(false), m_selectloopReturnValue(0), : m_selectloopDoReturn(false), m_selectloopReturnValue(0),
m_placetostart(0), m_placetostart(0),
m_periodichandler(0), m_periodicparam(0), m_periodicmillis(0) m_periodichandler(0), m_periodicparam(0), m_periodicmillis(0) {
{} }
/// Loop waiting for events on the connections and call the /// Loop waiting for events on the connections and call the
/// cando() method on the object when something happens (this will in /// cando() method on the object when something happens (this will in
@ -126,8 +144,7 @@ public:
int doLoop(); int doLoop();
/// Call from data handler: make selectloop return the param value /// Call from data handler: make selectloop return the param value
void loopReturn(int value) void loopReturn(int value) {
{
m_selectloopDoReturn = true; m_selectloopDoReturn = true;
m_selectloopReturnValue = value; m_selectloopReturnValue = value;
} }
@ -156,7 +173,7 @@ private:
int m_placetostart; int m_placetostart;
// Map of NetconP indexed by fd // Map of NetconP indexed by fd
map<int, NetconP> m_polldata; std::map<int, NetconP> m_polldata;
// The last time we did the periodic thing. Initialized by setperiodic() // The last time we did the periodic thing. Initialized by setperiodic()
struct timeval m_lasthdlcall; struct timeval m_lasthdlcall;
@ -192,8 +209,8 @@ public:
/// Base class for connections that actually transfer data. T /// Base class for connections that actually transfer data. T
class NetconData : public Netcon { class NetconData : public Netcon {
public: public:
NetconData() : m_buf(0), m_bufbase(0), m_bufbytes(0), m_bufsize(0) NetconData() : m_buf(0), m_bufbase(0), m_bufbytes(0), m_bufsize(0) {
{} }
virtual ~NetconData(); virtual ~NetconData();
/// Write data to the connection. /// Write data to the connection.
@ -218,11 +235,14 @@ public:
virtual int readready(); virtual int readready();
/// Check for data being available for writing /// Check for data being available for writing
virtual int writeready(); virtual int writeready();
/// Read a line of text on an ascii connection /// Read a line of text on an ascii connection. Returns -1 or byte count
/// including final 0. \n is kept
virtual int getline(char *buf, int cnt, int timeo = -1); virtual int getline(char *buf, int cnt, int timeo = -1);
/// Set handler to be called when the connection is placed in the /// Set handler to be called when the connection is placed in the
/// selectloop and an event occurs. /// selectloop and an event occurs.
virtual void setcallback(RefCntr<NetconWorker> user) {m_user = user;} virtual void setcallback(RefCntr<NetconWorker> user) {
m_user = user;
}
private: private:
char *m_buf; // Buffer. Only used when doing getline()s char *m_buf; // Buffer. Only used when doing getline()s
@ -236,13 +256,18 @@ private:
/// Network endpoint, client side. /// Network endpoint, client side.
class NetconCli : public NetconData { class NetconCli : public NetconData {
public: public:
NetconCli(int silent = 0) {m_silentconnectfailure = silent;} NetconCli(int silent = 0) {
m_silentconnectfailure = silent;
}
/// Open connection to specified host and named service. /// Open connection to specified host and named service. Set host
int openconn(const char *host, char *serv, int timeo = -1); /// to an absolute path name for an AF_UNIX service. serv is
/// ignored in this case.
int openconn(const char *host, const char *serv, int timeo = -1);
/// Open connection to specified host and numeric port. port is in /// Open connection to specified host and numeric port. port is in
/// HOST byte order /// HOST byte order. Set host to an absolute path name for an
/// AF_UNIX service. serv is ignored in this case.
int openconn(const char *host, unsigned int port, int timeo = -1); int openconn(const char *host, unsigned int port, int timeo = -1);
/// Reuse existing fd. /// Reuse existing fd.
@ -253,7 +278,9 @@ public:
int setconn(int fd); int setconn(int fd);
/// Do not log message if openconn() fails. /// Do not log message if openconn() fails.
void setSilentFail(int onoff) {m_silentconnectfailure = onoff;} void setSilentFail(int onoff) {
m_silentconnectfailure = onoff;
}
private: private:
int m_silentconnectfailure; // No logging of connection failures if set int m_silentconnectfailure; // No logging of connection failures if set
@ -289,8 +316,9 @@ public:
#endif /* NETCON_ACCESSCONTROL */ #endif /* NETCON_ACCESSCONTROL */
} }
~NetconServLis(); ~NetconServLis();
/// Open named service. /// Open named service. Used absolute pathname to create an
int openservice(char *serv, int backlog = 10); /// AF_UNIX path-based socket instead of an IP one.
int openservice(const char *serv, int backlog = 10);
/// Open service by port number. /// Open service by port number.
int openservice(int port, int backlog = 10); int openservice(int port, int backlog = 10);
/// Wait for incoming connection. Returned connected Netcon /// Wait for incoming connection. Returned connected Netcon
@ -302,12 +330,15 @@ protected:
/// insert the new connection in the selectloop. /// insert the new connection in the selectloop.
virtual int cando(Netcon::Event reason); virtual int cando(Netcon::Event reason);
// Empty if port was numeric, else service name or socket path
std::string m_serv;
private: private:
#ifdef NETCON_ACCESSCONTROL #ifdef NETCON_ACCESSCONTROL
int permsinit; int permsinit;
struct intarrayparam okaddrs; struct intarrayparam okaddrs;
struct intarrayparam okmasks; struct intarrayparam okmasks;
int initperms(char *servicename); int initperms(const char *servicename);
int initperms(int port); int initperms(int port);
int checkperms(void *cli, int clilen); int checkperms(void *cli, int clilen);
#endif /* NETCON_ACCESSCONTROL */ #endif /* NETCON_ACCESSCONTROL */
@ -318,15 +349,15 @@ private:
/// case of a forking server) /// case of a forking server)
class NetconServCon : public NetconData { class NetconServCon : public NetconData {
public: public:
NetconServCon(int newfd, Netcon* lis = 0) NetconServCon(int newfd, Netcon* lis = 0) {
{
m_liscon = lis; m_liscon = lis;
m_fd = newfd; m_fd = newfd;
} }
/// This is for forked servers that want to get rid of the main socket /// This is for forked servers that want to get rid of the main socket
void closeLisCon() { void closeLisCon() {
if (m_liscon) if (m_liscon) {
m_liscon->closeconn(); m_liscon->closeconn();
}
} }
private: private:
Netcon* m_liscon; Netcon* m_liscon;