activemq-cpp-3.4.0

src/main/decaf/net/ServerSocket.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef _DECAF_NET_SERVERSOCKET_H_
00018 #define _DECAF_NET_SERVERSOCKET_H_
00019 
00020 #include <decaf/util/Config.h>
00021 
00022 #include <decaf/net/InetAddress.h>
00023 #include <decaf/net/SocketImpl.h>
00024 #include <decaf/net/SocketImplFactory.h>
00025 
00026 #include <decaf/lang/exceptions/NullPointerException.h>
00027 #include <decaf/lang/exceptions/IllegalArgumentException.h>
00028 #include <decaf/net/UnknownHostException.h>
00029 #include <decaf/net/SocketTimeoutException.h>
00030 #include <decaf/io/IOException.h>
00031 
00032 #include <string>
00033 
00034 namespace decaf{
00035 namespace net{
00036 
00037     class Socket;
00038     class SocketImpl;
00039 
00050     class DECAF_API ServerSocket {
00051     private:
00052 
00053         // Factory for creating sockets, if not set a Plan TCP Socket is created
00054         static SocketImplFactory* factory;
00055 
00056         // The actual Socket that this Socket represents.
00057         mutable SocketImpl* impl;
00058         mutable volatile bool created;
00059 
00060         bool closed;
00061         bool bound;
00062 
00063         int backlog;
00064         int port;
00065 
00066     private:
00067 
00068         ServerSocket( const ServerSocket& );
00069         ServerSocket& operator= ( const ServerSocket& );
00070 
00071     public:
00072 
00076         ServerSocket();
00077 
00094         ServerSocket( int port );
00095 
00115         ServerSocket( int port, int backlog );
00116 
00139         ServerSocket( int port, int backlog, const InetAddress* address );
00140 
00144         virtual ~ServerSocket();
00145 
00146     protected:
00147 
00158         ServerSocket( SocketImpl* impl );
00159 
00160     public:
00161 
00175         virtual void bind( const std::string& host, int port );
00176 
00195         virtual void bind( const std::string& host, int port, int backlog );
00196 
00209         virtual Socket* accept();
00210 
00217         virtual void close();
00218 
00222         virtual bool isClosed() const;
00223 
00227         virtual bool isBound() const;
00228 
00237         virtual int getReceiveBufferSize() const;
00238 
00248         virtual void setReceiveBufferSize( int size );
00249 
00257         virtual bool getReuseAddress() const;
00258 
00267         virtual void setReuseAddress( bool reuse );
00268 
00276         virtual int getSoTimeout() const;
00277 
00288         virtual void setSoTimeout( int timeout );
00289 
00295         virtual int getLocalPort() const;
00296 
00300         virtual std::string toString() const;
00301 
00302     public:
00303 
00315         static void setSocketImplFactory( SocketImplFactory* factory );
00316 
00317     protected:
00318 
00328         virtual void implAccept( Socket* socket );
00329 
00335         virtual int getDefaultBacklog();
00336 
00337     protected:
00338 
00339         // Check for already closed and throw an error if so.
00340         void checkClosed() const;
00341 
00342         // Create but don't connect the underlying OS Socket instance, if not already created.
00343         void ensureCreated() const;
00344 
00345         // Binds and sets up the Listen for this Server Socket
00346         void setupSocketImpl( int port, int backlog, const InetAddress* ifAddress );
00347 
00348     };
00349 
00350 }}
00351 
00352 #endif // _DECAF_NET_SERVERSOCKETIMPL_H_
00353