You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/07/25 21:18:05 UTC
svn commit: r559564 - in /activemq/activemq-cpp/trunk/src/decaf/src/main: ./
decaf/net/ decaf/util/
Author: tabish
Date: Wed Jul 25 12:18:04 2007
New Revision: 559564
URL: http://svn.apache.org/viewvc?view=rev&rev=559564
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Working on APRizing the sockets, currently broken.
Added:
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.cpp
Modified:
activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/BufferedSocket.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/Socket.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.h
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am Wed Jul 25 12:18:04 2007
@@ -47,7 +47,8 @@
decaf/util/logging/Logger.cpp \
decaf/util/logging/LogWriter.cpp \
decaf/util/logging/SimpleLogger.cpp \
- decaf/util/logging/LogManager.cpp
+ decaf/util/logging/LogManager.cpp \
+ decaf/util/Config.cpp
h_sources = \
decaf/net/BufferedSocket.h \
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/BufferedSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/BufferedSocket.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/BufferedSocket.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/BufferedSocket.cpp Wed Jul 25 12:18:04 2007
@@ -37,8 +37,7 @@
inputBufferSize(0),
outputBufferSize(0)
{
- if(inputBufferSize < 0 || outputBufferSize < 0 )
- {
+ if(inputBufferSize < 0 || outputBufferSize < 0 ) {
throw IllegalArgumentException(
__FILE__, __LINE__,
"BufferedSocket::BufferedSocket - buffer sizes must be >=0! "
@@ -47,8 +46,7 @@
outputBufferSize );
}
- if(socket == NULL)
- {
+ if(socket == NULL) {
throw IllegalArgumentException(
__FILE__, __LINE__,
"BufferedSocket::BufferedSocket - Constructed with NULL Socket");
@@ -61,10 +59,8 @@
}
////////////////////////////////////////////////////////////////////////////////
-BufferedSocket::~BufferedSocket()
-{
- try
- {
+BufferedSocket::~BufferedSocket() {
+ try {
close();
}
DECAF_CATCH_NOTHROW( Exception )
@@ -73,12 +69,11 @@
////////////////////////////////////////////////////////////////////////////////
void BufferedSocket::connect( const char* host, int port )
- throw( SocketException )
-{
- try
- {
- if( socket->isConnected() )
- {
+ throw( SocketException ) {
+
+ try {
+
+ if( socket->isConnected() ) {
throw SocketException( __FILE__, __LINE__,
"BufferedSocket::connect() - socket already connected" );
}
@@ -98,12 +93,11 @@
}
////////////////////////////////////////////////////////////////////////////////
-void BufferedSocket::close() throw( lang::Exception )
-{
- try
- {
- if( outputStream != NULL )
- {
+void BufferedSocket::close() throw( lang::Exception ) {
+
+ try {
+
+ if( outputStream != NULL ) {
// Ensure all data is written
outputStream->flush();
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.cpp Wed Jul 25 12:18:04 2007
@@ -18,22 +18,6 @@
#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>
@@ -45,127 +29,93 @@
using namespace decaf;
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() {
+ socketHandle = (apr_socket_t*)Socket::INVALID_SOCKET_HANDLE;
+ apr_pool_create( &apr_pool, NULL );
}
////////////////////////////////////////////////////////////////////////////////
-ServerSocket::~ServerSocket()
-{
+ServerSocket::~ServerSocket() {
// No shutdown, just close - dont want blocking destructor.
close();
+
+ // Free up the APR pool, this will release any remaining data we
+ // allocated but have deallocated.
+ apr_pool_destroy( apr_pool );
}
////////////////////////////////////////////////////////////////////////////////
-void ServerSocket::bind( const char* host, int port ) throw ( SocketException )
-{
- bind (host, port, SOMAXCONN);
+void ServerSocket::bind( const char* host, int port ) throw ( SocketException ) {
+ this->bind( host, port, SOMAXCONN );
}
////////////////////////////////////////////////////////////////////////////////
void ServerSocket::bind( const char* host,
int port,
- int backlog ) throw ( SocketException )
-{
- if(isBound()) {
+ int backlog ) throw ( SocketException ) {
+
+ apr_status_t result = APR_SUCCESS;
+
+ 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__,
+ 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) );
+ // Create the Address Info for the Socket
+ result = apr_sockaddr_info_get(
+ &socketAddress, host, APR_INET, port, 0, apr_pool );
+
+ if( result != APR_SUCCESS ) {
+ socketHandle = (apr_socket_t*)Socket::INVALID_SOCKET_HANDLE;
+ throw SocketException(
+ __FILE__, __LINE__,
+ SocketError::getErrorString().c_str() );
+ }
+
+ // Create the socket.
+ result = apr_socket_create(
+ &socketHandle, APR_INET, SOCK_STREAM, APR_PROTO_TCP, apr_pool );
+
+ if( result != APR_SUCCESS ) {
+ socketHandle = (apr_socket_t*)Socket::INVALID_SOCKET_HANDLE;
+ throw SocketException(
+ __FILE__, __LINE__,
+ SocketError::getErrorString().c_str() );
+ }
- status = ::bind(socketHandle,
- reinterpret_cast<sockaddr*>(&bind_addr), sizeof( bind_addr ));
+ // Set the socket to reuse the address and default as blocking
+ apr_socket_opt_set( socketHandle, APR_SO_REUSEADDR, 1 );
+ apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, 0);
+ apr_socket_timeout_set( socketHandle, -1 );
- if( status < 0 ){
+ // Bind to the Socket, this may be where we find out if the port is in use.
+ result = apr_socket_bind( socketHandle, socketAddress );
+
+ if( result != APR_SUCCESS ) {
close();
- throw SocketException ( __FILE__, __LINE__,
- "ServerSocket::bind - %s", SocketError::getErrorString().c_str() );
+ throw SocketException(
+ __FILE__, __LINE__,
+ "ServerSocket::bind - %s",
+ SocketError::getErrorString().c_str() );
}
- status = ::listen( socketHandle, (int)backlog );
- if( status < 0 ) {
+
+ // Setup the listen for incoming connection requests
+ result = apr_socket_listen( socketHandle, backlog );
+
+ if( result != APR_SUCCESS ) {
close();
- throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
+ throw SocketException(
+ __FILE__, __LINE__,
+ "ServerSocket::bind - %s",
+ SocketError::getErrorString().c_str() );
}
}
@@ -173,50 +123,34 @@
void ServerSocket::close() throw ( lang::Exception ){
if( isBound() ) {
-
- #if !defined(HAVE_WINSOCK2_H)
- ::close( socketHandle );
- #else
- ::closesocket( socketHandle );
- #endif
-
- socketHandle = Socket::INVALID_SOCKET_HANDLE;
+ apr_socket_close( socketHandle );
+ socketHandle = (apr_socket_t*)Socket::INVALID_SOCKET_HANDLE;
}
}
////////////////////////////////////////////////////////////////////////////////
bool ServerSocket::isBound() const {
- return this->socketHandle != Socket::INVALID_SOCKET_HANDLE;
+ return this->socketHandle != (apr_socket_t*)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;
+ SocketHandle incoming = NULL;
+ apr_status_t result = APR_SUCCESS;
// Loop to ignore any signal interruptions that occur during the operation.
do {
+ result = apr_socket_accept( &incoming, socketHandle, apr_pool );
+ } while( result == APR_EINTR );
- 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() );
+ if( result != APR_SUCCESS ) {
+ std::cout << "Failed to accept New Connection:" << std::endl;
+ throw SocketException(
+ __FILE__, __LINE__,
+ "ServerSocket::accept - %s",
+ SocketError::getErrorString().c_str() );
}
- return new TcpSocket( ss_socket_handle );
+ return new TcpSocket( incoming );
}
-
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/ServerSocket.h Wed Jul 25 12:18:04 2007
@@ -21,6 +21,9 @@
#include <decaf/net/SocketException.h>
#include <decaf/util/Config.h>
+#include <apr_pools.h>
+#include <apr_network_io.h>
+
namespace decaf{
namespace net{
@@ -31,11 +34,14 @@
{
public:
- typedef Socket::SocketHandle SocketHandle;
+ typedef apr_socket_t* SocketHandle;
+ typedef apr_sockaddr_t* SocketAddress;
private:
SocketHandle socketHandle;
+ SocketAddress socketAddress;
+ apr_pool_t* apr_pool;
public:
@@ -85,35 +91,6 @@
* @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
};
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/Socket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/Socket.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/Socket.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/Socket.h Wed Jul 25 12:18:04 2007
@@ -23,9 +23,7 @@
#include <decaf/io/Closeable.h>
#include <decaf/util/Config.h>
-#if defined(HAVE_WINSOCK2_H)
-#include <Winsock2.h> // SOCKET
-#endif
+#include <apr_network_io.h>
namespace decaf{
namespace net{
@@ -34,17 +32,20 @@
{
public:
- // Define the SocketHandle type.
- #if !defined(HAVE_WINSOCK2_H)
- typedef int SocketHandle;
- #else
- typedef SOCKET SocketHandle;
- #endif
+ /**
+ * Define the SocketHandle type.
+ */
+ typedef apr_socket_t* SocketHandle;
+
+ /**
+ * Define the SocketAddress type
+ */
+ typedef apr_sockaddr_t* SocketAddress;
/**
* Defines a constant for an invalid socket handle.
*/
- static const SocketHandle INVALID_SOCKET_HANDLE = (SocketHandle) -1;
+ static const int INVALID_SOCKET_HANDLE = 0;
public:
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.cpp Wed Jul 25 12:18:04 2007
@@ -18,34 +18,12 @@
#include "SocketError.h"
#include <decaf/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
+ return apr_get_netos_error();
}
////////////////////////////////////////////////////////////////////////////////
@@ -54,31 +32,9 @@
std::string returnValue;
// Get the error code.
- int errorCode = getErrorCode();
+ apr_status_t errorCode = apr_get_netos_error();
- #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;
+ // Create a buffer and get the error
+ char buffer[256];
+ return apr_strerror( errorCode, buffer, sizeof( buffer ) );
}
-
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketError.h Wed Jul 25 12:18:04 2007
@@ -32,13 +32,6 @@
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();
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp Wed Jul 25 12:18:04 2007
@@ -31,13 +31,12 @@
Socket* SocketFactory::createSocket(
const std::string& uri,
const Properties& properties)
- throw ( SocketException )
-{
- try
- {
+ throw ( SocketException ) {
+
+ try {
+
// Ensure something is actually passed in for the URI
- if( uri == "" )
- {
+ if( uri == "" ) {
throw SocketException( __FILE__, __LINE__,
"SocketTransport::start() - uri not provided" );
}
@@ -46,16 +45,14 @@
// Extract the port.
std::size_t portIx = dummy.find( ':' );
- if( portIx == string::npos )
- {
+ if( portIx == string::npos ) {
throw SocketException( __FILE__, __LINE__,
"SocketTransport::start() - uri malformed - port not specified: %s", uri.c_str() );
}
string host = dummy.substr( 0, portIx );
string portString = dummy.substr( portIx + 1 );
int port;
- if( sscanf( portString.c_str(), "%d", &port) != 1 )
- {
+ if( sscanf( portString.c_str(), "%d", &port) != 1 ) {
throw SocketException( __FILE__, __LINE__,
"SocketTransport::start() - unable to extract port from uri: %s", uri.c_str() );
}
@@ -100,8 +97,8 @@
// clean it up when it is cleaned up.
TcpSocket* tcpSocket = new TcpSocket();
- try
- {
+ try {
+
// Connect the socket.
tcpSocket->connect( host.c_str(), port );
@@ -117,10 +114,7 @@
if( soSendBufferSize > 0 ){
tcpSocket->setSendBufferSize( soSendBufferSize );
}
-
- }
- catch ( SocketException& ex )
- {
+ } catch ( SocketException& ex ) {
ex.setMark( __FILE__, __LINE__ );
try{
delete tcpSocket;
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.cpp Wed Jul 25 12:18:04 2007
@@ -44,6 +44,8 @@
#include <stdio.h>
#include <iostream>
+#include <apr_portable.h>
+
using namespace decaf;
using namespace decaf::net;
using namespace decaf::io;
@@ -52,8 +54,7 @@
using namespace std;
////////////////////////////////////////////////////////////////////////////////
-SocketInputStream::SocketInputStream( net::Socket::SocketHandle socket )
-{
+SocketInputStream::SocketInputStream( net::Socket::SocketHandle socket ) {
this->socket = socket;
this->closed = false;
}
@@ -69,12 +70,16 @@
////////////////////////////////////////////////////////////////////////////////
std::size_t SocketInputStream::available() const throw ( io::IOException ){
+ // Convert to an OS level socket.
+ apr_os_sock_t* oss = NULL;
+ apr_os_sock_get( oss, socket );
+
// The windows version
#if defined(HAVE_WINSOCK2_H)
unsigned long numBytes = 0;
- if (::ioctlsocket (socket, FIONREAD, &numBytes) == SOCKET_ERROR){
+ if( ::ioctlsocket( *oss, FIONREAD, &numBytes ) == SOCKET_ERROR ){
throw SocketException( __FILE__, __LINE__, "ioctlsocket failed" );
}
@@ -87,7 +92,7 @@
#if defined(FIONREAD)
std::size_t numBytes = 0;
- if( ::ioctl (socket, FIONREAD, &numBytes) != -1 ){
+ if( ::ioctl( *oss, FIONREAD, &numBytes ) != -1 ){
return numBytes;
}
@@ -100,15 +105,17 @@
fd_set rd;
FD_ZERO(&rd);
- FD_SET( socket, &rd );
+ FD_SET( *oss, &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() );
+ int returnCode = ::select( *oss+1, &rd, NULL, NULL, &tv );
+ if( returnCode == -1 ){
+ throw IOException(
+ __FILE__, __LINE__,
+ SocketError::getErrorString().c_str() );
}
- return (returnCode == 0)? 0 : 1;
+ return (returnCode == 0) ? 0 : 1;
#else
@@ -116,16 +123,19 @@
#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) && !closed ){
+ apr_status_t result = APR_SUCCESS;
+ char c;
+ apr_size_t size = 1;
+
+ result = apr_socket_recv( socket, &c, &size );
+
+ if( ( size != sizeof(c) && !closed ) || result != APR_SUCCESS ){
throw IOException( __FILE__, __LINE__,
"activemq::io::SocketInputStream::read - failed reading a byte");
}
@@ -137,39 +147,38 @@
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 {
+ apr_size_t size = (apr_size_t)bufferSize;
+ apr_status_t result = APR_SUCCESS;
- // Read data from the socket.
- len = ::recv(socket, (char*)buffer, (int)bufferSize, 0);
-
- // Check for a closed socket.
- if( len == 0 && closed ){
- throw IOException( __FILE__, __LINE__,
- "activemq::io::SocketInputStream::read - The connection is broken" );
- }
-
- } while( !closed && len == -1 &&
- SocketError::getErrorCode() == SocketError::INTERRUPTED );
+ // Read data from the socket, size on input is size of buffer, when done
+ // size is the number of bytes actually read, can be <= bufferSize.
+ result = apr_socket_recv( socket, (char*)buffer, &size );
+
+ // Check for a closed socket.
+ if( size == 0 || closed || APR_STATUS_IS_EOF( result ) ){
+ throw IOException(
+ __FILE__, __LINE__,
+ "activemq::io::SocketInputStream::read - The connection is broken" );
+ }
// Check for error.
- if( len == -1 ){
-
+ if( result != APR_SUCCESS ){
// Otherwise, this was a bad error - throw an exception.
- throw IOException( __FILE__, __LINE__,
- "activemq::io::SocketInputStream::read - %s", SocketError::getErrorString().c_str() );
+ throw IOException(
+ __FILE__, __LINE__,
+ "decaf::net::SocketInputStream::read - %s",
+ SocketError::getErrorString().c_str() );
}
- return len;
+ return size;
}
////////////////////////////////////////////////////////////////////////////////
std::size_t SocketInputStream::skip( std::size_t num DECAF_UNUSED )
-throw ( io::IOException, lang::exceptions::UnsupportedOperationException ) {
+ throw ( io::IOException, lang::exceptions::UnsupportedOperationException ) {
+
throw lang::exceptions::UnsupportedOperationException(
__FILE__, __LINE__,
- "skip() method is not supported");
+ "SocketInputStream::skip() method is not supported");
}
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketInputStream.h Wed Jul 25 12:18:04 2007
@@ -30,8 +30,7 @@
* Input stream for performing reads on a socket. This
* class will only work properly for blocking sockets.
*/
- class DECAF_API SocketInputStream : public io::InputStream
- {
+ class DECAF_API SocketInputStream : public io::InputStream {
private:
// The socket handle.
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.cpp Wed Jul 25 12:18:04 2007
@@ -20,25 +20,6 @@
#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;
@@ -46,8 +27,7 @@
using namespace std;
////////////////////////////////////////////////////////////////////////////////
-SocketOutputStream::SocketOutputStream( Socket::SocketHandle socket )
-{
+SocketOutputStream::SocketOutputStream( Socket::SocketHandle socket ) {
this->socket = socket;
this->closed = false;
}
@@ -63,28 +43,31 @@
}
////////////////////////////////////////////////////////////////////////////////
-void SocketOutputStream::write( unsigned char c ) throw (IOException)
-{
+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 && !closed )
- {
- int length = ::send( socket, (const char*)buffer, (int)remaining, sendOpts );
- if( length == -1 || closed ){
- throw IOException( __FILE__, __LINE__,
- "activemq::io::SocketOutputStream::write - %s", SocketError::getErrorString().c_str() );
+ throw ( IOException ) {
+
+ apr_size_t remaining = (apr_size_t)len;
+ apr_status_t result = APR_SUCCESS;
+
+ while( remaining > 0 && !closed ) {
+ // On input remaining is the bytes to send, after return remaining
+ // is the amount actually sent.
+ result = apr_socket_send( socket, (const char*)buffer, &remaining );
+
+ if( result != APR_SUCCESS || closed ) {
+ throw IOException(
+ __FILE__, __LINE__,
+ "decaf::net::SocketOutputStream::write - %s",
+ SocketError::getErrorString().c_str() );
}
- buffer+=length;
- remaining -= length;
+ // move us to next position to write, or maybe end.
+ buffer += remaining;
+ remaining = len - remaining;
}
}
-
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketOutputStream.h Wed Jul 25 12:18:04 2007
@@ -29,8 +29,7 @@
* Output stream for performing write operations
* on a socket.
*/
- class DECAF_API SocketOutputStream : public io::OutputStream
- {
+ class DECAF_API SocketOutputStream : public io::OutputStream {
private:
// The socket.
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.cpp?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.cpp Wed Jul 25 12:18:04 2007
@@ -16,33 +16,6 @@
*/
#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"
@@ -53,48 +26,11 @@
using namespace decaf::io;
using namespace decaf::lang;
-#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 ),
+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
- }
- DECAF_CATCH_RETHROW( SocketException )
- DECAF_CATCHALL_THROW( SocketException )
+ outputStream( NULL ) {
}
////////////////////////////////////////////////////////////////////////////////
@@ -102,15 +38,9 @@
:
socketHandle( INVALID_SOCKET_HANDLE ),
inputStream( NULL ),
- outputStream( NULL )
-{
- try {
+ outputStream( NULL ) {
-#if defined(HAVE_WINSOCK2_H)
- if( staticSocketInitializer.getSocketInitError() != NULL ) {
- throw *staticSocketInitializer.getSocketInitError();
- }
-#endif
+ try {
this->socketHandle = socketHandle;
this->inputStream = new SocketInputStream( socketHandle );
@@ -121,8 +51,7 @@
}
////////////////////////////////////////////////////////////////////////////////
-TcpSocket::~TcpSocket()
-{
+TcpSocket::~TcpSocket() {
// No shutdown, just close - dont want blocking destructor.
close();
}
@@ -138,8 +67,8 @@
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::connect(const char* host, int port) throw ( SocketException )
-{
+void TcpSocket::connect(const char* host, int port) throw ( SocketException ) {
+
try{
if( isConnected() ) {
@@ -147,70 +76,44 @@
"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
+ // Create the APR Pool
+ apr_pool_create( &apr_pool, NULL );
- 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 the Address data
+ checkResult( apr_sockaddr_info_get(
+ &socketAddress, host, APR_INET, port, 0, apr_pool ) );
+
+ // Create the actual socket.
+ checkResult( apr_socket_create(
+ &socketHandle, socketAddress->family, SOCK_STREAM, APR_PROTO_TCP, apr_pool ) );
+
+ // it is a good idea to specify socket options explicitly. in this
+ // case, we make a blocking socket with timeout, this should allow us
+ // the time needed to connect to the socket before returning, but not
+ // block us forever waiting if there isn't anyone there..
+ apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, 1 );
+ apr_socket_timeout_set( socketHandle, 1000 );
+
+ checkResult( apr_socket_connect( socketHandle, socketAddress ) );
+
+ // Now that we are connected, we want to set ourself up as a blocking
+ // socket by default.
+ apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, 0 );
+ apr_socket_timeout_set( socketHandle, -1 );
// Create an input/output stream for this socket.
inputStream = new SocketInputStream( socketHandle );
outputStream = new SocketOutputStream( socketHandle );
- }
- catch( SocketException& ex ) {
+ } catch( SocketException& ex ) {
ex.setMark( __FILE__, __LINE__);
try{ close(); } catch( lang::Exception& cx){ /* Absorb */ }
throw ex;
- }
- catch( ... ){
+ } catch( ... ) {
try{ close(); } catch( lang::Exception& cx){ /* Absorb */ }
- throw SocketException( __FILE__, __LINE__, "connect() caught unknown exception");
+ throw SocketException(
+ __FILE__, __LINE__,
+ "TcpSocket::connect() - caught unknown exception" );
}
}
@@ -229,17 +132,18 @@
outputStream = NULL;
}
- if( isConnected() )
- {
- ::shutdown( socketHandle, SHUT_RDWR );
-
- #if !defined(HAVE_WINSOCK2_H)
- ::close( socketHandle );
- #else
- ::closesocket( socketHandle );
- #endif
+ // When connected we first shutdown, which breaks our reads and writes
+ // then we close to free APR resources.
+ if( isConnected() ) {
+ apr_socket_shutdown( socketHandle, APR_SHUTDOWN_READWRITE );
+ apr_socket_close( socketHandle );
+ socketHandle = INVALID_SOCKET_HANDLE;
+ }
- socketHandle = INVALID_SOCKET_HANDLE;
+ // Destroy the APR Pool
+ if( apr_pool != NULL ) {
+ apr_pool_destroy( apr_pool );
+ apr_pool = NULL;
}
}
@@ -247,11 +151,9 @@
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;
+ int value = 0;
+ checkResult( apr_socket_opt_get( socketHandle, APR_SO_LINGER, &value ) );
+ return value;
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -261,10 +163,7 @@
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) ));
+ checkResult( apr_socket_opt_set( socketHandle, APR_SO_LINGER, dolinger ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -274,9 +173,8 @@
bool TcpSocket::getKeepAlive() const throw( SocketException ){
try{
- int value;
- socklen_t length = sizeof( int );
- checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, &length ));
+ int value = 0;
+ checkResult( apr_socket_opt_get( socketHandle, APR_SO_KEEPALIVE, &value ) );
return value != 0;
}
DECAF_CATCH_RETHROW( SocketException )
@@ -287,8 +185,8 @@
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)) );
+ int value = keepAlive ? 1 : 0;
+ checkResult( apr_socket_opt_set( socketHandle, APR_SO_KEEPALIVE, value ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -299,8 +197,7 @@
try{
int value;
- socklen_t length = sizeof( value );
- checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&value, &length ));
+ checkResult( apr_socket_opt_get( socketHandle, APR_SO_RCVBUF, &value ) );
return value;
}
DECAF_CATCH_RETHROW( SocketException )
@@ -311,7 +208,7 @@
void TcpSocket::setReceiveBufferSize( int size ) throw( SocketException ){
try{
- checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size) ));
+ checkResult( apr_socket_opt_set( socketHandle, APR_SO_RCVBUF, size ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -322,8 +219,7 @@
try{
int value;
- socklen_t length = sizeof( int );
- checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, &length ));
+ checkResult( apr_socket_opt_get( socketHandle, APR_SO_REUSEADDR, &value ) );
return value != 0;
}
DECAF_CATCH_RETHROW( SocketException )
@@ -334,8 +230,8 @@
void TcpSocket::setReuseAddress( bool reuse ) throw( SocketException ){
try{
- int value = reuse? 1 : 0;
- checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) ));
+ int value = reuse ? 1 : 0;
+ checkResult( apr_socket_opt_set( socketHandle, APR_SO_REUSEADDR, value ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -346,8 +242,7 @@
try{
int value;
- socklen_t length = sizeof( value );
- checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&value, &length ));
+ checkResult( apr_socket_opt_get( socketHandle, APR_SO_SNDBUF, &value ) );
return value;
}
DECAF_CATCH_RETHROW( SocketException )
@@ -358,7 +253,7 @@
void TcpSocket::setSendBufferSize( int size ) throw( SocketException ){
try{
- checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&size, sizeof(size) ));
+ checkResult( apr_socket_opt_set( socketHandle, APR_SO_SNDBUF, size ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -368,17 +263,8 @@
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) ));
+ // Time is in microseconds so multiply by 1000.
+ checkResult( apr_socket_timeout_set( socketHandle, millisecs * 1000 ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
@@ -388,29 +274,13 @@
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
-
+ // Time is in microseconds so divide by 1000.
+ apr_interval_time_t value = 0;
+ checkResult( apr_socket_timeout_get( socketHandle, &value ) );
+ return value / 1000;
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
-
}
////////////////////////////////////////////////////////////////////////////////
@@ -418,8 +288,7 @@
try{
int value;
- socklen_t length = sizeof( int );
- checkResult(::getsockopt( socketHandle, IPPROTO_TCP, TCP_NODELAY, (char*)&value, &length ));
+ checkResult( apr_socket_opt_get( socketHandle, APR_TCP_NODELAY, &value ) );
return value != 0;
}
DECAF_CATCH_RETHROW( SocketException )
@@ -431,19 +300,18 @@
try{
int ivalue = value ? 1 : 0;
- checkResult(::setsockopt( socketHandle, IPPROTO_TCP, TCP_NODELAY, (char*)&ivalue, sizeof(int) ));
+ checkResult( apr_socket_opt_set( socketHandle, APR_TCP_NODELAY, ivalue ) );
}
DECAF_CATCH_RETHROW( SocketException )
DECAF_CATCHALL_THROW( SocketException )
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::checkResult( int value ) const throw (SocketException) {
+void TcpSocket::checkResult( apr_status_t value ) const throw (SocketException) {
- if( value < 0 ){
- throw SocketException( __FILE__, __LINE__,
+ if( value != APR_SUCCESS ){
+ throw SocketException(
+ __FILE__, __LINE__,
SocketError::getErrorString().c_str() );
}
}
-
-
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/TcpSocket.h Wed Jul 25 12:18:04 2007
@@ -21,9 +21,10 @@
#include <decaf/net/Socket.h>
#include <decaf/io/InputStream.h>
#include <decaf/io/OutputStream.h>
-
#include <decaf/util/Config.h>
+#include <apr_pools.h>
+
namespace decaf{
namespace net{
@@ -34,24 +35,33 @@
/**
* Platform-independent implementation of the socket interface.
*/
- class DECAF_API TcpSocket : public Socket
- {
+ class DECAF_API TcpSocket : public Socket {
private:
/**
+ * APR Socket Pool to allocate from
+ */
+ apr_pool_t* apr_pool;
+
+ /**
* The handle for this socket.
*/
- SocketHandle socketHandle;
+ SocketHandle socketHandle;
+
+ /**
+ * The Address info for this Socket
+ */
+ SocketAddress socketAddress;
+
+ /**
+ * The input stream for reading this socket.
+ */
+ SocketInputStream* inputStream;
- /**
- * The input stream for reading this socket.
- */
- SocketInputStream* inputStream;
-
- /**
- * The output stream for writing to this socket.
- */
- SocketOutputStream* outputStream;
+ /**
+ * The output stream for writing to this socket.
+ */
+ SocketOutputStream* outputStream;
public:
@@ -60,7 +70,7 @@
* @throws SocketException thrown one windows if the static initialization
* call to WSAStartup was not successful.
*/
- TcpSocket() throw (SocketException);
+ TcpSocket() throw ( SocketException );
/**
* Construct a connected or bound socket based on given
@@ -221,36 +231,7 @@
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);
+ void checkResult( apr_status_t value ) const throw (SocketException);
};
Added: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.cpp?view=auto&rev=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.cpp Wed Jul 25 12:18:04 2007
@@ -0,0 +1,20 @@
+/*
+ * 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>
+
+_APR_LIBRARY _APR_LIBRARY::self;
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.h?view=diff&rev=559564&r1=559563&r2=559564
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Config.h Wed Jul 25 12:18:04 2007
@@ -103,8 +103,11 @@
~_APR_LIBRARY() {
apr_terminate();
}
-};
-static _APR_LIBRARY _decaf_apr;
+private:
+
+ static _APR_LIBRARY self;
+
+};
#endif /*_DECAF_UTIL_CONFIG_H_*/