Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / remoting / impl / SocketListener.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * SocketListener.h
19  *
20  * Berkeley Socket-based ListenerService implementation
21  */
22
23 #ifndef __shibsp_socklisten_h__
24 #define __shibsp_socklisten_h__
25
26 #ifndef FD_SETSIZE
27 # define FD_SETSIZE 1024
28 #endif
29
30 #include <shibsp/remoting/ListenerService.h>
31
32 #include <xercesc/dom/DOM.hpp>
33 #include <xmltooling/logging.h>
34 #include <xmltooling/util/Threads.h>
35
36 #ifdef WIN32
37 # include <winsock.h>
38 #endif
39
40 namespace shibsp {
41
42     class SocketPool;
43     class ServerThread;
44
45     /**
46      * Berkeley Socket-based ListenerService implementation
47      */
48     class SocketListener : public virtual ListenerService
49     {
50     public:
51         /// @cond OFF
52         SocketListener(const xercesc::DOMElement* e);
53         ~SocketListener();
54
55         DDF send(const DDF& in);
56
57         bool init(bool force);
58         bool run(bool* shutdown);
59         void term();
60
61         // Implemented by socket-specific subclasses.
62 #ifdef WIN32
63         typedef SOCKET ShibSocket;
64 #else
65         typedef int ShibSocket;
66 #endif
67         virtual bool create(ShibSocket& s) const=0;
68         virtual bool connect(ShibSocket& s) const=0;
69         virtual bool bind(ShibSocket& s, bool force=false) const=0;
70         virtual bool accept(ShibSocket& listener, ShibSocket& s) const=0;
71         virtual bool close(ShibSocket& s) const=0;
72         virtual int send(ShibSocket& s, const char* buf, int len) const=0;
73         virtual int recv(ShibSocket& s, char* buf, int buflen) const=0;
74
75         bool m_catchAll;
76     protected:
77         bool log_error() const; // for OS-level errors
78         xmltooling::logging::Category* log;
79         /// @endcond
80
81     private:
82         mutable SocketPool* m_socketpool;
83         bool* m_shutdown;
84
85         // Manage child threads
86         friend class ServerThread;
87         std::map<ShibSocket,xmltooling::Thread*> m_children;
88         xmltooling::Mutex* m_child_lock;
89         xmltooling::CondWait* m_child_wait;
90
91         // Primary socket
92         ShibSocket m_socket;
93     };
94 }
95
96 #endif /* __shibsp_socklisten_h__ */