[crisscross commit] r858 - in trunk: examples examples/SimpleTCPClient source source/crisscross

0 views
Skip to first unread message

codesite...@google.com

unread,
Oct 15, 2008, 12:42:06 AM10/15/08
to crisscr...@googlegroups.com
Author: steven.noonan
Date: Tue Oct 14 21:40:41 2008
New Revision: 858

Added:
trunk/examples/SimpleTCPClient/
trunk/examples/SimpleTCPClient/CMakeLists.txt
trunk/examples/SimpleTCPClient/header.cpp
trunk/examples/SimpleTCPClient/header.h
trunk/examples/SimpleTCPClient/main.cpp
Modified:
trunk/examples/CMakeLists.txt
trunk/source/core_socket.cpp
trunk/source/crisscross/core_socket.h
trunk/source/crisscross/error.h
trunk/source/crisscross/tcpsocket.h
trunk/source/crisscross/udpsocket.h
trunk/source/crisscross/universal_include.h
trunk/source/error.cpp
trunk/source/tcpsocket.cpp
trunk/source/udpsocket.cpp

Log:
Socket fixes, SimpleTCPClient added.


Modified: trunk/examples/CMakeLists.txt
==============================================================================
--- trunk/examples/CMakeLists.txt (original)
+++ trunk/examples/CMakeLists.txt Tue Oct 14 21:40:41 2008
@@ -3,4 +3,5 @@
add_subdirectory (CheckMark)
add_subdirectory (EmptyProject)
add_subdirectory (GenPrime)
+add_subdirectory (SimpleTCPClient)
add_subdirectory (SortBenchmark)

Added: trunk/examples/SimpleTCPClient/CMakeLists.txt
==============================================================================
--- (empty file)
+++ trunk/examples/SimpleTCPClient/CMakeLists.txt Tue Oct 14 21:40:41 2008
@@ -0,0 +1,4 @@
+add_executable(SimpleTCPClient header.cpp main.cpp)
+add_precompiled_header(SimpleTCPClient
${CMAKE_CURRENT_SOURCE_DIR}/header.h)
+
+target_link_libraries (SimpleTCPClient CrissCross)

Added: trunk/examples/SimpleTCPClient/header.cpp
==============================================================================
--- (empty file)
+++ trunk/examples/SimpleTCPClient/header.cpp Tue Oct 14 21:40:41 2008
@@ -0,0 +1,12 @@
+/*
+ * CrissCross
+ * A multi-purpose cross-platform library.
+ *
+ * A product of IO.IN Research.
+ *
+ * (c) 2006-2008 Steven Noonan.
+ * Licensed under the New BSD License.
+ *
+ */
+
+#include "header.h"

Added: trunk/examples/SimpleTCPClient/header.h
==============================================================================
--- (empty file)
+++ trunk/examples/SimpleTCPClient/header.h Tue Oct 14 21:40:41 2008
@@ -0,0 +1,17 @@
+/*
+ * CrissCross
+ * A multi-purpose cross-platform library.
+ *
+ * A product of IO.IN Research.
+ *
+ * (c) 2006-2008 Steven Noonan.
+ * Licensed under the New BSD License.
+ *
+ */
+
+#ifndef __included_header_h
+#define __included_header_h
+
+#include <crisscross/crisscross.h>
+
+#endif

