[Nutshell-devel] Network Class Information : IoSelector and IoSelectorListener
Status: Planning
Brought to you by:
alexandream
From: Alexandre M. <ale...@gm...> - 2006-09-13 19:47:59
|
Ok, now we get into the controversial bits of the server structure: our network classes. First of all I must say that I didn't achieve a very good result with any model I tried, so I just decided to use this one, although a bit troubled: It is not exactly "high level" and it is possible that we will have to change a few things later when the project is bigger. The good part, though is that we can pretty much insulate this code from the rest of the application, since it runs in its own thread and talks to the rest of the server(s) through queues of messages. The "Socket" information is not higher level than the raw socket calls, and I will not talk about it right now: it will only be a wrapper to the file descriptor, associating socket functions with it as methods, dealing with a little higher level structures (ByteArrays instead of char* as an example) and doing error handling through exceptions. That being said, most of the interfaces and basic classes of the system, will deal mostly with the 32 bit integer used as file descriptor for a socket. At last, I do not have the names and descriptions of all the exceptions I planned on using here at hand right now, so I will just say "throws an exception" where it is due... There we go: enum IoSelectionType { IO_SELECTOR_READ, IO_SELECTOR_WRITE, IO_SELECTOR_EXCEPTION }; IoSelector public: IoSelector( IoSelectorListener& listener ); void register( int32 ioChannel, IoSelectionType ); //throws an exception void unregister( in32 ioChannel, IoSelectionType ); //throws an exception void operator()(void) // used when it is run as a thread. void stop() // used when it is run as a thread. private: IoSelectorListener& listener; fd_set fdReadSet; fd_set fdWriteSet; fd_set fdExceptionSet; // it should have a Mutex here, but I can't remember how the mutexes are // expressed in boost::thread. the public methods should acquire the lock // while changing the sets. IoSelectorListener // this is an interface public: virtual void onReadReady( IoSelector& selector, int32 ioChannel ) = 0; virtual void onWriteReady( IoSelector& selector, int32 ioChannel ) = 0; virtual void onExceptionReady( IoSelector& selector, int32 ioChannel ) = 0; ... As you can see, this is nothing really different from the select() system call, except that the types are wrapped in pretty CamelCaseNamed things and it works sorta like an event-driven system. Some exception generation will go in those classes and it is already prepared to run looping this select() forever in a separate thread. Nice... but no high-level at all. What can I say... I always liked the way select() works. Perhaps we will have to change select() for poll (or epoll where it is supported) and then the name Selector will not be exactly intuitive, but... I tried a lot of names and none of them felt right... whatever. Until the next bit, folks... see ya, Alexandre Moreira. |