design patterns - Non-OS Specific FD(File Descriptor) for C/C++ -


linux treated file, network socket. but, windows not. , common files , network sockets treated "fd". if code should not rely on operating system, how should write?

i think below..

#ifndef invalid_socket #define invalid_socket (-1) #endif  class descriptor { private:    int m_fd;  public:    descriptor() : m_fd(invalid_socket) { }    virtual ~descriptor() { this->close(); }    virtual bool isvalid();    virtual bool close() = 0;    virtual int getno() { return m_fd; } };  enum elistenflags {    e_listen_read = 1,    e_listen_write = 2,    e_listen_error = 4,    e_listen_nonblock = 8 };  class asyncdescriptor : public descriptor { // epoll (for linux) or iocp (for windows) or select, poll... public:    virtual bool listen(descriptor* pdesc, int listenflags) = 0;    virtual bool dizzy(descriptor* pdesc, int dizzyflags) = 0;    virtual bool wait(std::list<descriptor*>& listout) = 0;    virtual bool list(std::list<descriptor*>& listout) = 0;    virtual bool getflags(descriptor* pdesc, int* flagoutput) = 0; };  class socketdescriptor : public descriptor {      // omitted....... };  // details omitted below ... 

how can implement it???! :(

you cannot (getting non-os specific file descriptor, or portably dealing network sockets without os support). file descriptors & network sockets operating system specific thing (or in posix standard). c standard library (from c99 or c11 standards) not know them. fileno(3) posix 2001 (and in posix 2008).

if coding in c++, might use framework libraries poco or qt (or perhaps boost) wrapping os services (e.g. linux, macosx, windows, android) , providing unified abstractions. if coding in c, consider glib and/or gio (from gnome/gtk).

btw, want multiplexing ability (like poll(2) syscall provides on linux & posix), , operating system specific. consider using event loop library, e.g. libev or libevent (or use event loop facilities qt, poco, gtk or glib)

notice c & c++ different languages, , both can exist on implementations without networking facilities (e.g. computers without network abilities, embedded systems, etc...).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -