SOL9 2.0 Class: SocketSelector

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * SocketSelector.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000

// 1999.08.04
#pragma once

#include <sol\Socket.h>

namespace SOL {

class SocketSelector :public Object {
private:
    fd_set  readFDSet;
    fd_set  writeFDSet;
    fd_set  exceptionFDSet;
    timeval timeout;

public:
    SocketSelector() 
    {
        FD_ZERO(&readFDSet);
        FD_ZERO(&writeFDSet);
        FD_ZERO(&exceptionFDSet);
        memset(&timeout, 0, sizeof(timeout));
        timeout.tv_usec = 100;
    }

public:
    void setTimeout(int sec, int usec ) 
    {
        timeout.tv_sec = sec;
        timeout.tv_usec = usec;
    }

public:
    void    clearAll()
    {
        FD_ZERO(&readFDSet);
        FD_ZERO(&writeFDSet);
        FD_ZERO(&exceptionFDSet);
    }

public:
    void    setReadable(Socket* soc) 
    {
        if (soc) {
            FD_SET(soc->getSocket(), &readFDSet);
        }
    }

public:

    void    setWritable(Socket* soc) 
    {
        if (soc) {
            FD_SET(soc->getSocket(), &writeFDSet);
        }
    }

public:
    void    setException(Socket* soc) 
    {
        if (soc) {
            FD_SET(soc->getSocket(), &exceptionFDSet);
        }
    }

public:
    int select() 
    {
        return ::select(FD_SETSIZE, &readFDSet, 
                &writeFDSet, &exceptionFDSet,  &timeout);
    }

public:
    int isReadable(Socket* soc) 
    {
        int rc = SOCKET_ERROR;
        if (soc) {
            rc = FD_ISSET(soc->getSocket(), &(this->readFDSet));
        }
        return rc;
    }

public:
    int isWritable(Socket* soc) 
    {
        int rc = SOCKET_ERROR;
        if (soc) {
            rc = FD_ISSET(soc ->getSocket(), &(this->writeFDSet));
        }
        return rc;
    }

public:
    int isException(Socket* soc) 
    {
        int rc = SOCKET_ERROR;
        if (soc) {
            rc = FD_ISSET(soc ->getSocket(), &(this->exceptionFDSet));
        }
        return rc;
    }

    ///////////////////////////////////
public:
    void    clearReadable() {
            FD_ZERO(&readFDSet);
    }

    void    clearWritable() {
            FD_ZERO(&writeFDSet);
    }

    void    clearException() {
            FD_ZERO(&exceptionFDSet);
    }
};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.