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