[svn:parrot] r37827 - in trunk: examples/io include/parrot src/io

Infinoid at svn.parrot.org Infinoid at svn.parrot.org
Tue Mar 31 16:34:26 UTC 2009


Author: Infinoid
Date: Tue Mar 31 16:34:25 2009
New Revision: 37827
URL: https://trac.parrot.org/parrot/changeset/37827

Log:
Apply patch from bacek++ in TT #518:
Add some constants for sockets

Modified:
   trunk/examples/io/httpd.pir
   trunk/include/parrot/io.h
   trunk/src/io/socket_unix.c
   trunk/src/io/socket_win32.c

Modified: trunk/examples/io/httpd.pir
==============================================================================
--- trunk/examples/io/httpd.pir	Tue Mar 31 15:40:48 2009	(r37826)
+++ trunk/examples/io/httpd.pir	Tue Mar 31 16:34:25 2009	(r37827)
@@ -93,6 +93,7 @@
 
 .include "stat.pasm"
 .include 'except_types.pasm'
+.include 'socket.pasm'
 
 .sub main :main
     .local pmc listener, work, fp
@@ -112,7 +113,7 @@
 
     # TODO provide sys/socket constants
     listener = new 'Socket'
-    listener.'socket'(2, 1, 6)	# PF_INET, SOCK_STREAM, tcp
+    listener.'socket'(.PIO_PF_INET, .PIO_SOCK_STREAM, .PIO_PROTO_TCP)	# PF_INET, SOCK_STREAM, tcp
     unless listener goto ERR_NO_SOCKET
 
     # Pack a sockaddr_in structure with IP and port

Modified: trunk/include/parrot/io.h
==============================================================================
--- trunk/include/parrot/io.h	Tue Mar 31 15:40:48 2009	(r37826)
+++ trunk/include/parrot/io.h	Tue Mar 31 16:34:25 2009	(r37827)
@@ -905,7 +905,35 @@
 #define PIOCTL_LINEBUF             1
 #define PIOCTL_BLKBUF              2
 
+/*
+ * Enum definition of constants for Socket.socket.
+ * Happens to be same for corresponding values on Linux. Other implementations
+ * of socket API may have to map this values to system specific.
+ */
+
+/* &gen_from_enum(socket.pasm) */
+typedef enum {
+    PIO_PF_LOCAL    = 0,
+    PIO_PF_UNIX     = 1,
+    PIO_PF_INET     = 2,
+    PIO_PF_INET6    = 3,
+    PIO_PF_MAX      = 4     /* last elem */
+} Socket_Protocol_Family;
+
+typedef enum {
+    PIO_SOCK_STREAM     = 1,
+    PIO_SOCK_DGRAM      = 2,
+    PIO_SOCK_RAW        = 3,
+    PIO_SOCK_RDM        = 4,
+    PIO_SOCK_SEQPACKET  = 5,
+    PIO_SOCK_PACKET     = 10,
+} Socket_Socket_Type;
 
+typedef enum {
+    PIO_PROTO_TCP   = 6,
+    PIO_PROTO_UDP   = 17,
+} Socket_Protocol;
+/* &end_gen */
 
 #endif /* PARROT_IO_H_GUARD */
 

Modified: trunk/src/io/socket_unix.c
==============================================================================
--- trunk/src/io/socket_unix.c	Tue Mar 31 15:40:48 2009	(r37826)
+++ trunk/src/io/socket_unix.c	Tue Mar 31 16:34:25 2009	(r37827)
@@ -114,6 +114,19 @@
 #  if PARROT_NET_DEVEL
 
 /*
+ * Mappping between PIO_PF_* constants and system-specific PF_* constants.
+ *
+ * Uses -1 for unsupported protocols.
+ */
+
+static int pio_pf[PIO_PF_MAX+1] = {
+    PF_LOCAL,   /* PIO_PF_LOCAL */
+    PF_UNIX,    /* PIO_PF_UNIX */
+    PF_INET,    /* PIO_PF_INET */
+    PF_INET6,   /* PIO_PF_INET6 */
+};
+
+/*
 
 =item C<INTVAL Parrot_io_socket_unix(PARROT_INTERP, PMC *s, int fam, int type,
 int proto)>
@@ -132,6 +145,13 @@
 {
     ASSERT_ARGS(Parrot_io_socket_unix)
     int i = 1;
+    /* convert Parrot's family to system family */
+    if (fam < 0 || fam >= PIO_PF_MAX)
+        return -1;
+    fam = pio_pf[fam];
+    if (fam < 0)
+        return -1;
+
     const int sock = socket(fam, type, proto);
     if (sock >= 0) {
         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i));

Modified: trunk/src/io/socket_win32.c
==============================================================================
--- trunk/src/io/socket_win32.c	Tue Mar 31 15:40:48 2009	(r37826)
+++ trunk/src/io/socket_win32.c	Tue Mar 31 16:34:25 2009	(r37827)
@@ -60,6 +60,19 @@
                 PARROT_SOCKET((p))->remote))
 
 /*
+ * Mappping between PIO_PF_* constants and system-specific PF_* constants.
+ *
+ * Uses -1 for unsupported protocols.
+ */
+
+static int pio_pf[PIO_PF_MAX+1] = {
+    PF_LOCAL,   /* PIO_PF_LOCAL */
+    PF_UNIX,    /* PIO_PF_UNIX */
+    PF_INET,    /* PIO_PF_INET */
+    PF_INET6,   /* PIO_PF_INET6 */
+};
+
+/*
 
 =item C<INTVAL Parrot_io_socket_win32(PARROT_INTERP, PMC * s, int fam,
 int type, int proto)>
@@ -78,6 +91,13 @@
 {
     ASSERT_ARGS(Parrot_io_socket_win32)
     int i = 1;
+    /* convert Parrot's family to system family */
+    if (fam < 0 || fam >= PIO_PF_MAX)
+        return -1;
+    fam = pio_pf[fam];
+    if (fam < 0)
+        return -1;
+
     const int sock = socket(fam, type, proto);
     if (sock >= 0) {
         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&i, sizeof (i));


More information about the parrot-commits mailing list