Added: trunk/examples/SimpleTCPClient/main.cpp
==============================================================================
--- (empty file)
+++ trunk/examples/SimpleTCPClient/main.cpp Tue Oct 14 21:40:41 2008
@@ -0,0 +1,73 @@
+/*
+ * CrissCross
+ * A multi-purpose cross-platform library.
+ *
+ * A product of IO.IN Research.
+ *
+ * (c) 2006-2008 Steven Noonan.
+ * Licensed under the New BSD License.
+ *
+ */
+
+#include "header.h"
+
+using namespace CrissCross;
+using namespace CrissCross::IO;
+using namespace CrissCross::Network;
+using namespace std;
+
+Console *console = NULL;
+
+int RunApplication(int argc, char * *argv)
+{
+ console = new Console();
+
+ /* Begin your application here. */
+ int cc_err;
+
+ TCPSocket * sock = new TCPSocket();
+ console->WriteLine("Connecting...");
+ cc_err = sock->Connect("www.example.com", 80);
+ if (cc_err == CC_ERR_WOULD_BLOCK)
+ console->WriteLine("Non-blocking sockets are enabled. Waiting for
connection...");
+ while(sock->State() == SOCKET_STATE_CONNECTING);
+ if (sock->State() != SOCKET_STATE_CONNECTED) {
+ console->WriteLine("Connection failed.");
+ return 1;
+ } else
+ console->WriteLine("Connection seems OK.");
+ console->Write("Requesting http://www.example.com/... ");
+ cc_err = sock->Send("GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n");
+ if (cc_err < 1)
+ console->WriteLine("Failed.");
+ else
+ console->WriteLine("OK");
+
+ while(sock->State() == SOCKET_STATE_CONNECTED) {
+ string in;
+ cc_err = sock->Read(in);
+ if (cc_err == 0)
+ {
+ console->WriteLine(in);
+ break;
+ } else {
+ if (cc_err != CC_ERR_WOULD_BLOCK)
+ console->WriteLine("Read error %d (%s)", cc_err,
GetErrorDescription(cc_err));
+ System::ThreadSleep(10);
+ }
+ }
+
+ sock->Close();
+
+ delete sock;
+
+ return 0;
+ /* End your application here. */
+
+#ifdef TARGET_OS_WINDOWS
+ system("pause");
+#endif
+
+ delete console;
+ return 0;
+}

Modified: trunk/source/core_socket.cpp
==============================================================================
--- trunk/source/core_socket.cpp (original)
+++ trunk/source/core_socket.cpp Tue Oct 14 21:40:41 2008
@@ -49,7 +49,7 @@
{
CoreSocket::CoreSocket()
{
- CrissCross::Errors retval = __initialise_network();
+ int retval = __initialise_network();
m_calledInitialise = 1;
CoreAssert(retval == CC_ERR_NONE);
memset(&m_sock, 0, sizeof(socket_t));
@@ -75,9 +75,9 @@
__cleanup_network();
}

- CrissCross::Errors CoreSocket::Close()
+ int CoreSocket::Close()
{
- if (m_sock == INVALID_SOCKET) return CC_ERR_ENOTSOCK;
+ if (m_sock == INVALID_SOCKET) return CC_ERR_NOT_SOCKET;

/* Close the socket. */
#ifdef TARGET_OS_WINDOWS
@@ -150,29 +150,42 @@

int CoreSocket::Read(std::string &_output)
{
- if (m_sock == INVALID_SOCKET) return CC_ERR_ENOTSOCK;
+ if (m_sock == INVALID_SOCKET) return CC_ERR_NOT_SOCKET;
+ if (!IsReadable()) return CC_ERR_WOULD_BLOCK;
+
+ _output = "";
+
+ int errbefore = GetError();

char *buf = new char[m_bufferSize];
- memset(buf, 0, m_bufferSize);
- int ret = 0, recvlen = 0;
-
- recvlen = recv(m_sock, buf, m_bufferSize - 1, 0);
- ret = errno;
-
- _output = std::string(buf);
+ int recvlen = 0;
+ bool receivedData = false;
+ do {
+ memset(buf, 0, m_bufferSize);
+ recvlen = recv(m_sock, buf, 1, MSG_PEEK);
+ if (recvlen > 0) {
+ recvlen = recv(m_sock, buf, m_bufferSize - 1, 0);
+ _output += std::string(buf);
+ receivedData = true;
+ }
+ } while (recvlen > 0);

delete [] buf;
+
+ int err = GetError();
+
+ /* The error wasn't triggered by what we did here */
+ if (err == errbefore)
+ err = CC_ERR_NONE;

- return (recvlen == 0) ? CC_ERR_NONE : errno;
+ return err;
}

int CoreSocket::Read(char *_output, unsigned int *_len)
{
/* Sanity checks. */
- if (m_sock == INVALID_SOCKET) return CC_ERR_ENOTSOCK;
-
- if (_len == NULL) return CC_ERR_BADPARAMETER;
-
+ if (m_sock == INVALID_SOCKET) return CC_ERR_NOT_SOCKET;
+ if (_len == NULL || *_len < 1) return CC_ERR_BADPARAMETER;
if (_output == NULL) return CC_ERR_BADPARAMETER;

/* Set up a buffer. */
@@ -191,7 +204,7 @@
return CC_ERR_NONE;
}

- CrissCross::Errors CoreSocket::GetError() const
+ int CoreSocket::GetError() const
{
CoreAssert(m_sock != 0);
int retval = 0;
@@ -206,16 +219,17 @@
if (retval == WSAEWOULDBLOCK || retval == 0) {
getsockopt(m_sock, SOL_SOCKET, SO_ERROR, (char *)&ret, (socklen_t
*)&retsize);
if (ret != 0)
- return GetErrorNumber(ret);
+ return TranslateError(ret);
}

#endif
- return GetErrorNumber(retval);
+ return TranslateError(retval);
}

int CoreSocket::Send(const void *_data, size_t _length)
{
- CoreAssert(m_sock != 0);
+ if (m_sock == INVALID_SOCKET) return CC_ERR_NOT_SOCKET;
+ if (!IsWritable()) return CC_ERR_WOULD_BLOCK;

int sent = 0;
#ifdef PACKET_DEBUG
@@ -232,29 +246,13 @@
delete [] temp_buf;
#endif
sent = send(m_sock, (const char *)_data, (int)_length, 0);
+
return sent;
}

int CoreSocket::Send(std::string _data)
{
- CoreAssert(m_sock != 0);
-
- int sent = 0;
-#ifdef PACKET_DEBUG
- char *temp_buf = new char[m_bufferSize];
- memset(temp_buf, 0, m_bufferSize);
- char *p = temp_buf, *d = (char *)_data.c_str();
- while (*d != '\x0') {
- if (!(*d == '\n' || *d == '\r'))
- *p = *d;
-
- p++; d++;
- }
- fprintf(stdout, "<<< '%s'\n", temp_buf);
- delete [] temp_buf;
-#endif
- sent = send(m_sock, _data.c_str(), (int)_data.size(), 0);
- return sent;
+ return Send(_data.c_str(), _data.size());
}

bool CoreSocket::IsReadable() const
@@ -270,7 +268,7 @@
FD_ZERO(&read);
FD_SET(m_sock, &read);

- CrissCross::Errors errbefore = GetError(), errafter = CC_ERR_NONE;
+ int errbefore = GetError(), errafter;

/* Select to check if it's readable. */
ret = select(m_sock + 1, &read, NULL, NULL, &timeout);
@@ -280,7 +278,7 @@
/* ret < 0 is error */
/* ret == 0 is in progress */
/* ret > 0 is success */
- if (ret < 0 || (errafter && errafter != errbefore && errafter !=
CC_ERR_EINPROGRESS && errafter != CC_ERR_TRY_AGAIN)) {
+ if (ret < 0 || (errafter && errafter != errbefore && errafter !=
CC_ERR_WOULD_BLOCK && errafter != TRY_AGAIN)) {
/* Bugger. Operation timed out. */
m_state = SOCKET_STATE_ERROR;
return false;
@@ -305,7 +303,7 @@
FD_ZERO(&write);
FD_SET(m_sock, &write);

- CrissCross::Errors errbefore = GetError(), errafter = CC_ERR_NONE;
+ int errbefore = GetError(), errafter;

/* Select to check if it's readable. */
ret = select(m_sock + 1, NULL, &write, NULL, &timeout);
@@ -315,7 +313,7 @@
/* ret < 0 is error */
/* ret == 0 is in progress */
/* ret > 0 is success */
- if (ret < 0 || (errafter && errafter != errbefore && errafter !=
CC_ERR_EINPROGRESS && errafter != CC_ERR_TRY_AGAIN)) {
+ if (ret < 0 || (errafter && errafter != errbefore && errafter !=
CC_ERR_WOULD_BLOCK && errafter != TRY_AGAIN)) {
/* Bugger. Operation timed out. */
m_state = SOCKET_STATE_ERROR;
return false;

Modified: trunk/source/crisscross/core_socket.h
==============================================================================
--- trunk/source/crisscross/core_socket.h (original)
+++ trunk/source/crisscross/core_socket.h Tue Oct 14 21:40:41 2008
@@ -100,13 +100,13 @@
* it is automatically called in the destructor.
* \return Currently always returns CC_ERR_NONE.
*/
- virtual CrissCross::Errors Close();
+ virtual int Close();

/*! \brief Get the error value for the socket. */
/*!
* \return The error value for the socket.
*/
- virtual CrissCross::Errors GetError() const;
+ virtual int GetError() const;

/*! \brief Fetches the IP address of the remote host. */
/*!
@@ -175,7 +175,7 @@
* \return If the return value is greater than zero, it is an 'errno'
* value. If it is less than zero, it is a socketError value.
*/
- virtual CrissCross::Errors Listen(unsigned short _port) = 0;
+ virtual int Listen(unsigned short _port) = 0;

/*! \brief Reads a block of data with a specified maximum size. */
/*!

Modified: trunk/source/crisscross/error.h
==============================================================================
--- trunk/source/crisscross/error.h (original)
+++ trunk/source/crisscross/error.h Tue Oct 14 21:40:41 2008
@@ -22,159 +22,25 @@
*/
enum Errors
{
- CC_ERR_BADPARAMETER = -1000, /*! \brief< One of the
parameters passed to the function was not valid. */
- CC_ERR_NOT_IMPLEMENTED,
- CC_ERR_FILE_OPEN, /*! \brief< The specified
file could not be opened. */
- CC_ERR_WRITE, /*! \brief< The buffer
could not be written to. */
- CC_ERR_INTERNAL, /*! \brief< A call inside
the function failed. */
- CC_ERR_INVALID_CALL = -500, /*! \brief< The function
call was invalid. */
- CC_ERR_INVALID_BUFFER = -499, /*! \brief< The buffer was
NULL (file not opened?) */
- CC_ERR_INCOMPATIBLE_BUFFER = -498, /*! \brief< The buffer was
incompatible (i.e. reading unicode data on a non-unicode buffer). */
- CC_ERR_EPERM,
- CC_ERR_ENOENT,
- CC_ERR_ESRCH,
- CC_ERR_EINTR,
- CC_ERR_EIO,
- CC_ERR_ENXIO,
- CC_ERR_E2BIG,
- CC_ERR_ENOEXEC,
- CC_ERR_EBADF,
- CC_ERR_ECHILD,
- CC_ERR_EAGAIN,
- CC_ERR_ENOMEM,
- CC_ERR_EACCES,
- CC_ERR_EFAULT,
- CC_ERR_ENOTBLK,
- CC_ERR_EBUSY,
- CC_ERR_EEXIST,
- CC_ERR_EXDEV,
- CC_ERR_ENODEV,
- CC_ERR_ENOTDIR,
- CC_ERR_EISDIR,
- CC_ERR_EINVAL,
- CC_ERR_ENFILE,
- CC_ERR_EMFILE,
- CC_ERR_ENOTTY,
- CC_ERR_ETXTBSY,
- CC_ERR_EFBIG,
- CC_ERR_ENOSPC,
- CC_ERR_ESPIPE,
- CC_ERR_EROFS,
- CC_ERR_EMLINK,
- CC_ERR_EPIPE,
- CC_ERR_EDOM,
- CC_ERR_ERANGE,
- CC_ERR_EDEADLK,
- CC_ERR_ENAMETOOLONG,
- CC_ERR_ENOLCK,
- CC_ERR_ENOSYS,
- CC_ERR_ENOTEMPTY,
- CC_ERR_ELOOP,
- CC_ERR_EWOULDBLOCK,
- CC_ERR_ENOMSG,
- CC_ERR_EIDRM,
- CC_ERR_ECHRNG,
- CC_ERR_EL2NSYNC,
- CC_ERR_EL3HLT,
- CC_ERR_EL3RST,
- CC_ERR_ELNRNG,
- CC_ERR_EUNATCH,
- CC_ERR_ENOCSI,
- CC_ERR_EL2HLT,
- CC_ERR_EBADE,
- CC_ERR_EBADR,
- CC_ERR_EXFULL,
- CC_ERR_ENOANO,
- CC_ERR_EBADRQC,
- CC_ERR_EBADSLT,
- CC_ERR_EDEADLOCK = CC_ERR_EDEADLK,
- CC_ERR_EBFONT,
- CC_ERR_ENOSTR,
- CC_ERR_ENODATA,
- CC_ERR_ETIME,
- CC_ERR_ENOSR,
- CC_ERR_ENONET,
- CC_ERR_ENOPKG,
- CC_ERR_EREMOTE,
- CC_ERR_ENOLINK,
- CC_ERR_EADV,
- CC_ERR_ESRMNT,
- CC_ERR_ECOMM,
- CC_ERR_EPROTO,
- CC_ERR_EMULTIHOP,
- CC_ERR_EDOTDOT,
- CC_ERR_EBADMSG,
- CC_ERR_EOVERFLOW,
- CC_ERR_ENOTUNIQ,
- CC_ERR_EBADFD,
- CC_ERR_EREMCHG,
- CC_ERR_ELIBACC,
- CC_ERR_ELIBBAD,
- CC_ERR_ELIBSCN,
- CC_ERR_ELIBMAX,
- CC_ERR_ELIBEXEC,
- CC_ERR_EILSEQ,
- CC_ERR_ERESTART,
- CC_ERR_ESTRPIPE,
- CC_ERR_EUSERS,
- CC_ERR_ENOTSOCK,
- CC_ERR_EDESTADDRREQ,
- CC_ERR_EMSGSIZE,
- CC_ERR_EPROTOTYPE,
- CC_ERR_ENOPROTOOPT,
- CC_ERR_EPROTONOSUPPORT,
- CC_ERR_ESOCKTNOSUPPORT,
- CC_ERR_EOPNOTSUPP,
- CC_ERR_EPFNOSUPPORT,
- CC_ERR_EAFNOSUPPORT,
- CC_ERR_EADDRINUSE,
- CC_ERR_EADDRNOTAVAIL,
- CC_ERR_ENETDOWN,
- CC_ERR_ENETUNREACH,
- CC_ERR_ENETRESET,
- CC_ERR_ECONNABORTED,
- CC_ERR_ECONNRESET,
- CC_ERR_ENOBUFS,
- CC_ERR_EISCONN,
- CC_ERR_ENOTCONN,
- CC_ERR_ESHUTDOWN,
- CC_ERR_ETOOMANYREFS,
- CC_ERR_ETIMEDOUT,
- CC_ERR_ECONNREFUSED,
- CC_ERR_EHOSTDOWN,
- CC_ERR_EHOSTUNREACH,
- CC_ERR_EALREADY,
- CC_ERR_EINPROGRESS,
- CC_ERR_ESTALE,
- CC_ERR_EUCLEAN,
- CC_ERR_ENOTNAM,
- CC_ERR_ENAVAIL,
- CC_ERR_EISNAM,
- CC_ERR_EREMOTEIO,
- CC_ERR_EDQUOT,
- CC_ERR_ENOMEDIUM,
- CC_ERR_EMEDIUMTYPE,
- CC_ERR_ECANCELED,
- CC_ERR_ENOKEY,
- CC_ERR_EKEYEXPIRED,
- CC_ERR_EKEYREVOKED,
- CC_ERR_EKEYREJECTED,
- CC_ERR_EOWNERDEAD,
- CC_ERR_ENOTRECOVERABLE,
- CC_ERR_NO_RECOVERY,
- CC_ERR_HOST_NOT_FOUND,
- CC_ERR_TRY_AGAIN,
- CC_ERR_NO_DATA,
- CC_ERR_NO_SOCK,
- CC_ERR_SOCK_BUSY,
- CC_ERR_NONE = 0 /*! \brief< Everything is
fine, nothing is ruined. */
+ CC_ERR_NO_SOCK = -11, /*!< \brief No connection was
accepted. */
+ CC_ERR_WOULD_BLOCK = -10, /*!< \brief Non-blocking I/O
is enabled and the requested operation would block. */
+ CC_ERR_NOT_SOCKET = -9, /*!< \brief An operation was
attempted on an invalid socket. */
+ CC_ERR_BADPARAMETER = -8, /*!< \brief One of the
parameters passed to the function was not valid. */
+ CC_ERR_NOT_IMPLEMENTED = -7,
+ CC_ERR_FILE_OPEN = -6, /*!< \brief The specified
file could not be opened. */
+ CC_ERR_WRITE = -5, /*!< \brief The buffer could
not be written to. */
+ CC_ERR_INTERNAL = -4, /*!< \brief A call inside the
function failed. */
+ CC_ERR_INVALID_CALL = -3, /*!< \brief The function call
was invalid. */
+ CC_ERR_INVALID_BUFFER = -2, /*!< \brief The buffer was
NULL (file not opened?) */
+ CC_ERR_INCOMPATIBLE_BUFFER = -1, /*!< \brief The buffer was
incompatible (i.e. reading unicode data on a non-unicode buffer). */
+ CC_ERR_NONE = 0 /*!< \brief Everything is
fine, nothing is ruined. */
};

/*! \brief Converts an errno number to a CrissCross::Errors value. */
- CrissCross::Errors GetErrorNumber(int why);
+ int TranslateError(int why);

/*! \brief Gets the name of the CrissCross::Errors value. */
- const char *GetErrorDescription(CrissCross::Errors why);
+ const char *GetErrorDescription(int why);
}

#endif

Modified: trunk/source/crisscross/tcpsocket.h
==============================================================================
--- trunk/source/crisscross/tcpsocket.h (original)
+++ trunk/source/crisscross/tcpsocket.h Tue Oct 14 21:40:41 2008
@@ -46,21 +46,21 @@
* \return A pointer to a newly created TCPSocket instance for the new
* connection. If no incoming connections are pending, this returns
NULL.
*/
- CrissCross::Errors Accept(TCPSocket * *_socket);
+ int Accept(TCPSocket * *_socket);

/*! \brief Establishes an outbound connection to the specified address
and port. */
/*!
* \param _address The remote address to connect to. Can be a
hostname, as it will be resolved by gethostbyname().
* \param _port The remote port to connect to.
*/
- CrissCross::Errors Connect(const char *_address, unsigned short _port);
+ int Connect(const char *_address, unsigned short _port);

/*! \brief Listens for connections on the specified port. */
/*!
* You need to Accept() connections after calling this.
* \param _port The local port to listen on.
*/
- CrissCross::Errors Listen(unsigned short _port);
+ int Listen(unsigned short _port);

/*! \brief Fetch the state of the socket. */
/*!

Modified: trunk/source/crisscross/udpsocket.h
==============================================================================
--- trunk/source/crisscross/udpsocket.h (original)
+++ trunk/source/crisscross/udpsocket.h Tue Oct 14 21:40:41 2008
@@ -47,14 +47,14 @@
* \param _port The remote port to bind to.
* \return Due to the use of Berkley sockets, this returns zero on
success, and a nonzero value on failure.
*/
- CrissCross::Errors Bind(const char *_address, unsigned short _port);
+ int Bind(const char *_address, unsigned short _port);

/*! \brief Binds an inbound UDP/IP socket on the specified port. */
/*!
* \param _port The port to bind.
* \return Due to the use of Berkley sockets, this returns zero on
success, and a nonzero value on failure.
*/
- CrissCross::Errors Listen(unsigned short _port);
+ int Listen(unsigned short _port);
};
}
}

Modified: trunk/source/crisscross/universal_include.h
==============================================================================
--- trunk/source/crisscross/universal_include.h (original)
+++ trunk/source/crisscross/universal_include.h Tue Oct 14 21:40:41 2008
@@ -59,9 +59,6 @@
//#define ENABLE_CRASHREPORTS /* Enables XCrashReports on Windows. */
/* #define DISABLE_DEPRECATED_CODE */ /* This will be enabled by default
in a future release */

-/* Enables non-blocking sockets. */
-#define ENABLE_NONBLOCKING
-
/* NOTE: By disabling this line, you will not be in compliance with the
New BSD License. */
/* If you disable this line, you must display the copyright notice in
the program */
/* elsewhere. */

Modified: trunk/source/error.cpp
==============================================================================
--- trunk/source/error.cpp (original)
+++ trunk/source/error.cpp Tue Oct 14 21:40:41 2008
@@ -22,7 +22,7 @@
{
int w;
const char *s;
- CrissCross::Errors e;
+ int e;
};

#if defined (TARGET_OS_WINDOWS)
@@ -66,7 +66,6 @@
{WSAEHOSTDOWN, "WSAEHOSTDOWN", CC_ERR_EHOSTDOWN},
{WSAEHOSTUNREACH, "WSAEHOSTUNREACH", CC_ERR_EHOSTUNREACH},
{WSAENOTEMPTY, "WSAENOTEMPTY", CC_ERR_ENOTEMPTY},
- /*{WSAEPROCLIM, "WSAEPROCLIM", CC_ERR_EPROCLIM}, // MISSING? */
{WSAEUSERS, "WSAEUSERS", CC_ERR_EUSERS},
{WSAEDQUOT, "WSAEDQUOT", CC_ERR_EDQUOT},
{WSAESTALE, "WSAESTALE", CC_ERR_ESTALE},
@@ -81,26 +80,17 @@
const struct tl errmap [] =
{
#if !defined (TARGET_OS_NDSFIRMWARE)
+ /*
{NO_DATA, "NO_DATA", CC_ERR_NO_DATA},
{HOST_NOT_FOUND, "HOST_NOT_FOUND", CC_ERR_HOST_NOT_FOUND},
{NO_RECOVERY, "NO_RECOVERY", CC_ERR_NO_RECOVERY},
{TRY_AGAIN, "TRY_AGAIN", CC_ERR_TRY_AGAIN},
+ */
#endif
- {EINTR, "EINTR", CC_ERR_EINTR},
- {EWOULDBLOCK, "EWOULDBLOCK", CC_ERR_EWOULDBLOCK},
- {EINPROGRESS, "EINPROGRESS", CC_ERR_EINPROGRESS},
- {EALREADY, "EALREADY", CC_ERR_EALREADY},
- {ENOTSOCK, "ENOTSOCK", CC_ERR_ENOTSOCK},
- {EDESTADDRREQ, "EDESTADDRREQ", CC_ERR_EDESTADDRREQ},
- {EMSGSIZE, "EMSGSIZE", CC_ERR_EMSGSIZE},
- {EPROTOTYPE, "EPROTOTYPE", CC_ERR_EPROTOTYPE},
- {ENOPROTOOPT, "ENOPROTOOPT", CC_ERR_ENOPROTOOPT},
- {EPROTONOSUPPORT, "EPROTONOSUPPORT", CC_ERR_EPROTONOSUPPORT},
- {ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT", CC_ERR_ESOCKTNOSUPPORT},
- {EOPNOTSUPP, "EOPNOTSUPP", CC_ERR_EOPNOTSUPP},
- {EPFNOSUPPORT, "EPFNOSUPPORT", CC_ERR_EPFNOSUPPORT},
- {EAFNOSUPPORT, "EAFNOSUPPORT", CC_ERR_EAFNOSUPPORT},
- {EADDRINUSE, "EADDRINUSE", CC_ERR_EADDRINUSE},
+ {EWOULDBLOCK, "EWOULDBLOCK", CC_ERR_WOULD_BLOCK},
+ {EINPROGRESS, "EINPROGRESS", CC_ERR_WOULD_BLOCK},
+ /*
+ {EADDRINUSE, "EADDRINUSE", CC_ERR_ADDR_IN_USE},
{EADDRNOTAVAIL, "EADDRNOTAVAIL", CC_ERR_EADDRNOTAVAIL},
{ENETDOWN, "ENETDOWN", CC_ERR_ENETDOWN},
{ENETUNREACH, "ENETUNREACH", CC_ERR_ENETUNREACH},
@@ -119,37 +109,39 @@
{EHOSTDOWN, "EHOSTDOWN", CC_ERR_EHOSTDOWN},
{EHOSTUNREACH, "EHOSTUNREACH", CC_ERR_EHOSTUNREACH},
{ENOTEMPTY, "ENOTEMPTY", CC_ERR_ENOTEMPTY},
- /*{EPROCLIM, "EPROCLIM", CC_ERR_EPROCLIM}, // MISSING? */
{EUSERS, "EUSERS", CC_ERR_EUSERS},
{EDQUOT, "EDQUOT", CC_ERR_EDQUOT},
{ESTALE, "ESTALE", CC_ERR_ESTALE},
{EREMOTE, "EREMOTE", CC_ERR_EREMOTE},
{EINVAL, "EINVAL", CC_ERR_EINVAL},
{EFAULT, "EFAULT", CC_ERR_EFAULT},
+ */
{0, "UNKNOWN_ERROR", CC_ERR_INTERNAL},
{0, "NO_ERROR", CC_ERR_NONE}
};
#endif

- CrissCross::Errors GetErrorNumber(int why)
+ int TranslateError(int why)
{
for (int i = 0; errmap[i].s != NULL; ++i)
if (why == errmap[i].w)
return errmap[i].e;
-
- printf("CrissCross: Error %d (%s) couldn't be mapped to a CrissCross
internal error number.\n", why, strerror(why));
-
- return CC_ERR_INTERNAL;
+ return why;
}

- const char *GetErrorDescription(CrissCross::Errors why)
+ const char *GetErrorDescription(int why)
{
+ static const char *unknown = "Unknown Error";
+
#if !defined(TARGET_OS_WINDOWS) && !defined(TARGET_COMPILER_CYGWIN)
- static char buffer[128];
- memset(buffer, 0, sizeof(buffer));
- strerror_r(why, buffer, 128);
- if (strlen(buffer))
- return buffer;
+ if (why >= 0)
+ {
+ static char buffer[128];
+ memset(buffer, 0, sizeof(buffer));
+ strerror_r(why, buffer, 128);
+ if (strlen(buffer))
+ return buffer;
+ }

#endif

@@ -157,6 +149,6 @@
if (why == errmap[i].e)
return errmap[i].s;

- return NULL;
+ return unknown;
}
}

Modified: trunk/source/tcpsocket.cpp
==============================================================================
--- trunk/source/tcpsocket.cpp (original)
+++ trunk/source/tcpsocket.cpp Tue Oct 14 21:40:41 2008
@@ -51,7 +51,7 @@
{
}

- CrissCross::Errors TCPSocket::Accept(TCPSocket * *_socket)
+ int TCPSocket::Accept(TCPSocket * *_socket)
{
/* We're forced to accept any incoming connection, unfortunately. */
socket_t sock = accept(m_sock, 0, 0);
@@ -61,7 +61,7 @@
/* Set up the typical transmission attributes. */
SetAttributes(sock);

-#if defined (ENABLE_NONBLOCKING)
+#if 0
unsigned long arg = 1;

/* Non-blocking I/O, if possible. Ignore any errors. */
@@ -70,7 +70,6 @@
#else
ioctl(m_sock, FIONBIO, &arg);
#endif
-
#endif

/* Create a new wrapper for our socket. */
@@ -88,7 +87,7 @@
return CC_ERR_NO_SOCK;
}

- CrissCross::Errors TCPSocket::Connect(const char *_address, unsigned
short _port)
+ int TCPSocket::Connect(const char *_address, unsigned short _port)
{
struct sockaddr_in sin;
struct hostent *host;
@@ -104,9 +103,9 @@
return GetError();

/* Set up the typical transmission attributes. */
- /* SetAttributes ( m_sock ); */
+ //SetAttributes ( m_sock );

-#if defined (ENABLE_NONBLOCKING)
+#if 0
unsigned long arg = 1;

/* Non-blocking I/O, if possible. Ignore any errors. */
@@ -115,7 +114,6 @@
#else
ioctl(m_sock, FIONBIO, &arg);
#endif
-
#endif

/* Resolve the IP of the host we're trying to connect to. */
@@ -130,12 +128,12 @@

/* Attempt a connection. */
if (connect(m_sock, (( struct sockaddr * )&sin), sizeof(sin)) != 0) {
- CrissCross::Errors err = GetError();
+ int err = GetError();

/* If this is a non-blocking socket, we need to handle appropriately.
*/
- if (err == CC_ERR_EWOULDBLOCK || err == CC_ERR_EINPROGRESS) {
+ if (err == CC_ERR_WOULD_BLOCK) {
m_state = SOCKET_STATE_CONNECTING;
- return CC_ERR_EINPROGRESS;
+ return CC_ERR_WOULD_BLOCK;
} else {
m_state = SOCKET_STATE_ERROR;
/* Close the connection, it failed. */
@@ -152,12 +150,12 @@
return CC_ERR_NONE;
}

- CrissCross::Errors TCPSocket::Listen(unsigned short _port)
+ int TCPSocket::Listen(unsigned short _port)
{
struct sockaddr_in sin;

/* Verify our socket isn't in use. */
- if (m_sock != INVALID_SOCKET) return CC_ERR_SOCK_BUSY;
+ if (m_sock != INVALID_SOCKET) return CC_ERR_INVALID_CALL;

/* Set up our sockaddr_in */
memset(&sin, 0, sizeof(sin));
@@ -174,8 +172,8 @@

/* Set up the typical transmission attributes. */
SetAttributes(m_sock);
-
-#if defined (ENABLE_NONBLOCKING)
+
+#if 0
unsigned long arg = 1;

/* Non-blocking I/O, if possible. Ignore any errors. */
@@ -184,13 +182,12 @@
#else
ioctl(m_sock, FIONBIO, &arg);
#endif
-
#endif

/* Bind our socket to our given port number. */
if (bind(m_sock, (sockaddr *)&sin, sizeof(sin)) != 0) {
/* Bind failure, for some reason. */
- CrissCross::Errors err = GetError();
+ int err = GetError();

#ifdef TARGET_OS_WINDOWS
closesocket(m_sock);
@@ -232,6 +229,14 @@
err = setsockopt(_socket, SOL_SOCKET, SO_LINGER,
(char *)&linger_opts, optlen);
if (err == -1) return errno;
+
+ /* SO_REUSEADDR */
+#ifndef TARGET_OS_WINDOWS
+ optval = 1;
+ optlen = sizeof(optval);
+ setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR,
+ (char*)&optval, optlen);
+#endif

/* SO_KEEPALIVE */
#ifdef TARGET_OS_WINDOWS

Modified: trunk/source/udpsocket.cpp
==============================================================================
--- trunk/source/udpsocket.cpp (original)
+++ trunk/source/udpsocket.cpp Tue Oct 14 21:40:41 2008
@@ -45,12 +45,12 @@
{
}

- CrissCross::Errors UDPSocket::Bind(const char *_address, unsigned short
_port)
+ int UDPSocket::Bind(const char *_address, unsigned short _port)
{
struct sockaddr_in sin;
struct hostent *host;

- if (m_sock != INVALID_SOCKET) return CC_ERR_ENOTSOCK;
+ if (m_sock != INVALID_SOCKET) return CC_ERR_NOT_SOCKET;

m_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (m_sock == INVALID_SOCKET)
@@ -67,7 +67,7 @@
sin.sin_port = htons(_port);

if (connect(m_sock, (( struct sockaddr * )&sin), sizeof(sin)) != 0) {
- CrissCross::Errors err = GetError();
+ int err = GetError();

/* Close the connection, it failed. */
#ifdef TARGET_OS_WINDOWS
@@ -87,11 +87,11 @@
return 0;
}

- CrissCross::Errors UDPSocket::Listen(unsigned short _port)
+ int UDPSocket::Listen(unsigned short _port)
{
struct sockaddr_in sin;

- if (m_sock != INVALID_SOCKET) return CC_ERR_ENOTSOCK;
+ if (m_sock != INVALID_SOCKET) return CC_ERR_NOT_SOCKET;

memset(&sin, 0, sizeof(sin));

@@ -105,7 +105,7 @@

SetAttributes(m_sock);

-#if defined (ENABLE_NONBLOCKING)
+#if 0
unsigned long arg = 1;
#if defined (TARGET_OS_WINDOWS)
ioctlsocket(m_sock, FIONBIO, &arg);
@@ -115,7 +115,7 @@
#endif

if (bind(m_sock, (sockaddr *)&sin, sizeof(sin)) != 0) {
- CrissCross::Errors err = GetError();
+ int err = GetError();

/* Close the connection, it failed. */
#ifdef TARGET_OS_WINDOWS

Reply all
Reply to author
Forward
0 new messages