activemq-cpp-3.8.2
Socket.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef _DECAF_NET_SOCKET_H_
18 #define _DECAF_NET_SOCKET_H_
19 
20 #include <decaf/net/InetAddress.h>
23 #include <decaf/io/InputStream.h>
24 #include <decaf/io/OutputStream.h>
25 #include <decaf/io/Closeable.h>
26 #include <decaf/util/Config.h>
27 
32 #include <decaf/io/IOException.h>
33 
34 namespace decaf{
35 namespace net{
36 
37  class SocketImpl;
38  class ServerSocket;
39 
45  protected:
46 
47  // The actual Socket that this Socket represents.
48  mutable SocketImpl* impl;
49 
50  private:
51 
52  // Factory for creating sockets, if not set a Plan TCP Socket is created
53  static SocketImplFactory* factory;
54 
55  mutable volatile bool created;
56 
57  bool connected;
58  bool closed;
59  bool bound;
60  bool inputShutdown;
61  bool outputShutdown;
62 
63  friend class ServerSocket;
64 
65  private:
66 
67  Socket( const Socket& );
68  Socket& operator= ( const Socket& );
69 
70  public:
71 
76  Socket();
77 
88  Socket( SocketImpl* impl );
89 
107  Socket( const InetAddress* address, int port );
108 
129  Socket( const InetAddress* address, int port, const InetAddress* localAddress, int localPort );
130 
147  Socket( const std::string& host, int port );
148 
169  Socket( const std::string& host, int port, const InetAddress* localAddress, int localPort );
170 
171  virtual ~Socket();
172 
187  virtual void bind( const std::string& ipaddress, int port );
188 
195  virtual void close();
196 
208  virtual void connect( const std::string& host, int port );
209 
227  virtual void connect( const std::string& host, int port, int timeout );
228 
234  bool isConnected() const {
235  return connected;
236  }
237 
241  bool isClosed() const {
242  return closed;
243  }
244 
248  bool isBound() const {
249  return bound;
250  }
251 
255  bool isInputShutdown() const {
256  return inputShutdown;
257  }
258 
262  bool isOutputShutdown() const {
263  return outputShutdown;
264  }
265 
281  virtual decaf::io::InputStream* getInputStream();
282 
294  virtual decaf::io::OutputStream* getOutputStream();
295 
301  int getPort() const;
302 
308  int getLocalPort() const;
309 
315  std::string getInetAddress() const;
316 
322  std::string getLocalAddress() const;
323 
330  virtual void shutdownInput();
331 
338  virtual void shutdownOutput();
339 
348  virtual int getSoLinger() const;
349 
362  virtual void setSoLinger( bool state, int timeout );
363 
371  virtual bool getKeepAlive() const;
372 
381  virtual void setKeepAlive( bool keepAlive );
382 
391  virtual int getReceiveBufferSize() const;
392 
402  virtual void setReceiveBufferSize( int size );
403 
411  virtual bool getReuseAddress() const;
412 
421  virtual void setReuseAddress( bool reuse );
422 
431  virtual int getSendBufferSize() const;
432 
443  virtual void setSendBufferSize( int size );
444 
452  virtual int getSoTimeout() const;
453 
464  virtual void setSoTimeout( int timeout );
465 
473  virtual bool getTcpNoDelay() const;
474 
484  virtual void setTcpNoDelay( bool value );
485 
497  virtual int getTrafficClass() const;
498 
512  virtual void setTrafficClass( int value );
513 
521  virtual bool getOOBInline() const;
522 
531  virtual void setOOBInline( bool value );
532 
541  virtual void sendUrgentData( int data );
542 
546  virtual std::string toString() const;
547 
548  public:
549 
561  static void setSocketImplFactory( SocketImplFactory* factory );
562 
563  protected:
564 
565  // Flags the Socket instance as created, bound and connected when its connected
566  // via a ServerSocket accept call.
567  void accepted();
568 
569  // Sets up a connected socket for the constructors that take connection arguments.
570  void initSocketImpl( const std::string& address, int port, const InetAddress* localAddress, int localPort );
571 
572  // Check for already closed and throw an error if so.
573  void checkClosed() const;
574 
575  // Create but don't connect the underlying OS Socket instance, if not already created.
576  void ensureCreated() const;
577 
578  };
579 
580 }}
581 
582 #endif /*_DECAF_NET_SOCKET_H_*/
bool isConnected() const
Indicates whether or not this socket is connected to am end point.
Definition: Socket.h:234
bool isOutputShutdown() const
Definition: Socket.h:262
bool isInputShutdown() const
Definition: Socket.h:255
bool isBound() const
Definition: Socket.h:248
bool isClosed() const
Definition: Socket.h:241
Factory class interface for a Factory that creates ScoketImpl objects.
Definition: SocketImplFactory.h:38
A base class that must be implemented by all classes wishing to provide a class that reads in a strea...
Definition: InputStream.h:39
Definition: Socket.h:44
Represents an IP address.
Definition: InetAddress.h:33
Interface for a class that implements the close method.
Definition: Closeable.h:30
Acts as a base class for all physical Socket implementations.
Definition: SocketImpl.h:42
SocketImpl * impl
Definition: Socket.h:48
#define DECAF_API
Definition: Config.h:29
This class implements server sockets.
Definition: ServerSocket.h:50
Base interface for any class that wants to represent an output stream of bytes.
Definition: OutputStream.h:39
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25