activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r532861 - /activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/
Date Thu, 26 Apr 2007 21:00:05 GMT
Author: tabish
Date: Thu Apr 26 14:00:04 2007
New Revision: 532861

URL: http://svn.apache.org/viewvc?view=rev&rev=532861
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103

Building Decaf lib

Added:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketException.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.cpp?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.cpp Thu Apr 26 14:00:04 2007
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BufferedSocket.h"
+
+#include <decaf/lang/IllegalArgumentException.h>
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+using namespace decaf::lang;
+
+////////////////////////////////////////////////////////////////////////////////
+BufferedSocket::BufferedSocket( Socket* socket,
+                                int inputBufferSize,
+                                int outputBufferSize,
+                                bool own ) :
+    socket(NULL),
+    own(false),
+    inputStream(NULL),
+    outputStream(NULL),
+    inputBufferSize(0),
+    outputBufferSize(0)
+{
+    if(inputBufferSize < 0 || outputBufferSize < 0 )
+    {
+        throw IllegalArgumentException(
+            __FILE__, __LINE__,
+            "BufferedSocket::BufferedSocket - buffer sizes must be >=0! "
+            "Given input buffer size: %d, Given output buffer size: %d",
+            inputBufferSize,
+            outputBufferSize );
+    }
+
+    if(socket == NULL)
+    {
+        throw IllegalArgumentException(
+            __FILE__, __LINE__,
+            "BufferedSocket::BufferedSocket - Constructed with NULL Socket");
+    }
+
+    this->socket = socket;
+    this->inputBufferSize = inputBufferSize;
+    this->outputBufferSize = outputBufferSize;
+    this->own = own;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+BufferedSocket::~BufferedSocket()
+{
+    try
+    {
+        close();
+    }
+    AMQ_CATCH_NOTHROW( ActiveMQException )
+    AMQ_CATCHALL_NOTHROW()
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void BufferedSocket::connect( const char* host, int port )
+    throw( SocketException )
+{
+    try
+    {
+        if( socket->isConnected() )
+        {
+            throw SocketException( __FILE__, __LINE__,
+                 "BufferedSocket::connect() - socket already connected" );
+        }
+
+        // Connect the socket.
+        socket->connect( host, port );
+
+        // Now create the buffered streams that wrap around the socket.
+        inputStream = new BufferedInputStream(
+            socket->getInputStream(), (std::size_t)inputBufferSize );
+        outputStream = new BufferedOutputStream(
+            socket->getOutputStream(), (std::size_t)outputBufferSize );
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCH_EXCEPTION_CONVERT( ActiveMQException, SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void BufferedSocket::close() throw( cms::CMSException )
+{
+    try
+    {
+        if( outputStream != NULL )
+        {
+            // Ensure all data is written
+            outputStream->flush();
+
+            delete outputStream;
+            outputStream = NULL;
+        }
+
+        if( inputStream != NULL ){
+            delete inputStream;
+            inputStream = NULL;
+        }
+
+        if( socket != NULL ){
+            // Close the socket
+            try{
+                socket->close();
+            } catch( cms::CMSException& ex ){ /* Absorb */ }
+
+            // if we own it, delete it.
+            if( own ) {
+                delete socket;
+            }
+            socket = NULL;
+        }
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCH_EXCEPTION_CONVERT( ActiveMQException, SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/BufferedSocket.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _DECAF_NET_BUFFEREDSOCKET_H_
+#define _DECAF_NET_BUFFEREDSOCKET_H_
+
+#include <decaf/net/Socket.h>
+#include <decaf/net/SocketException.h>
+#include <decaf/io/BufferedInputStream.h>
+#include <decaf/io/BufferedOutputStream.h>
+
+namespace decaf{
+namespace net{
+
+    /**
+     * Buffered Socket class that wraps a <code>Socket</code> derived
+     * object and provides Buffered input and Output Streams to improce
+     * the efficiency of the reads and writes.
+     */
+    class BufferedSocket : public Socket
+    {
+    private:
+
+        // Socket that this class wraps to provide buffering
+        Socket* socket;
+
+        // Indicates if the lifetime of the Socket is controlled by this
+        // class.  If true Socket is deleted at destruction.
+        bool own;
+
+        // Buffered Input stream to wrap the Socket input stream
+        io::BufferedInputStream* inputStream;
+
+        // Buffered Output stream to wrap the Socket input stream
+        io::BufferedOutputStream* outputStream;
+
+        // Sizes for the Buffered Streams
+        int inputBufferSize;
+        int outputBufferSize;
+
+    public:
+
+        /**
+         * Constructs a new Buffered socket object
+         * @param socket the socket to buffer
+         * @param inputBufferSize size of the input buffer
+         * @param outputBufferSize size of the output buffer
+         * @param own does this object own the passed socket
+         */
+        BufferedSocket( Socket* socket,
+                        int inputBufferSize = 1000,
+                        int outputBufferSize = 1000,
+                        bool own = true );
+
+        virtual ~BufferedSocket();
+
+        /**
+         * Connects to the specified destination. Closes this socket if
+         * connected to another destination.
+         * @param host The host of the server to connect to.
+         * @param port The port of the server to connect to.
+         * @throws IOException Thrown if a failure occurred in the connect.
+         */
+        virtual void connect( const char* host, int port )
+            throw( SocketException );
+
+        /**
+         * Closes this object and deallocates the appropriate resources.
+         * @throws CMSException
+         */
+        virtual void close() throw( cms::CMSException );
+
+        /**
+         * Indicates whether or not this socket is connected to a destination.
+         * @return true if connected
+         */
+        virtual bool isConnected() const{
+            if( socket == NULL ){
+                return false;
+            }
+
+            return socket->isConnected();
+        }
+
+        /**
+         * Gets the InputStream for this socket.
+         * @return The InputStream for this socket. NULL if not connected.
+         */
+        virtual io::InputStream* getInputStream(){
+            return inputStream;
+        }
+
+        /**
+         * Gets the OutputStream for this socket.
+         * @return the OutputStream for this socket.  NULL if not connected.
+         */
+        virtual io::OutputStream* getOutputStream(){
+            return outputStream;
+        }
+
+        /**
+         * Gets the linger time.
+         * @return The linger time in seconds.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getSoLinger() const throw( SocketException ){
+            checkSocket();
+            return socket->getSoLinger();
+        }
+
+        /**
+         * Sets the linger time.
+         * @param linger The linger time in seconds.  If 0, linger is off.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setSoLinger( int linger ) throw( SocketException ){
+            checkSocket();
+            socket->setSoLinger( linger );
+        }
+
+        /**
+         * Gets the keep alive flag.
+         * @return True if keep alive is enabled.
+         * @throws SocketException if the operation fails.
+         */
+        virtual bool getKeepAlive() const throw( SocketException ){
+            checkSocket();
+            return socket->getKeepAlive();
+        }
+
+        /**
+         * Enables/disables the keep alive flag.
+         * @param keepAlive If true, enables the flag.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setKeepAlive( bool keepAlive ) throw( SocketException ){
+            checkSocket();
+            socket->setKeepAlive( keepAlive );
+        }
+
+        /**
+         * Gets the receive buffer size.
+         * @return the receive buffer size in bytes.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getReceiveBufferSize() const throw( SocketException ){
+            checkSocket();
+            return socket->getReceiveBufferSize();
+        }
+
+        /**
+         * Sets the recieve buffer size.
+         * @param size Number of bytes to set the receive buffer to.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setReceiveBufferSize( int size ) throw( SocketException ){
+            checkSocket();
+            socket->setReceiveBufferSize( size );
+        }
+
+        /**
+         * Gets the reuse address flag.
+         * @return True if the address can be reused.
+         * @throws SocketException if the operation fails.
+         */
+        virtual bool getReuseAddress() const throw( SocketException ){
+            checkSocket();
+            return socket->getReuseAddress();
+        }
+
+        /**
+         * Sets the reuse address flag.
+         * @param reuse If true, sets the flag.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setReuseAddress( bool reuse ) throw( SocketException ){
+            checkSocket();
+            socket->setReuseAddress( reuse );
+        }
+
+        /**
+         * Gets the send buffer size.
+         * @return the size in bytes of the send buffer.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getSendBufferSize() const throw( SocketException ){
+            checkSocket();
+            return socket->getSendBufferSize();
+        }
+
+        /**
+         * Sets the send buffer size.
+         * @param size The number of bytes to set the send buffer to.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setSendBufferSize( int size ) throw( SocketException ){
+            checkSocket();
+            socket->setSendBufferSize( size );
+        }
+
+        /**
+         * Gets the timeout for socket operations.
+         * @return The timeout in milliseconds for socket operations.
+         * @throws SocketException Thrown if unable to retrieve the information.
+         */
+        virtual int getSoTimeout() const throw( SocketException ){
+            checkSocket();
+            return socket->getSoTimeout();
+        }
+
+        /**
+         * Sets the timeout for socket operations.
+         * @param timeout The timeout in milliseconds for socket operations.<p>
+         * @throws SocketException Thrown if unable to set the information.
+         */
+        virtual void setSoTimeout( int timeout ) throw( SocketException ){
+            checkSocket();
+            socket->setSoTimeout( timeout );
+        }
+
+    private:
+
+        void checkSocket() const throw ( SocketException ) {
+            if( socket == NULL ) {
+                throw SocketException( __FILE__, __LINE__, "socket is NULL" );
+            }
+        }
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_BUFFEREDSOCKET_H_*/

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp Thu Apr 26 14:00:04 2007
@@ -0,0 +1,221 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ServerSocket.h"
+#include "SocketError.h"
+
+#ifdef HAVE_WINSOCK2_H
+    #include <Winsock2.h>
+    #include <Ws2tcpip.h>
+    #include <sys/stat.h>
+    #define stat _stat
+#else
+    #include <unistd.h>
+    #include <netdb.h>
+    #include <fcntl.h>
+    #include <sys/file.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+    #include <string.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <assert.h>
+#include <string>
+
+using namespace decaf::net;
+
+#ifdef HAVE_WINSOCK2_H
+
+    // Static socket initializer needed for winsock
+
+    ServerSocket::StaticServerSocketInitializer::StaticServerSocketInitializer () {
+        socketInitError = NULL;
+        const WORD version_needed = MAKEWORD(2,2); // lo-order byte: major version
+        WSAData temp;
+        if( WSAStartup(version_needed, &temp )){
+           clear();
+               socketInitError = new SocketException ( __FILE__, __LINE__,
+                   "winsock.dll was not found");
+        }
+    }
+    ServerSocket::StaticServerSocketInitializer::~StaticServerSocketInitializer () {
+        clear();
+        WSACleanup();
+    }
+
+    // Create static instance of the socket initializer.
+    ServerSocket::StaticServerSocketInitializer
+        ServerSocket::staticSocketInitializer;
+
+#endif
+
+
+////////////////////////////////////////////////////////////////////////////////
+ServerSocket::ServerSocket()
+{
+    socketHandle = Socket::INVALID_SOCKET_HANDLE;
+
+#if defined(HAVE_WINSOCK2_H)
+    if( ServerSocket::staticSocketInitializer.getSocketInitError() != NULL ) {
+        throw *ServerSocket::staticSocketInitializer.getSocketInitError();
+    }
+#endif
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ServerSocket::~ServerSocket()
+{
+    // No shutdown, just close - dont want blocking destructor.
+    close();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ServerSocket::bind( const char* host, int port ) throw ( SocketException )
+{
+    bind (host, port, SOMAXCONN);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ServerSocket::bind( const char* host,
+                         int port,
+                         int backlog ) throw ( SocketException )
+{
+    if(isBound()) {
+        throw SocketException ( __FILE__, __LINE__,
+            "ServerSocket::bind - Socket already bound" );
+    }
+
+    // Create the socket.
+    socketHandle = ::socket(AF_INET, SOCK_STREAM, 0 );
+    if( socketHandle < 0) {
+        socketHandle = Socket::INVALID_SOCKET_HANDLE;
+            throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str());
+    }
+
+    // Verify the port value.
+    if( port <= 0 || port > 65535 ) {
+        throw SocketException( __FILE__, __LINE__,
+            "ServerSocket::bind - Port out of range: %d", port );
+    }
+
+    sockaddr_in bind_addr;
+    bind_addr.sin_family = AF_INET;
+    bind_addr.sin_port = htons((short)port);
+    bind_addr.sin_addr.s_addr = 0; // To be set later down...
+    memset(&bind_addr.sin_zero, 0, sizeof(bind_addr.sin_zero));
+    int status;
+
+    // Resolve name
+#if defined(HAVE_STRUCT_ADDRINFO)
+    ::addrinfo hints;
+    memset(&hints, 0, sizeof(addrinfo));
+    hints.ai_family = PF_INET;
+    struct addrinfo *res_ptr = NULL;
+    status = ::getaddrinfo(host, NULL, &hints, &res_ptr);
+    if( status != 0 || res_ptr == NULL) {
+        throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
+    }
+    assert(res_ptr->ai_addr->sa_family == AF_INET);
+    // Porting: On both 32bit and 64 bit systems that we compile to soo far, sin_addr is a 32 bit value, not an unsigned long.
+    assert(sizeof(((sockaddr_in*)res_ptr->ai_addr)->sin_addr.s_addr) == 4);
+    bind_addr.sin_addr.s_addr = ((sockaddr_in*)res_ptr->ai_addr)->sin_addr.s_addr;
+    freeaddrinfo(res_ptr);
+#else
+    struct ::hostent *he = ::gethostbyname(host);
+    if( he == NULL ) {
+        throw SocketException( __FILE__, __LINE__, "Failed to resolve hostname" );
+    }
+    bind_addr.sin_addr.s_addr = *((in_addr_t *)he->h_addr);
+#endif
+
+
+    // Set the socket to reuse the address.
+    int value = 1;
+    ::setsockopt(socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) );
+
+    status = ::bind(socketHandle,
+             reinterpret_cast<sockaddr*>(&bind_addr), sizeof( bind_addr ));
+
+    if( status < 0 ){
+        close();
+        throw SocketException ( __FILE__, __LINE__,
+            "ServerSocket::bind - %s", SocketError::getErrorString().c_str() );
+    }
+    status = ::listen( socketHandle, (int)backlog );
+    if( status < 0 ) {
+        close();
+        throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ServerSocket::close() throw (cms::CMSException){
+
+    if( isBound() ) {
+
+        #if !defined(HAVE_WINSOCK2_H)
+            ::close( socketHandle );
+        #else
+            ::closesocket( socketHandle );
+        #endif
+
+        socketHandle = Socket::INVALID_SOCKET_HANDLE;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool ServerSocket::isBound() const {
+    return this->socketHandle != Socket::INVALID_SOCKET_HANDLE;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Socket* ServerSocket::accept() throw (SocketException)
+{
+    struct sockaddr_in temp;
+
+    #if !defined(HAVE_WINSOCK2_H)
+        socklen_t temp_len = sizeof( sockaddr_in );
+    #else
+        int temp_len = sizeof( sockaddr_in );
+    #endif
+
+    SocketHandle ss_socket_handle = NULL;
+
+    // Loop to ignore any signal interruptions that occur during the operation.
+    do {
+
+        ss_socket_handle = ::accept( socketHandle,
+                                     reinterpret_cast<struct sockaddr*>(&temp),
+                                     &temp_len );
+
+    } while( ss_socket_handle < 0 &&
+             SocketError::getErrorCode() == SocketError::INTERRUPTED );
+
+    if( ss_socket_handle < 0 ) {
+        throw SocketException( __FILE__, __LINE__,
+            "ServerSocket::accept- %s", SocketError::getErrorString().c_str() );
+    }
+
+    return new TcpSocket( ss_socket_handle );
+}
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _DECAF_NET_SERVERSOCKETIMPL_H_
+#define _DECAF_NET_SERVERSOCKETIMPL_H_
+
+#include <decaf/net/TcpSocket.h>
+#include <decaf/net/SocketException.h>
+#include <decaf/util/Config.h>
+
+namespace decaf{
+namespace net{
+
+    /**
+     * A server socket class (for testing purposes).
+     */
+    class ServerSocket
+    {
+    public:
+
+        typedef Socket::SocketHandle SocketHandle;
+
+    private:
+
+        SocketHandle socketHandle;
+
+    public:
+
+        /**
+         * Constructor.
+         * Creates a non-bound server socket.
+         */
+        ServerSocket();
+
+        /**
+         * Destructor.
+         * Releases socket handle if close() hasn't been called.
+         */
+        virtual ~ServerSocket();
+
+    public:
+
+        /**
+         * Bind and listen to given IP/dns and port.
+         * @param host IP address or host name.
+         * @param port TCP port between 1..655535
+         */
+        virtual void bind( const char* host, int port ) throw ( SocketException );
+
+        /**
+         * Bind and listen to given IP/dns and port.
+         * @param host IP address or host name.
+         * @param port TCP port between 1..655535
+         * @param backlog Size of listen backlog.
+         */
+        virtual void bind( const char* host,
+                           int port,
+                           int backlog ) throw ( SocketException );
+
+        /**
+         * Blocks until a client connects to the bound socket.
+         * @return new socket. Never returns NULL.
+         */
+        virtual Socket* accept () throw ( SocketException );
+
+        /**
+         * Closes the server socket.
+         */
+        virtual void close() throw( cms::CMSException );
+
+        /**
+         * @return true of the server socket is bound.
+         */
+        virtual bool isBound() const;
+
+   protected:
+
+      #ifdef HAVE_WINSOCK2_H
+
+          // WINDOWS needs initialization of winsock
+          class StaticServerSocketInitializer {
+          private:
+
+              SocketException* socketInitError;
+
+              void clear(){
+                  if( socketInitError != NULL ){
+                      delete socketInitError;
+                  }
+                  socketInitError = NULL;
+              }
+
+          public:
+
+              SocketException* getSocketInitError() {
+                  return socketInitError;
+              }
+              StaticServerSocketInitializer();
+              virtual ~StaticServerSocketInitializer();
+
+          };
+          static StaticServerSocketInitializer staticSocketInitializer;
+      #endif
+
+   };
+
+}}
+
+#endif // _DECAF_NET_SERVERSOCKETIMPL_H_
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _DECAF_NET_SOCKET_H_
+#define _DECAF_NET_SOCKET_H_
+
+#include <decaf/net/SocketException.h>
+#include <decaf/io/InputStream.h>
+#include <decaf/io/OutputStream.h>
+#include <decaf/io/Closeable.h>
+#include <decaf/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+#include <Winsock2.h> // SOCKET
+#endif
+
+namespace decaf{
+namespace net{
+
+    class Socket : public decaf::io::Closeable
+    {
+    public:
+
+        // Define the SocketHandle type.
+        #if !defined(HAVE_WINSOCK2_H)
+            typedef int SocketHandle;
+        #else
+            typedef SOCKET SocketHandle;
+        #endif
+
+        /**
+         * Defines a constant for an invalid socket handle.
+         */
+        static const SocketHandle INVALID_SOCKET_HANDLE = (SocketHandle) -1;
+
+    public:
+
+        virtual ~Socket() {}
+
+        /**
+         * Connects to the specified destination. Closes this socket if
+         * connected to another destination.
+         * @param host The host of the server to connect to.
+         * @param port The port of the server to connect to.
+         * @throws IOException Thrown if a failure occurred in the connect.
+         */
+        virtual void connect( const char* host, int port )
+            throw(SocketException) = 0;
+
+        /**
+         * Indicates whether or not this socket is connected to a destination.
+         * @returns true if connected
+         */
+        virtual bool isConnected() const = 0;
+
+        /**
+         * Gets the InputStream for this socket.
+         * @return The InputStream for this socket. NULL if not connected.
+         */
+        virtual io::InputStream* getInputStream() = 0;
+
+        /**
+         * Gets the OutputStream for this socket.
+         * @return the OutputStream for this socket.  NULL if not connected.
+         */
+        virtual io::OutputStream* getOutputStream() = 0;
+
+        /**
+         * Gets the linger time.
+         * @return The linger time in seconds.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getSoLinger() const throw( SocketException ) = 0;
+
+        /**
+         * Sets the linger time.
+         * @param linger The linger time in seconds.  If 0, linger is off.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setSoLinger( int linger ) throw( SocketException ) = 0;
+
+        /**
+         * Gets the keep alive flag.
+         * @return True if keep alive is enabled.
+         * @throws SocketException if the operation fails.
+         */
+        virtual bool getKeepAlive() const throw( SocketException ) = 0;
+
+        /**
+         * Enables/disables the keep alive flag.
+         * @param keepAlive If true, enables the flag.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setKeepAlive( bool keepAlive ) throw( SocketException ) = 0;
+
+        /**
+         * Gets the receive buffer size.
+         * @return the receive buffer size in bytes.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getReceiveBufferSize() const throw( SocketException ) = 0;
+
+        /**
+         * Sets the recieve buffer size.
+         * @param size Number of bytes to set the receive buffer to.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setReceiveBufferSize( int size ) throw( SocketException ) = 0;
+
+        /**
+         * Gets the reuse address flag.
+         * @return True if the address can be reused.
+         * @throws SocketException if the operation fails.
+         */
+        virtual bool getReuseAddress() const throw( SocketException ) = 0;
+
+        /**
+         * Sets the reuse address flag.
+         * @param reuse If true, sets the flag.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setReuseAddress( bool reuse ) throw( SocketException ) = 0;
+
+        /**
+         * Gets the send buffer size.
+         * @return the size in bytes of the send buffer.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getSendBufferSize() const throw( SocketException ) = 0;
+
+        /**
+         * Sets the send buffer size.
+         * @param size The number of bytes to set the send buffer to.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setSendBufferSize( int size ) throw( SocketException ) = 0;
+
+        /**
+         * Gets the timeout for socket operations.
+         * @return The timeout in milliseconds for socket operations.
+         * @throws SocketException Thrown if unable to retrieve the information.
+         */
+        virtual int getSoTimeout() const throw( SocketException ) = 0;
+
+        /**
+         * Sets the timeout for socket operations.
+         * @param timeout The timeout in milliseconds for socket operations.<p>
+         * @throws SocketException Thrown if unable to set the information.
+         */
+        virtual void setSoTimeout( int timeout ) throw( SocketException ) = 0;
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_SOCKET_H_*/

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.cpp?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.cpp Thu Apr 26 14:00:04 2007
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "SocketError.h"
+#include <activemq/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+    #include <Winsock2.h>
+#else
+    #include <errno.h>
+#endif
+
+using namespace decaf;
+using namespace decaf::net;
+
+// Platform constants.
+#if defined(HAVE_WINSOCK2_H)
+    const int SocketError::INTERRUPTED = WSAEINTR;
+#else
+    const int SocketError::INTERRUPTED = EINTR;
+#endif
+
+////////////////////////////////////////////////////////////////////////////////
+int SocketError::getErrorCode() {
+
+    #if defined(HAVE_WINSOCK2_H)
+
+        return ::WSAGetLastError();
+
+    #else
+
+        return errno;
+
+    #endif
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string SocketError::getErrorString() {
+
+    std::string returnValue;
+
+    // Get the error code.
+    int errorCode = getErrorCode();
+
+    #if defined(HAVE_WINSOCK2_H)
+
+        // Create the error string.
+        static const int errorStringSize = 512;
+        char errorString[errorStringSize];
+        memset( errorString, 0, errorStringSize );
+        ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+           0,
+           errorCode,
+           0,
+           errorString,
+           errorStringSize - 1,
+           NULL);
+
+        returnValue = errorString;
+
+    #else
+
+        // Create the error string.
+        returnValue = ::strerror(errorCode);
+
+    #endif
+
+    return returnValue;
+}
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketError.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NET_SOCKETERROR_H_
+#define _DECAF_NET_SOCKETERROR_H_
+
+#include <string>
+
+namespace decaf{
+namespace net{
+
+    /**
+     * Static utility class to simplify handling of error codes
+     * for socket operations.
+     */
+    class SocketError {
+    public:
+
+        /**
+         * Indicates that a socket operation was interrupted by a signal.
+         */
+        static const int INTERRUPTED;
+
+    public:
+
+        /**
+         * Gets the last error appropriate for the platform.
+         */
+        static int getErrorCode();
+
+        /**
+         * Gets the string description for the last error.
+         */
+        static std::string getErrorString();
+    };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETERROR_H_*/

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketException.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketException.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketException.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketException.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _DECAF_NET_SOCKETEXCEPTION_H_
+#define _DECAF_NET_SOCKETEXCEPTION_H_
+
+#include <decaf/io/IOException.h>
+
+namespace decaf{
+namespace net{
+
+    /**
+     * Exception for errors when manipulating sockets.
+     */
+    class SocketException : public io::IOException
+    {
+    public:
+
+        SocketException() throw() {}
+        SocketException( const lang::Exception& ex ) throw()
+        : io::IOException()
+        {
+            *(lang::Exception*)this = ex;
+        }
+        SocketException( const SocketException& ex ) throw()
+        : io::IOException()
+        {
+            *(lang::Exception*)this = ex;
+        }
+        SocketException( const char* file, const int lineNumber,
+                         const char* msg, ...) throw()
+        : io::IOException()
+        {
+            va_list vargs;
+            va_start(vargs, msg);
+            buildMessage(msg, vargs);
+
+            // Set the first mark for this exception.
+            setMark( file, lineNumber );
+        }
+
+        /**
+         * Clones this exception.  This is useful for cases where you need
+         * to preserve the type of the original exception as well as the message.
+         * All subclasses should override.
+         */
+        virtual lang::Exception* clone() const{
+            return new SocketException( *this );
+        }
+
+        virtual ~SocketException() throw() {}
+
+    };
+
+}}
+
+
+#endif // _DECAF_NET_SOCKETEXCEPTION_H_
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp Thu Apr 26 14:00:04 2007
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <decaf/util/Config.h>
+
+#if !defined(HAVE_WINSOCK2_H)
+    #include <sys/select.h>
+    #include <sys/socket.h>
+#else
+    #include <Winsock2.h>
+#endif
+
+#ifdef HAVE_SYS_IOCTL_H
+#define BSD_COMP /* Get FIONREAD on Solaris2. */
+#include <sys/ioctl.h>
+#endif
+
+// Pick up FIONREAD on Solaris 2.5.
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
+
+#include <decaf/network/SocketInputStream.h>
+#include <decaf/network/SocketError.h>
+#include <decaf/io/IOException.h>
+#include <decaf/util/Character.h>
+#include <decaf/exceptions/UnsupportedOperationException.h>
+#include <stdlib.h>
+#include <string>
+#include <stdio.h>
+#include <iostream>
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+using namespace decaf::util;
+using namespace decaf::lang;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+SocketInputStream::SocketInputStream( network::Socket::SocketHandle socket )
+{
+    this->socket = socket;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SocketInputStream::~SocketInputStream()
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t SocketInputStream::available() const throw (activemq::io::IOException){
+
+// The windows version
+#if defined(HAVE_WINSOCK2_H)
+
+    unsigned long numBytes = 0;
+
+    if (::ioctlsocket (socket, FIONREAD, &numBytes) == SOCKET_ERROR){
+        throw SocketException( __FILE__, __LINE__, "ioctlsocket failed" );
+    }
+
+    return (std::size_t)numBytes;
+
+#else // !defined(HAVE_WINSOCK2_H)
+
+    // If FIONREAD is defined - use ioctl to find out how many bytes
+    // are available.
+    #if defined(FIONREAD)
+
+        std::size_t numBytes = 0;
+        if( ::ioctl (socket, FIONREAD, &numBytes) != -1 ){
+            return numBytes;
+        }
+
+    #endif
+
+    // If we didn't get anything we can use select.  This is a little
+    // less functional.  We will poll on the socket - if there is data
+    // available, we'll return 1, otherwise we'll return zero.
+    #if defined(HAVE_SELECT)
+
+        fd_set rd;
+        FD_ZERO(&rd);
+        FD_SET( socket, &rd );
+        struct timeval tv;
+        tv.tv_sec = 0;
+        tv.tv_usec = 0;
+        int returnCode = ::select(socket+1, &rd, NULL, NULL, &tv);
+        if(returnCode == -1){
+            throw IOException(__FILE__, __LINE__, SocketError::getErrorString().c_str() );
+        }
+        return (returnCode == 0)? 0 : 1;
+
+    #else
+
+        return 0;
+
+    #endif /* HAVE_SELECT */
+
+
+#endif // !defined(HAVE_WINSOCK2_H)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+unsigned char SocketInputStream::read() throw (IOException){
+
+    unsigned char c;
+    std::size_t len = read( &c, 1 );
+    if( len != sizeof(c) ){
+        throw IOException( __FILE__, __LINE__,
+            "activemq::io::SocketInputStream::read - failed reading a byte");
+    }
+
+    return c;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t SocketInputStream::read( unsigned char* buffer,
+                                     std::size_t bufferSize ) throw (IOException)
+{
+    int len = 0;
+
+    // Loop to ignore any signal interruptions that occur during the read.
+    do {
+
+        // Read data from the socket.
+        len = ::recv(socket, (char*)buffer, (int)bufferSize, 0);
+
+        // Check for a closed socket.
+        if( len == 0 ){
+            throw IOException( __FILE__, __LINE__,
+                "activemq::io::SocketInputStream::read - The connection is broken" );
+        }
+
+    } while( len == -1 &&
+             SocketError::getErrorCode() == SocketError::INTERRUPTED );
+
+    // Check for error.
+    if( len == -1 ){
+
+        // Otherwise, this was a bad error - throw an exception.
+        throw IOException( __FILE__, __LINE__,
+                "activemq::io::SocketInputStream::read - %s", SocketError::getErrorString().c_str() );
+    }
+
+    return len;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t SocketInputStream::skip( std::size_t num AMQCPP_UNUSED )
+throw ( io::IOException, exceptions::UnsupportedOperationException ) {
+    throw exceptions::UnsupportedOperationException(__FILE__, __LINE__,
+        "skip() method is not supported");
+}
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NET_SOCKETINPUTSTREAM_H_
+#define _DECAF_NET_SOCKETINPUTSTREAM_H_
+
+#include <decaf/io/InputStream.h>
+#include <decaf/net/Socket.h>
+#include <decaf/lang/Mutex.h>
+
+namespace decaf{
+namespace net{
+
+    /**
+     * Input stream for performing reads on a socket.  This
+     * class will only work properly for blocking sockets.
+     */
+    class SocketInputStream : public io::InputStream
+    {
+    private:
+
+        // The socket handle.
+        Socket::SocketHandle socket;
+        concurrent::Mutex mutex;
+
+    public:
+
+        /**
+         * Constructor.
+         * @param socket the socket handle.
+         */
+        SocketInputStream( Socket::SocketHandle socket );
+
+        /**
+         * Destructor.
+         */
+        virtual ~SocketInputStream();
+
+        /**
+         * Locks the object.
+         * @throws ActiveMQException
+         */
+        virtual void lock() throw( exceptions::ActiveMQException ){
+            mutex.lock();
+        }
+
+        /**
+         * Unlocks the object.
+         * @throws ActiveMQException
+         */
+        virtual void unlock() throw( exceptions::ActiveMQException ){
+            mutex.unlock();
+        }
+
+        /**
+         * Waits on a signal from this object, which is generated
+         * by a call to Notify.  Must have this object locked before
+         * calling.
+         * @throws ActiveMQException
+         */
+        virtual void wait() throw( exceptions::ActiveMQException ){
+            mutex.wait();
+        }
+
+        /**
+         * Waits on a signal from this object, which is generated
+         * by a call to Notify.  Must have this object locked before
+         * calling.  This wait will timeout after the specified time
+         * interval.
+         * @param millisecs time in millisecsonds to wait, or WAIT_INIFINITE
+         * @throws ActiveMQException
+         */
+        virtual void wait( unsigned long millisecs )
+            throw( exceptions::ActiveMQException ) {
+
+            mutex.wait( millisecs );
+        }
+
+        /**
+         * Signals a waiter on this object that it can now wake
+         * up and continue.  Must have this object locked before
+         * calling.
+         * @throws ActiveMQException
+         */
+        virtual void notify() throw( exceptions::ActiveMQException ){
+            mutex.notify();
+        }
+
+        /**
+         * Signals the waiters on this object that it can now wake
+         * up and continue.  Must have this object locked before
+         * calling.
+         * @throws ActiveMQException
+         */
+        virtual void notifyAll() throw( exceptions::ActiveMQException ){
+            mutex.notifyAll();
+        }
+
+        /**
+         * Returns the number of bytes available on the socket to
+         * be read right now.
+         * @return The number of bytes currently available to
+         * be read on the socket.
+         */
+        virtual std::size_t available() const throw (activemq::io::IOException);
+
+        /**
+         * Reads a single byte from the buffer.  If no data
+         * is available, blocks until their is.
+         * @return The next byte.
+         * @throws IOException thrown if an error occurs.
+         */
+        virtual unsigned char read() throw ( io::IOException );
+
+        /**
+         * Reads an array of bytes from the buffer.  If no data
+         * is available, blocks until there is.
+         * @param buffer (out) the target buffer.
+         * @param bufferSize the size of the output buffer.
+         * @return The number of bytes read.
+         * @throws IOException thrown if an error occurs.
+         */
+        virtual std::size_t read( unsigned char* buffer,
+                                  std::size_t bufferSize )
+            throw (io::IOException);
+
+        /**
+         * Close - does nothing.  It is the responsibility of the owner
+         * of the socket object to close it.
+         * @throws CMSException
+         */
+        virtual void close() throw( cms::CMSException ){}
+
+        /**
+         * Not supported.
+         * @throws an UnsupportedOperationException.
+         */
+        virtual std::size_t skip( std::size_t num )
+            throw ( io::IOException,
+                    exceptions::UnsupportedOperationException );
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETINPUTSTREAM_H_*/

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp Thu Apr 26 14:00:04 2007
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "SocketOutputStream.h"
+#include <decaf/util/Config.h>
+#include <decaf/lang/Character.h>
+#include "SocketError.h"
+
+#ifdef HAVE_WINSOCK2_H
+    #include <Winsock2.h>
+#else
+    #include <sys/socket.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <iostream>
+
+#if defined(SOCKET_NOSIGNAL)
+    #define AMQ_SEND_OPTS SOCKET_NOSIGNAL
+#elif defined(MSG_NOSIGNAL)
+    #define AMQ_SEND_OPTS MSG_NOSIGNAL
+#else
+    #define AMQ_SEND_OPTS 0
+#endif
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+using namespace decaf::util;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+SocketOutputStream::SocketOutputStream( Socket::SocketHandle socket )
+{
+    this->socket = socket;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SocketOutputStream::~SocketOutputStream()
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketOutputStream::write( unsigned char c ) throw (IOException)
+{
+    write( &c, 1 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketOutputStream::write( const unsigned char* buffer, std::size_t len )
+    throw (IOException)
+{
+    std::size_t remaining = len;
+    int sendOpts = AMQ_SEND_OPTS;
+
+    while( remaining > 0 )
+    {
+        int length = ::send( socket, (const char*)buffer, (int)remaining, sendOpts );
+        if( length == -1 ){
+            throw IOException( __FILE__, __LINE__,
+                "activemq::io::SocketOutputStream::write - %s", SocketError::getErrorString().c_str() );
+        }
+
+        buffer+=length;
+        remaining -= length;
+    }
+}
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NET_SOCKETOUTPUTSTREAM_H_
+#define _DECAF_NET_SOCKETOUTPUTSTREAM_H_
+
+#include <decaf/io/OutputStream.h>
+#include <decaf/net/Socket.h>
+#include <decaf/lang/Mutex.h>
+
+namespace decaf{
+namespace net{
+
+    /**
+     * Output stream for performing write operations
+     * on a socket.
+     */
+    class SocketOutputStream : public io::OutputStream
+    {
+    private:
+
+        // The socket.
+        Socket::SocketHandle socket;
+        lang::Mutex mutex;
+
+    public:
+
+        /**
+         * Constructor.
+         * @param socket the socket handle.
+         */
+        SocketOutputStream( Socket::SocketHandle socket );
+
+        virtual ~SocketOutputStream();
+
+        /**
+         * Locks the object.
+         * @throws ActiveMQException
+         */
+        virtual void lock() throw( exceptions::ActiveMQException ){
+            mutex.lock();
+        }
+
+        /**
+         * Unlocks the object.
+         * @throws ActiveMQException
+         */
+        virtual void unlock() throw( exceptions::ActiveMQException ){
+            mutex.unlock();
+        }
+
+        /**
+         * Waits on a signal from this object, which is generated
+         * by a call to Notify.  Must have this object locked before
+         * calling.
+         * @throws ActiveMQException
+         */
+        virtual void wait() throw( exceptions::ActiveMQException ){
+            mutex.wait();
+        }
+
+        /**
+         * Waits on a signal from this object, which is generated
+         * by a call to Notify.  Must have this object locked before
+         * calling.  This wait will timeout after the specified time
+         * interval.
+         * @param millisecs time in millisecsonds to wait, or WAIT_INIFINITE
+         * @throws ActiveMQException
+         */
+        virtual void wait( unsigned long millisecs )
+            throw( exceptions::ActiveMQException ) {
+
+            mutex.wait( millisecs );
+        }
+
+        /**
+         * Signals a waiter on this object that it can now wake
+         * up and continue.  Must have this object locked before
+         * calling.
+         * @throws ActiveMQException
+         */
+        virtual void notify() throw( exceptions::ActiveMQException ){
+            mutex.notify();
+        }
+
+        /**
+         * Signals the waiters on this object that it can now wake
+         * up and continue.  Must have this object locked before
+         * calling.
+         */
+        virtual void notifyAll() throw( exceptions::ActiveMQException ){
+            mutex.notifyAll();
+         }
+
+        /**
+         * Writes a single byte to the output stream.
+         * @param c the byte.
+         * @throws IOException thrown if an error occurs.
+         */
+        virtual void write( unsigned char c ) throw ( io::IOException );
+
+        /**
+         * Writes an array of bytes to the output stream.
+         * @param buffer The array of bytes to write.
+         * @param len The number of bytes from the buffer to be written.
+         * @throws IOException thrown if an error occurs.
+         */
+        virtual void write( const unsigned char* buffer,
+                            std::size_t len ) throw ( io::IOException );
+
+        /**
+         * Flush - does nothing.
+         * @throws IOException
+         */
+        virtual void flush() throw ( io::IOException ){};
+
+        /**
+         * Close - does nothing.  It is the responsibility of the owner
+         * of the socket object to close it.
+         * @throws CMSException
+         */
+        virtual void close() throw( cms::CMSException ){}
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETOUTPUTSTREAM_H_*/

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp Thu Apr 26 14:00:04 2007
@@ -0,0 +1,448 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <decaf/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+    #include <Winsock2.h>
+    #include <Ws2tcpip.h>
+    #include <sys/stat.h>
+    #define stat _stat
+#else
+    #include <unistd.h>
+    #include <netdb.h>
+    #include <fcntl.h>
+    #include <sys/file.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+    #include <string.h>
+    #include <netinet/tcp.h>
+#endif
+
+#ifndef SHUT_RDWR
+    #define SHUT_RDWR 2 // Winsock2 doesn't seem to define this
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <sys/types.h>
+
+#include "TcpSocket.h"
+#include "SocketInputStream.h"
+#include "SocketOutputStream.h"
+#include "SocketError.h"
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+
+#if defined(HAVE_WINSOCK2_H)
+
+    // Static socket initializer needed for winsock
+
+    TcpSocket::StaticSocketInitializer::StaticSocketInitializer() {
+        socketInitError = NULL;
+        const WORD version_needed = MAKEWORD(2,2); // lo-order byte: major version
+        WSAData temp;
+        if( WSAStartup( version_needed, &temp ) ){
+           clear();
+           socketInitError = new SocketException ( __FILE__, __LINE__,
+               "winsock.dll was not found");
+        }
+    }
+    TcpSocket::StaticSocketInitializer::~StaticSocketInitializer() {
+        clear();
+        WSACleanup();
+    }
+
+    // Create static instance of the socket initializer.
+    TcpSocket::StaticSocketInitializer TcpSocket::staticSocketInitializer;
+
+#endif
+
+////////////////////////////////////////////////////////////////////////////////
+TcpSocket::TcpSocket() throw (SocketException)
+:
+    socketHandle( INVALID_SOCKET_HANDLE ),
+    inputStream( NULL ),
+    outputStream( NULL )
+{
+
+    try {
+
+#if defined(HAVE_WINSOCK2_H)
+        if( staticSocketInitializer.getSocketInitError() != NULL ) {
+            throw *staticSocketInitializer.getSocketInitError();
+        }
+#endif
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+TcpSocket::TcpSocket( SocketHandle socketHandle )
+:
+    socketHandle( INVALID_SOCKET_HANDLE ),
+    inputStream( NULL ),
+    outputStream( NULL )
+{
+    try {
+
+#if defined(HAVE_WINSOCK2_H)
+        if( staticSocketInitializer.getSocketInitError() != NULL ) {
+            throw *staticSocketInitializer.getSocketInitError();
+        }
+#endif
+
+        this->socketHandle = socketHandle;
+        this->inputStream = new SocketInputStream( socketHandle );
+        this->outputStream = new SocketOutputStream( socketHandle );
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+TcpSocket::~TcpSocket()
+{
+    // No shutdown, just close - dont want blocking destructor.
+    close();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+InputStream* TcpSocket::getInputStream(){
+    return inputStream;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+OutputStream* TcpSocket::getOutputStream(){
+    return outputStream;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::connect(const char* host, int port) throw ( SocketException )
+{
+    try{
+
+        if( isConnected() ) {
+            throw SocketException( __FILE__, __LINE__,
+                "Socket::connect - Socket already connected.  host: %s, port: %d", host, port );
+        }
+
+        // Create the socket.
+        checkResult( (int)(socketHandle = ::socket(AF_INET, SOCK_STREAM, 0)) );
+
+        // Check port value.
+        if (port <= 0 || port > 65535) {
+            close();
+            throw SocketException ( __FILE__, __LINE__,
+                "Socket::connect- Port out of range: %d", port );
+        }
+
+#ifdef SO_NOSIGPIPE // Don't want to get a SIGPIPE on FreeBSD and Mac OS X
+
+        int optval = 1;
+        checkResult( ::setsockopt( socketHandle, SOL_SOCKET, SO_NOSIGPIPE, (char*)&optval, sizeof(optval)) );
+
+#endif
+
+        sockaddr_in target_addr;
+        target_addr.sin_family = AF_INET;
+        target_addr.sin_port = htons( ( short ) port );
+        target_addr.sin_addr.s_addr = 0; // To be set later down...
+        memset( &target_addr.sin_zero, 0, sizeof( target_addr.sin_zero ) );
+
+        // Resolve name
+#if defined(HAVE_STRUCT_ADDRINFO)
+        addrinfo hints;
+        memset(&hints, 0, sizeof(addrinfo));
+        hints.ai_family = PF_INET;
+        struct addrinfo *res_ptr = NULL;
+
+        checkResult( ::getaddrinfo( host, NULL, &hints, &res_ptr ) );
+
+        assert(res_ptr->ai_addr->sa_family == AF_INET);
+        // Porting: On both 32bit and 64 bit systems that we compile to soo far, sin_addr
+        // is a 32 bit value, not an unsigned long.
+        assert( sizeof( ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr ) == 4 );
+        target_addr.sin_addr.s_addr = ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr;
+        freeaddrinfo( res_ptr );
+#else
+        struct ::hostent *he = ::gethostbyname(host);
+        if( he == NULL ) {
+            throw SocketException( __FILE__, __LINE__, "Failed to resolve hostname" );
+        }
+        target_addr.sin_addr.s_addr = *((in_addr_t *)he->h_addr);
+#endif
+
+        // Attempt the connection to the server.
+        checkResult( ::connect( socketHandle,
+                            ( const sockaddr * )&target_addr,
+                            sizeof( target_addr ) ) );
+
+        // Create an input/output stream for this socket.
+        inputStream = new SocketInputStream( socketHandle );
+        outputStream = new SocketOutputStream( socketHandle );
+
+    }
+    catch( SocketException& ex ) {
+        ex.setMark( __FILE__, __LINE__);
+        try{ close(); } catch( cms::CMSException& cx){ /* Absorb */ }
+        throw ex;
+    }
+    catch( ... ){
+        try{ close(); } catch( cms::CMSException& cx){ /* Absorb */ }
+        throw SocketException( __FILE__, __LINE__, "connect() caught unknown exception");
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::close() throw( cms::CMSException )
+{
+    // Destroy the input stream.
+    if( inputStream != NULL ){
+        delete inputStream;
+        inputStream = NULL;
+    }
+
+    // Destroy the output stream.
+    if( outputStream != NULL ){
+        delete outputStream;
+        outputStream = NULL;
+    }
+
+    if( isConnected() )
+    {
+        ::shutdown( socketHandle, SHUT_RDWR );
+
+        #if !defined(HAVE_WINSOCK2_H)
+            ::close( socketHandle );
+        #else
+           ::closesocket( socketHandle );
+        #endif
+
+       socketHandle = INVALID_SOCKET_HANDLE;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getSoLinger() const throw( SocketException ){
+
+   try{
+        linger value;
+        socklen_t length = sizeof( value );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, &length ));
+
+        return value.l_onoff? value.l_linger : 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setSoLinger( int dolinger ) throw( SocketException ){
+
+    try{
+        linger value;
+        value.l_onoff = dolinger != 0;
+        value.l_linger = dolinger;
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, sizeof(value) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool TcpSocket::getKeepAlive() const throw( SocketException ){
+
+    try{
+        int value;
+        socklen_t length = sizeof( int );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, &length ));
+        return value != 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setKeepAlive( const bool keepAlive ) throw( SocketException ){
+
+    try{
+        int value = keepAlive? 1 : 0;
+        checkResult(::setsockopt(socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, sizeof(int)) );
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getReceiveBufferSize() const throw( SocketException ){
+
+    try{
+        int value;
+        socklen_t length = sizeof( value );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&value, &length ));
+        return value;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setReceiveBufferSize( int size ) throw( SocketException ){
+
+    try{
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool TcpSocket::getReuseAddress() const throw( SocketException ){
+
+    try{
+        int value;
+        socklen_t length = sizeof( int );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, &length ));
+        return value != 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setReuseAddress( bool reuse ) throw( SocketException ){
+
+    try{
+        int value = reuse? 1 : 0;
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getSendBufferSize() const throw( SocketException ){
+
+    try{
+        int value;
+        socklen_t length = sizeof( value );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&value, &length ));
+        return value;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setSendBufferSize( int size ) throw( SocketException ){
+
+    try{
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&size, sizeof(size) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setSoTimeout ( const int millisecs ) throw ( SocketException )
+{
+    try{
+
+#if !defined(HAVE_WINSOCK2_H)
+        timeval timot;
+        timot.tv_sec = millisecs / 1000;
+        timot.tv_usec = (millisecs % 1000) * 1000;
+#else
+        int timot = millisecs;
+#endif
+
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVTIMEO, (const char*) &timot, sizeof (timot) ));
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDTIMEO, (const char*) &timot, sizeof (timot) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getSoTimeout() const throw( SocketException )
+{
+    try{
+
+#if !defined(HAVE_WINSOCK2_H)
+        timeval timot;
+        timot.tv_sec = 0;
+        timot.tv_usec = 0;
+        socklen_t size = sizeof(timot);
+#else
+        int timot = 0;
+        int size = sizeof(timot);
+#endif
+
+        checkResult(::getsockopt(socketHandle, SOL_SOCKET, SO_RCVTIMEO, (char*) &timot, &size));
+
+#if !defined(HAVE_WINSOCK2_H)
+        return (timot.tv_sec * 1000) + (timot.tv_usec / 1000);
+#else
+        return timot;
+#endif
+
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool TcpSocket::getTcpNoDelay() const throw ( cms::CMSException ) {
+
+    try{
+        int value;
+        socklen_t length = sizeof( int );
+        checkResult(::getsockopt( socketHandle, IPPROTO_TCP, TCP_NODELAY, (char*)&value, &length ));
+        return value != 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setTcpNoDelay( bool value ) throw ( cms::CMSException ) {
+
+    try{
+        int ivalue = value ? 1 : 0;
+        checkResult(::setsockopt( socketHandle, IPPROTO_TCP, TCP_NODELAY, (char*)&ivalue, sizeof(int) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::checkResult( int value ) const throw (SocketException) {
+
+    if( value < 0 ){
+        throw SocketException( __FILE__, __LINE__,
+            SocketError::getErrorString().c_str() );
+    }
+}
+
+

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h?view=auto&rev=532861
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h Thu Apr 26 14:00:04 2007
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _DECAF_NET_TCPSOCKET_H_
+#define _DECAF_NET_TCPSOCKET_H_
+
+#include <decaf/net/SocketException.h>
+#include <decaf/net/Socket.h>
+#include <decaf/io/InputStream.h>
+#include <decaf/io/OutputStream.h>
+
+#include <decaf/util/Config.h>
+
+namespace decaf{
+namespace net{
+
+    // Forward declarations
+    class SocketInputStream;
+    class SocketOutputStream;
+
+    /**
+     * Platform-independent implementation of the socket interface.
+     */
+    class TcpSocket : public Socket
+    {
+    private:
+
+        /**
+         * The handle for this socket.
+         */
+         SocketHandle socketHandle;
+
+         /**
+          * The input stream for reading this socket.
+          */
+         SocketInputStream* inputStream;
+
+         /**
+          * The output stream for writing to this socket.
+          */
+         SocketOutputStream* outputStream;
+
+    public:
+
+        /**
+         * Construct a non-connected socket.
+         * @throws SocketException thrown one windows if the static initialization
+         * call to WSAStartup was not successful.
+         */
+        TcpSocket() throw (SocketException);
+
+        /**
+         * Construct a connected or bound socket based on given
+         * socket handle.
+         * @param socketHandle a socket handle to wrap in the object
+         */
+        TcpSocket( SocketHandle socketHandle );
+
+        /**
+         * Destruct.
+         * Releases the socket handle but not
+         * gracefully shut down the connection.
+         */
+        virtual ~TcpSocket();
+
+        /**
+         * Gets the handle for the socket.
+         * @return SocketHabler for this Socket, can be NULL
+         */
+        SocketHandle getSocketHandle () {
+            return socketHandle;
+        }
+
+        /**
+         * Connects to the specified destination. Closes this socket if
+         * connected to another destination.
+         * @param host The host of the server to connect to.
+         * @param port The port of the server to connect to.
+         * @throws IOException Thrown if a failure occurred in the connect.
+         */
+        virtual void connect( const char* host, int port ) throw( SocketException );
+
+        /**
+         * Indicates whether or not this socket is connected to a destination.
+         * @return true if connected
+         */
+        virtual bool isConnected() const{
+            return socketHandle != INVALID_SOCKET_HANDLE;
+        }
+
+        /**
+         * Gets the InputStream for this socket.
+         * @return The InputStream for this socket. NULL if not connected.
+         */
+        virtual io::InputStream* getInputStream();
+
+        /**
+         * Gets the OutputStream for this socket.
+         * @return the OutputStream for this socket.  NULL if not connected.
+         */
+        virtual io::OutputStream* getOutputStream();
+
+        /**
+         * Gets the linger time.
+         * @return The linger time in seconds.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getSoLinger() const throw( SocketException );
+
+        /**
+         * Sets the linger time.
+         * @param linger The linger time in seconds.  If 0, linger is off.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setSoLinger( int linger ) throw( SocketException );
+
+        /**
+         * Gets the keep alive flag.
+         * @return True if keep alive is enabled.
+         * @throws SocketException if the operation fails.
+         */
+        virtual bool getKeepAlive() const throw( SocketException );
+
+        /**
+         * Enables/disables the keep alive flag.
+         * @param keepAlive If true, enables the flag.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setKeepAlive( bool keepAlive ) throw( SocketException );
+
+        /**
+         * Gets the receive buffer size.
+         * @return the receive buffer size in bytes.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getReceiveBufferSize() const throw( SocketException );
+
+        /**
+         * Sets the recieve buffer size.
+         * @param size Number of bytes to set the receive buffer to.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setReceiveBufferSize( int size ) throw( SocketException );
+
+        /**
+         * Gets the reuse address flag.
+         * @return True if the address can be reused.
+         * @throws SocketException if the operation fails.
+         */
+        virtual bool getReuseAddress() const throw( SocketException );
+
+        /**
+         * Sets the reuse address flag.
+         * @param reuse If true, sets the flag.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setReuseAddress( bool reuse ) throw( SocketException );
+
+        /**
+         * Gets the send buffer size.
+         * @return the size in bytes of the send buffer.
+         * @throws SocketException if the operation fails.
+         */
+        virtual int getSendBufferSize() const throw( SocketException );
+
+        /**
+         * Sets the send buffer size.
+         * @param size The number of bytes to set the send buffer to.
+         * @throws SocketException if the operation fails.
+         */
+        virtual void setSendBufferSize( int size ) throw( SocketException );
+
+        /**
+         * Gets the timeout for socket operations.
+         * @return The timeout in milliseconds for socket operations.
+         * @throws SocketException Thrown if unable to retrieve the information.
+         */
+        virtual int getSoTimeout() const throw( SocketException );
+
+        /**
+         * Sets the timeout for socket operations.
+         * @param timeout The timeout in milliseconds for socket operations.<p>
+         * @throws SocketException Thrown if unable to set the information.
+         */
+        virtual void setSoTimeout( int timeout ) throw(SocketException);
+
+        /**
+         * Closes this object and deallocates the appropriate resources.
+         * @throws CMSException
+         */
+        virtual void close() throw( cms::CMSException );
+
+    public:
+
+        /**
+         * Gets the Status of the TCP_NODELAY param for this socket as a Bool
+         * @returns true if TCP_NODELAY is enabled
+         * @throws CMSException
+         */
+        virtual bool getTcpNoDelay() const throw ( cms::CMSException );
+
+        /**
+         * Sets the Status of the TCP_NODELAY param for this socket as a Bool
+         * @param value - true if TCP_NODELAY is to be enabled
+         * @throws CMSException
+         */
+        virtual void setTcpNoDelay( bool value ) throw ( cms::CMSException );
+
+    protected:
+
+        #if defined(HAVE_WINSOCK2_H)
+
+            // WINDOWS needs initialization of winsock
+            class StaticSocketInitializer {
+            private:
+
+                SocketException* socketInitError;
+
+                void clear(){
+                    if( socketInitError != NULL ){
+                        delete socketInitError;
+                    }
+                    socketInitError = NULL;
+                }
+
+            public:
+
+                SocketException* getSocketInitError() {
+                    return socketInitError;
+                }
+
+                StaticSocketInitializer();
+                virtual ~StaticSocketInitializer();
+
+            };
+
+            static StaticSocketInitializer staticSocketInitializer;
+        #endif
+
+        void checkResult( int value ) const throw (SocketException);
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_TCPSOCKET_H_*/



Mime
View raw message