0

I have a problem using sigaction in C++ with this class:

class SerialHandler{
private:
...
             
    /* Action Handling */
    struct sigaction receive_handler;
    /* Called using the receive_handler*/
    static void receive_function(int status);
    /* Get byte from the Serial Port */
    unsigned char receive_byte();
    /* Buffer for Singalinterrupt on Reciving */
    std::vector<char> in_char_buffer;
    ...
};

I really need to use an Interrupt and really need to use a member function (receive_function) on the Interrupt/Sigaction, because I need access to a member variable (vector in_char_buffer). Problem: You can't use member function using sigaction because you have to pass a normal function to the sigaction.sa_handler.

As you can see using a static function isn't an alternative either, because you would not be able to get access to the member variable. Also a wrapper would not help. You would access to an new object but not to the specific one.

So is there an alternative that can handle this problem?

4

1 回答 1

1

How about something like this:

#include <signal.h>
#include <string.h>

#include <vector>

using namespace std;

class SerialHandler;

// A Singleton class
class SerialHandlerManager
{
private:
    SerialHandlerManager() {};

public:
    static SerialHandlerManager* get_instance();
    void register_handler( int signum, int fd, SerialHandler * handler );

private:
    static void dispatch_signal( int signum, siginfo_t * info, void * context );

private:
    static SerialHandlerManager* the_manager;
    vector<SerialHandler *> handler_objects;
};

class SerialHandler
{
private:
    friend class SerialHandlerManager;
    void install_signal_handler( int signum )
    {
        (SerialHandlerManager::get_instance())->register_handler( signum, fd, this );
    }

    void receive_function( int status );

    int fd;     // file descriptor returned from opening the serial port
};

SerialHandlerManager* SerialHandlerManager::the_manager;

SerialHandlerManager* SerialHandlerManager::get_instance()
{
    if( !the_manager )
        the_manager = new SerialHandlerManager;
    return the_manager;
}

void SerialHandlerManager::register_handler( int signum, int fd, SerialHandler * handler )
{
    struct sigaction act;

    memset( &act, 0, sizeof(struct sigaction));

    act.sa_flags = SA_SIGINFO;
    act.sa_sigaction = SerialHandlerManager::dispatch_signal;

    handler_objects[fd] = handler;
    sigaction( signum, &act, NULL );

}

void SerialHandlerManager::dispatch_signal( int signum, siginfo_t * info, void * context )
{
    (get_instance()->handler_objects[info->si_fd])->receive_function( signum );
}

Of course you can use a map to link file descriptors to objects. This is by no means to be comprehensive (or bulletproof) but just a quick thought I had whilst reading your question.

Good luck

Alwin

于 2013-11-15T11:14:10.833 回答