Merge up from branch.
[shibboleth/cpp-sp.git] / shib-target / SocketListener.h
1 /*
2  *  Copyright 2001-2006 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  * @file shib-target/SocketListener.h
19  * 
20  * Berkeley Socket-based Listener implementation
21  */
22
23 #ifndef __st_socklisten_h__
24 #define __st_socklisten_h__
25
26 #ifndef FD_SETSIZE
27 # define FD_SETSIZE 1024
28 #endif
29
30 #include "internal.h"
31
32 #include <winsock.h>
33
34 namespace shibtarget {
35
36     class SocketPool;
37     class ServerThread;
38     class SocketListener : public virtual IListener
39     {
40     public:
41         SocketListener(const DOMElement* e);
42         ~SocketListener();
43
44         DDF send(const DDF& in);
45         bool run(bool* shutdown);
46
47         // Implemented by socket-specific subclasses.
48 #ifdef WIN32
49         typedef SOCKET ShibSocket;
50 #else
51         typedef int ShibSocket;
52 #endif
53         virtual bool create(ShibSocket& s) const=0;
54         virtual bool connect(ShibSocket& s) const=0;
55         virtual bool bind(ShibSocket& s, bool force=false) const=0;
56         virtual bool accept(ShibSocket& listener, ShibSocket& s) const=0;
57         virtual bool close(ShibSocket& s) const=0;
58         virtual int send(ShibSocket& s, const char* buf, int len) const=0;
59         virtual int recv(ShibSocket& s, char* buf, int buflen) const=0;
60
61     protected:
62         bool log_error() const; // for OS-level errors
63         log4cpp::Category* log;
64     
65     private:
66         mutable SocketPool* m_socketpool;
67         bool* m_shutdown;
68
69         // Manage child threads
70         friend class ServerThread;
71         std::map<ShibSocket,shibboleth::Thread*> m_children;
72         shibboleth::Mutex* m_child_lock;
73         shibboleth::CondWait* m_child_wait;
74
75         // Primary socket
76         ShibSocket m_socket;
77     };
78 }
79
80 #endif /* __st_socklisten_h__ */