bbcde89b315f547a46461e471e8a978ef0228421
[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 <xercesc/dom/DOM.hpp>
37 #include <xmltooling/logging.h>
38 #include <xmltooling/util/Threads.h>
39
40 #ifdef WIN32
41 # include <winsock.h>
42 #endif
43
44 namespace shibsp {
45
46     class SocketPool;
47     class ServerThread;
48
49     /**
50      * Berkeley Socket-based ListenerService implementation
51      */
52     class SocketListener : public virtual ListenerService
53     {
54     public:
55         /// @cond OFF
56         SocketListener(const xercesc::DOMElement* e);
57         ~SocketListener();
58
59         DDF send(const DDF& in);
60
61         bool init(bool force);
62         bool run(bool* shutdown);
63         void term();
64
65         // Implemented by socket-specific subclasses.
66 #ifdef WIN32
67         typedef SOCKET ShibSocket;
68 #else
69         typedef int ShibSocket;
70 #endif
71         virtual bool create(ShibSocket& s) const=0;
72         virtual bool connect(ShibSocket& s) const=0;
73         virtual bool bind(ShibSocket& s, bool force=false) const=0;
74         virtual bool accept(ShibSocket& listener, ShibSocket& s) const=0;
75         virtual bool close(ShibSocket& s) const=0;
76         virtual int send(ShibSocket& s, const char* buf, int len) const=0;
77         virtual int recv(ShibSocket& s, char* buf, int buflen) const=0;
78
79         bool m_catchAll;
80     protected:
81         bool log_error(const char* fn=nullptr) const; // for OS-level errors
82         xmltooling::logging::Category* log;
83         /// @endcond
84
85     private:
86         mutable SocketPool* m_socketpool;
87         bool* m_shutdown;
88
89         // Manage child threads
90         friend class ServerThread;
91         std::map<ShibSocket,xmltooling::Thread*> m_children;
92         xmltooling::Mutex* m_child_lock;
93         xmltooling::CondWait* m_child_wait;
94
95         unsigned int m_stackSize;
96
97         // Primary socket
98         ShibSocket m_socket;
99     };
100 }
101
102 #endif /* __shibsp_socklisten_h__ */