|
activemq-cpp-3.3.0
|
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_SOCKET_H_ 00018 #define _DECAF_NET_SOCKET_H_ 00019 00020 #include <decaf/net/InetAddress.h> 00021 #include <decaf/net/SocketImplFactory.h> 00022 #include <decaf/net/SocketException.h> 00023 #include <decaf/io/InputStream.h> 00024 #include <decaf/io/OutputStream.h> 00025 #include <decaf/io/Closeable.h> 00026 #include <decaf/util/Config.h> 00027 00028 #include <decaf/lang/exceptions/NullPointerException.h> 00029 #include <decaf/lang/exceptions/IllegalArgumentException.h> 00030 #include <decaf/net/UnknownHostException.h> 00031 #include <decaf/net/SocketTimeoutException.h> 00032 #include <decaf/io/IOException.h> 00033 00034 namespace decaf{ 00035 namespace net{ 00036 00037 class SocketImpl; 00038 class ServerSocket; 00039 00044 class DECAF_API Socket : public decaf::io::Closeable { 00045 protected: 00046 00047 // The actual Socket that this Socket represents. 00048 mutable SocketImpl* impl; 00049 00050 private: 00051 00052 // Factory for creating sockets, if not set a Plan TCP Socket is created 00053 static SocketImplFactory* factory; 00054 00055 mutable volatile bool created; 00056 00057 bool connected; 00058 bool closed; 00059 bool bound; 00060 bool inputShutdown; 00061 bool outputShutdown; 00062 00063 friend class ServerSocket; 00064 00065 private: 00066 00067 Socket( const Socket& ); 00068 Socket& operator= ( const Socket& ); 00069 00070 public: 00071 00076 Socket(); 00077 00088 Socket( SocketImpl* impl ); 00089 00107 Socket( const InetAddress* address, int port ); 00108 00129 Socket( const InetAddress* address, int port, const InetAddress* localAddress, int localPort ); 00130 00147 Socket( const std::string& host, int port ); 00148 00169 Socket( const std::string& host, int port, const InetAddress* localAddress, int localPort ); 00170 00171 virtual ~Socket(); 00172 00187 virtual void bind( const std::string& ipaddress, int port ); 00188 00195 virtual void close(); 00196 00208 virtual void connect( const std::string& host, int port ); 00209 00227 virtual void connect( const std::string& host, int port, int timeout ); 00228 00234 bool isConnected() const { 00235 return connected; 00236 } 00237 00241 bool isClosed() const { 00242 return closed; 00243 } 00244 00248 bool isBound() const { 00249 return bound; 00250 } 00251 00255 bool isInputShutdown() const { 00256 return inputShutdown; 00257 } 00258 00262 bool isOutputShutdown() const { 00263 return outputShutdown; 00264 } 00265 00281 virtual decaf::io::InputStream* getInputStream(); 00282 00294 virtual decaf::io::OutputStream* getOutputStream(); 00295 00301 int getPort() const; 00302 00308 int getLocalPort() const; 00309 00315 std::string getInetAddress() const; 00316 00322 std::string getLocalAddress() const; 00323 00330 virtual void shutdownInput(); 00331 00338 virtual void shutdownOutput(); 00339 00348 virtual int getSoLinger() const; 00349 00362 virtual void setSoLinger( bool state, int timeout ); 00363 00371 virtual bool getKeepAlive() const; 00372 00381 virtual void setKeepAlive( bool keepAlive ); 00382 00391 virtual int getReceiveBufferSize() const; 00392 00402 virtual void setReceiveBufferSize( int size ); 00403 00411 virtual bool getReuseAddress() const; 00412 00421 virtual void setReuseAddress( bool reuse ); 00422 00431 virtual int getSendBufferSize() const; 00432 00443 virtual void setSendBufferSize( int size ); 00444 00452 virtual int getSoTimeout() const; 00453 00464 virtual void setSoTimeout( int timeout ); 00465 00473 virtual bool getTcpNoDelay() const; 00474 00484 virtual void setTcpNoDelay( bool value ); 00485 00497 virtual int getTrafficClass() const; 00498 00512 virtual void setTrafficClass( int value ); 00513 00521 virtual bool getOOBInline() const; 00522 00531 virtual void setOOBInline( bool value ); 00532 00541 virtual void sendUrgentData( int data ); 00542 00546 virtual std::string toString() const; 00547 00548 public: 00549 00561 static void setSocketImplFactory( SocketImplFactory* factory ); 00562 00563 protected: 00564 00565 // Flags the Socket instance as created, bound and connected when its connected 00566 // via a ServerSocket accept call. 00567 void accepted(); 00568 00569 // Sets up a connected socket for the constructors that take connection arguments. 00570 void initSocketImpl( const std::string& address, int port, const InetAddress* localAddress, int localPort ); 00571 00572 // Check for already closed and throw an error if so. 00573 void checkClosed() const; 00574 00575 // Create but don't connect the underlying OS Socket instance, if not already created. 00576 void ensureCreated() const; 00577 00578 }; 00579 00580 }} 00581 00582 #endif /*_DECAF_NET_SOCKET_H_*/