myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2983] Added new `SocketPair' class.


From: Giuseppe Scrivano
Subject: [myserver-commit] [2983] Added new `SocketPair' class.
Date: Mon, 19 Jan 2009 21:50:45 +0000

Revision: 2983
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2983
Author:   gscrivano
Date:     2009-01-19 21:50:45 +0000 (Mon, 19 Jan 2009)

Log Message:
-----------
Added new `SocketPair' class.

Modified Paths:
--------------
    trunk/myserver/configure.in
    trunk/myserver/include/base/Makefile.am
    trunk/myserver/src/base/Makefile.am
    trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/include/base/socket_pair/
    trunk/myserver/include/base/socket_pair/Makefile.am
    trunk/myserver/include/base/socket_pair/socket_pair.h
    trunk/myserver/src/base/socket_pair/
    trunk/myserver/src/base/socket_pair/Makefile.am
    trunk/myserver/src/base/socket_pair/socket_pair.cpp
    trunk/myserver/tests/test_socket_pair.cpp

Modified: trunk/myserver/configure.in
===================================================================
--- trunk/myserver/configure.in 2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/configure.in 2009-01-19 21:50:45 UTC (rev 2983)
@@ -2,7 +2,7 @@
 dnl
 dnl GNU MyServer
 dnl
-dnl Copyright (C) 2002-2008 Free Software Foundation, Inc.
+dnl Copyright (C) 2002-2009 Free Software Foundation, Inc.
 dnl This program is free software; you can redistribute it and/or modify
 dnl it under the terms of the GNU General Public License as published by
 dnl the Free Software Foundation; either version 3 of the License, or
@@ -365,6 +365,7 @@
     include/base/sync/Makefile
     include/base/safetime/Makefile
     include/base/socket/Makefile
+    include/base/socket_pair/Makefile
     include/base/hash_map/Makefile
     include/base/dynamic_lib/Makefile
     include/base/process/Makefile
@@ -414,6 +415,7 @@
     src/base/sync/Makefile
     src/base/safetime/Makefile
     src/base/socket/Makefile
+    src/base/socket_pair/Makefile
     src/base/hash_map/Makefile
     src/base/dynamic_lib/Makefile
     src/base/process/Makefile

Modified: trunk/myserver/include/base/Makefile.am
===================================================================
--- trunk/myserver/include/base/Makefile.am     2009-01-16 21:27:42 UTC (rev 
2982)
+++ trunk/myserver/include/base/Makefile.am     2009-01-19 21:50:45 UTC (rev 
2983)
@@ -1,4 +1,4 @@
 baseincludedir=$(includedir)/myserver/include/base
 baseinclude_HEADERS = utility.h
-SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket ssl string sync thread xml
+SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket socket_pair ssl string 
sync thread xml
 

Added: trunk/myserver/include/base/socket_pair/Makefile.am
===================================================================
--- trunk/myserver/include/base/socket_pair/Makefile.am                         
(rev 0)
+++ trunk/myserver/include/base/socket_pair/Makefile.am 2009-01-19 21:50:45 UTC 
(rev 2983)
@@ -0,0 +1,3 @@
+socket_pair_includedir=$(includedir)/myserver/include/base/socket_pair
+socket_pair_include_HEADERS = socket_pair.h
+SUBDIRS =

Added: trunk/myserver/include/base/socket_pair/socket_pair.h
===================================================================
--- trunk/myserver/include/base/socket_pair/socket_pair.h                       
        (rev 0)
+++ trunk/myserver/include/base/socket_pair/socket_pair.h       2009-01-19 
21:50:45 UTC (rev 2983)
@@ -0,0 +1,45 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef PIPE_H
+#define PIPE_H
+
+#include "stdafx.h"
+#include <include/filter/stream.h>
+#include <string>
+
+using namespace std;
+
+
+class SocketPair : public Stream
+{
+public:
+       SocketPair ();
+       int create ();
+       FileHandle getFirstHandle ();
+       FileHandle getSecondHandle ();
+       void inverted (SocketPair&);
+  virtual int read (char* buffer, u_long len, u_long *nbr);
+  virtual int write (const char* buffer, u_long len, u_long *nbw);
+       virtual int close ();
+       void closeFirstHandle ();
+       void closeSecondHandle ();
+private:
+       FileHandle handles[2];
+};
+#endif

Modified: trunk/myserver/src/base/Makefile.am
===================================================================
--- trunk/myserver/src/base/Makefile.am 2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/src/base/Makefile.am 2009-01-19 21:50:45 UTC (rev 2983)
@@ -1,5 +1,5 @@
 lib_LIBRARIES = libbase.a
 libbase_a_SOURCES = utility.cpp
-SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket ssl string sync thread xml
+SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket socket_pair ssl string 
sync thread xml
 INCLUDES = $(all_includes)
 

Added: trunk/myserver/src/base/socket_pair/Makefile.am
===================================================================
--- trunk/myserver/src/base/socket_pair/Makefile.am                             
(rev 0)
+++ trunk/myserver/src/base/socket_pair/Makefile.am     2009-01-19 21:50:45 UTC 
(rev 2983)
@@ -0,0 +1,5 @@
+lib_LIBRARIES = libsocket_pair.a
+libsocket_pair_a_SOURCES = socket_pair.cpp
+SUBDIRS =
+INCLUDES = $(all_includes)
+

Added: trunk/myserver/src/base/socket_pair/socket_pair.cpp
===================================================================
--- trunk/myserver/src/base/socket_pair/socket_pair.cpp                         
(rev 0)
+++ trunk/myserver/src/base/socket_pair/socket_pair.cpp 2009-01-19 21:50:45 UTC 
(rev 2983)
@@ -0,0 +1,208 @@
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include "stdafx.h"
+#include <include/base/utility.h>
+#include <include/base/socket_pair/socket_pair.h>
+
+#ifdef NOT_WIN
+extern "C" {
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+}
+#else
+#include <windows.h>
+#endif
+
+#include <string>
+#include <sstream>
+
+using namespace std;
+
+
+
+SocketPair::SocketPair ()
+{
+
+}
+
+/*!
+ *Create a Socket pair.
+ *\return 0 on success.
+ */
+int SocketPair::create ()
+{
+#ifdef WIN32
+#define LOCAL_SOCKETPAIR_AF AF_INET
+#else
+#define LOCAL_SOCKETPAIR_AF AF_UNIX
+#endif
+
+  int af = LOCAL_SOCKETPAIR_AF;
+  int type = SOCK_STREAM;
+  int protocol = 0;
+
+#ifndef WIN32
+  return socketpair (af, type, protocol, (int*)handles);
+#else
+  struct sockaddr_in addr;
+  SOCKET listener;
+  int e;
+  int addrlen = sizeof(addr);
+  DWORD flags = 0;
+  
+  if (handles == 0)
+    return -1;
+
+  handles[0] = handles[1] = INVALID_SOCKET;
+  listener = socket (AF_INET, type, 0);
+
+  if (listener == INVALID_SOCKET)
+    return -1;
+
+  memset(&addr, 0, sizeof (addr));
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl (0x7f000001);
+  addr.sin_port = 0;
+
+  e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
+  if (e == SOCKET_ERROR)
+    {
+      close(listener);
+      return -1;
+    }
+  
+  e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
+  if (e == SOCKET_ERROR)
+    {
+      close (listener);
+      return -1;
+    }
+  
+  do
+    {
+      if (listen (listener, 1) == SOCKET_ERROR)
+        break;
+      if ((handles[0] = socket (AF_INET, type, 0)) == INVALID_SOCKET)
+        break;
+      if (connect(handles[0], (const struct sockaddr*) &addr, sizeof (addr)) 
== SOCKET_ERROR)
+        break;
+      if ((handles[1] = accept (listener, NULL, NULL)) == INVALID_SOCKET)
+        break;
+    
+      close (listener);
+      return 0;
+    } while (0);
+  
+  close (listener);
+  close (handles[0]);
+  close (handles[1]);
+  
+  return -1;
+#endif
+
+  return 0;
+}
+
+/*!
+ *Get the first handle used by the socket pair.
+ */
+FileHandle SocketPair::getFirstHandle ()
+{
+  return handles[0];
+}
+
+/*!
+ *Get the second handle used by the socket pair.
+ */
+FileHandle SocketPair::getSecondHandle ()
+{
+  return handles[1];
+}
+
+
+/*!
+ *Invert the current socket pair on the passed argument.
+ *The first handle will be the second handle on the inverted SocketPair
+ *and the second handle will be the first one.
+ *\param inverted It will be the inverted SocketPair.
+ */
+void SocketPair::inverted (SocketPair& inverted)
+{
+  inverted.handles[0] = handles[1];
+  inverted.handles[1] = handles[0];
+}
+
+/*!
+ *Read using the first handle.
+ *\see Stream#read.
+ */
+int SocketPair::read (char* buffer, u_long len, u_long *nbr)
+{
+  Socket sock (handles[0]);
+  return sock.read (buffer, len, nbr);
+
+}
+
+/*!
+ *Write using the first handle.
+ *\see Stream#write.
+ */
+int SocketPair::write (const char* buffer, u_long len, u_long *nbw)
+{
+  Socket sock (handles[0]);
+  return sock.write (buffer, len, nbw);
+}
+
+/*!
+ *Close both handles.
+ */
+int SocketPair::close ()
+{
+  closeFirstHandle ();
+  closeSecondHandle ();
+
+  return 0;
+}
+
+/*!
+ *Close the first handle.
+ */
+void SocketPair::closeFirstHandle ()
+{
+  Socket sock (handles[0]);
+  sock.close ();
+}
+
+/*!
+ *Close the second handle.
+ */
+void SocketPair::closeSecondHandle ()
+{
+  Socket sock (handles[1]);
+  sock.close ();
+}

Modified: trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
===================================================================
--- trunk/myserver/src/connections_scheduler/connections_scheduler.cpp  
2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/src/connections_scheduler/connections_scheduler.cpp  
2009-01-19 21:50:45 UTC (rev 2983)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -18,7 +18,8 @@
 #include <include/connections_scheduler/connections_scheduler.h>
 #include <include/server/server.h>
 
-///////////////////////////////////////////////////////////////////////////
+
+//////FIXME: Use the SocketPair class//////////////////////////
 #ifdef WIN32
 #define socket_t intptr_t
 #include <windows.h>

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2009-01-16 21:27:42 UTC (rev 2982)
+++ trunk/myserver/tests/Makefile.am    2009-01-19 21:50:45 UTC (rev 2983)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_utility.cpp  test_xml_validator.cpp test_ip.cpp 
test_plugin_info.cpp test_fork_server.cpp
+tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_socket_pair.cpp test_utility.cpp  
test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp test_fork_server.cpp
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(SSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_socket_pair.cpp
===================================================================
--- trunk/myserver/tests/test_socket_pair.cpp                           (rev 0)
+++ trunk/myserver/tests/test_socket_pair.cpp   2009-01-19 21:50:45 UTC (rev 
2983)
@@ -0,0 +1,98 @@
+/*
+ MyServer
+ Copyright (C) 2009 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <include/base/socket_pair/socket_pair.h>
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <string.h>
+
+class TestSocketPair : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestSocketPair );
+  CPPUNIT_TEST( testCreateClose );
+  CPPUNIT_TEST( testInverted );
+  CPPUNIT_TEST( testWriteRead );
+  CPPUNIT_TEST_SUITE_END();
+  
+public:
+
+  void setUp() 
+  {
+  }
+
+  void tearDown()
+  {
+  }
+
+  void testInverted ()
+  {
+    SocketPair sp;
+    SocketPair inverted;
+    sp.create ();
+    sp.inverted (inverted);
+
+    CPPUNIT_ASSERT_EQUAL (sp.getFirstHandle (), inverted.getSecondHandle ());
+    CPPUNIT_ASSERT_EQUAL (sp.getSecondHandle (), inverted.getFirstHandle ());
+
+    sp.close ();
+  }
+
+  void testCreateClose ()
+  {
+    SocketPair sp;
+    int ret = sp.create ();
+
+    CPPUNIT_ASSERT_EQUAL (ret, 0);
+
+    ret = sp.close ();
+
+    CPPUNIT_ASSERT_EQUAL (ret, 0);
+  }
+
+  void testWriteRead ()
+  {
+    char inBuffer[] = "Hello World!";
+    char outBuffer[256];
+    u_long nbw, nbr;
+    int ret;
+    SocketPair writeSock;
+    SocketPair readSock;
+
+    writeSock.create ();
+    writeSock.inverted (readSock);
+
+
+    ret = writeSock.write (inBuffer, strlen (inBuffer) + 1, &nbw);
+
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+
+    ret = readSock.read (outBuffer, 256, &nbr);
+    
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+
+
+    CPPUNIT_ASSERT_EQUAL(strcmp (inBuffer, outBuffer), 0);
+
+    writeSock.close ();
+  }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestSocketPair );






reply via email to

[Prev in Thread] Current Thread [Next in Thread]