lundi 1 juin 2015

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

Linux is also treated as a file, a network socket. but, Windows is not. and common files and network sockets treated as "FD". if the code should not rely on the operating system, how should write?

i think it like 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 {
// like 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 are omitted below ...

How can i implement it???! :(

Aucun commentaire:

Enregistrer un commentaire