summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/SelectorThread.h
blob: 52f4a75a9ae4239b86e7584f6a73e9fd45d3467c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_SELECTOR_THREAD_H
#define ICE_SELECTOR_THREAD_H

#include <IceUtil/Shared.h>
#include <IceUtil/Handle.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/Monitor.h>
#include <IceUtil/Thread.h>
#include <IceUtil/Timer.h>

#include <Ice/Config.h>
#include <Ice/SelectorThreadF.h>
#include <Ice/InstanceF.h>
#include <Ice/Selector.h>

#if defined(_WIN32)
#   include <winsock2.h>
#else
#   define SOCKET int
#   if defined(ICE_USE_EPOLL)
#       include <sys/epoll.h>
#   elif defined(__APPLE__)
#       include <sys/event.h>
#   else
#       include <sys/poll.h>
#   endif
#endif

#include <deque>

namespace IceInternal
{

class SocketReadyCallback : public IceUtil::TimerTask
{
public:
    
    //
    // The selector thread unregisters the callback when socketReady returns SocketStatus.Finished.
    //
    virtual SocketStatus socketReady(bool) = 0;

    //
    // The selector thread doesn't unregister the callback when sockectTimeout is called; socketTimeout
    // must unregister the callback either explicitly with unregister() or by shutting down the socket 
    // (if necessary).
    //
    virtual void socketTimeout() = 0;

private:

    void
    runTimerTask()
    {
        this->socketTimeout();
    }
    
    friend class SelectorThread;
};
typedef IceUtil::Handle<SocketReadyCallback> SocketReadyCallbackPtr;

class SelectorThread : public IceUtil::Shared, public IceUtil::Monitor<IceUtil::Mutex>
{
public:

    SelectorThread(const InstancePtr&);
    virtual ~SelectorThread();

    void destroy();

    void incFdsInUse();
    void decFdsInUse();

    void _register(SOCKET, const SocketReadyCallbackPtr&, SocketStatus status, int timeout);
    void unregister(SOCKET);
    void joinWithThread();

private:

    void run();

    struct SocketInfo
    {
        SOCKET fd;
        SocketReadyCallbackPtr cb;
        SocketStatus status;
        int timeout;
        
        SocketInfo(SOCKET fd, const SocketReadyCallbackPtr& cb, SocketStatus status, int timeout)
        {
            this->fd = fd;
            this->cb = cb;
            this->status = status;
            this->timeout = timeout;
        }
    };

    class HelperThread : public IceUtil::Thread
    {
    public:
        
        HelperThread(const SelectorThreadPtr&);
        virtual void run();

    private:

        SelectorThreadPtr _selectorThread;
    };
    friend class HelperThread;

    InstancePtr _instance;
    bool _destroyed;

    SOCKET _maxFd;
    SOCKET _minFd;
    SOCKET _fdIntrRead;
    SOCKET _fdIntrWrite;
    Selector _selector;

    std::deque<SocketInfo> _changes;

    IceUtil::ThreadPtr _thread;
    IceUtil::TimerPtr _timer;
};

}

#endif