4544e36bc2da56148fde234da640f528c8040618
[shibboleth/sp.git] / shibsp / remoting / impl / SocketListener.h
1 /*
2  *  Copyright 2001-2007 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         bool run(bool* shutdown);
57
58         // Implemented by socket-specific subclasses.
59 #ifdef WIN32
60         typedef SOCKET ShibSocket;
61 #else
62         typedef int ShibSocket;
63 #endif
64         virtual bool create(ShibSocket& s) const=0;
65         virtual bool connect(ShibSocket& s) const=0;
66         virtual bool bind(ShibSocket& s, bool force=false) const=0;
67         virtual bool accept(ShibSocket& listener, ShibSocket& s) const=0;
68         virtual bool close(ShibSocket& s) const=0;
69         virtual int send(ShibSocket& s, const char* buf, int len) const=0;
70         virtual int recv(ShibSocket& s, char* buf, int buflen) const=0;
71
72     protected:
73         bool log_error() const; // for OS-level errors
74         xmltooling::logging::Category* log;
75         /// @endcond
76     
77     private:
78         mutable SocketPool* m_socketpool;
79         bool* m_shutdown;
80
81         // Manage child threads
82         friend class ServerThread;
83         std::map<ShibSocket,xmltooling::Thread*> m_children;
84         xmltooling::Mutex* m_child_lock;
85         xmltooling::CondWait* m_child_wait;
86
87         // Primary socket
88         ShibSocket m_socket;
89     };
90 }
91
92 #endif /* __shibsp_socklisten_h__ */