[svn:parrot] r37833 - in trunk: examples/io include/parrot src/io
Infinoid at svn.parrot.org
Infinoid at svn.parrot.org
Wed Apr 1 05:02:33 UTC 2009
Author: Infinoid
Date: Wed Apr 1 05:02:30 2009
New Revision: 37833
URL: https://trac.parrot.org/parrot/changeset/37833
Log:
Apply patches from bacek++ in TT #527:
* Implement lookup table for PIO_SOCK_* constants
* Update examples/io/http.pir to use constants.
Modified:
trunk/examples/io/http.pir
trunk/include/parrot/io.h
trunk/src/io/socket_unix.c
trunk/src/io/socket_win32.c
Modified: trunk/examples/io/http.pir
==============================================================================
--- trunk/examples/io/http.pir Wed Apr 1 01:12:40 2009 (r37832)
+++ trunk/examples/io/http.pir Wed Apr 1 05:02:30 2009 (r37833)
@@ -19,6 +19,8 @@
=cut
+.include 'socket.pasm'
+
.sub example :main
.local pmc sock
.local pmc address
@@ -29,7 +31,7 @@
# create the socket handle
print "Creating socket.\n"
sock = new 'Socket'
- sock.'socket'(2, 1, 0)
+ sock.'socket'(.PIO_PF_INET, .PIO_SOCK_STREAM, .PIO_PROTO_TCP)
unless sock goto ERR
# Pack a sockaddr_in structure with IP and port
Modified: trunk/include/parrot/io.h
==============================================================================
--- trunk/include/parrot/io.h Wed Apr 1 01:12:40 2009 (r37832)
+++ trunk/include/parrot/io.h Wed Apr 1 05:02:30 2009 (r37833)
@@ -922,12 +922,13 @@
} Socket_Protocol_Family;
typedef enum {
+ PIO_SOCK_PACKET = 0,
PIO_SOCK_STREAM = 1,
PIO_SOCK_DGRAM = 2,
PIO_SOCK_RAW = 3,
PIO_SOCK_RDM = 4,
PIO_SOCK_SEQPACKET = 5,
- PIO_SOCK_PACKET = 10,
+ PIO_SOCK_MAX = 6 /* last element */
} Socket_Socket_Type;
typedef enum {
Modified: trunk/src/io/socket_unix.c
==============================================================================
--- trunk/src/io/socket_unix.c Wed Apr 1 01:12:40 2009 (r37832)
+++ trunk/src/io/socket_unix.c Wed Apr 1 05:02:30 2009 (r37833)
@@ -114,7 +114,7 @@
# if PARROT_NET_DEVEL
/*
- * Mappping between PIO_PF_* constants and system-specific PF_* constants.
+ * Mapping between PIO_PF_* constants and system-specific PF_* constants.
*
* Uses -1 for unsupported protocols.
*/
@@ -143,6 +143,44 @@
};
/*
+ * Mapping between PIO_SOCK_* constants and system-specific SOCK_* constants.
+ * Uses -1 for unsupported socket types.
+ */
+
+static int pio_sock[PIO_SOCK_MAX+1] = {
+# ifdef SOCK_PACKET
+ SOCK_PACKET, /* PIO_SOCK_PACKET */
+# else
+ -1, /* PIO_SOCK_PACKET */
+# endif
+# ifdef SOCK_STREAM
+ SOCK_STREAM, /* PIO_SOCK_STREAM */
+# else
+ -1, /* PIO_SOCK_STREAM */
+# endif
+# ifdef SOCK_DGRAM
+ SOCK_DGRAM, /* PIO_SOCK_DGRAM */
+# else
+ -1, /* PIO_SOCK_DGRAM */
+# endif
+# ifdef SOCK_RAW
+ SOCK_RAW, /* PIO_SOCK_RAW */
+# else
+ -1, /* PIO_SOCK_RAW */
+# endif
+# ifdef SOCK_RDM
+ SOCK_RDM, /* PIO_SOCK_RDM */
+# else
+ -1, /* PIO_SOCK_RDM */
+# endif
+# ifdef SOCK_SEQPACKET
+ SOCK_SEQPACKET, /* PIO_SOCK_SEQPACKET */
+# else
+ -1, /* PIO_SOCK_SEQPACKET */
+# endif
+};
+
+/*
=item C<INTVAL Parrot_io_socket_unix(PARROT_INTERP, PMC *s, int fam, int type,
int proto)>
@@ -168,6 +206,13 @@
if (fam < 0)
return -1;
+ /* convert Parrot's socket type to system type */
+ if (type < 0 || type >= PIO_SOCK_MAX)
+ return -1;
+ type = pio_sock[type];
+ if (type < 0)
+ return -1;
+
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 Wed Apr 1 01:12:40 2009 (r37832)
+++ trunk/src/io/socket_win32.c Wed Apr 1 05:02:30 2009 (r37833)
@@ -60,7 +60,7 @@
PARROT_SOCKET((p))->remote))
/*
- * Mappping between PIO_PF_* constants and system-specific PF_* constants.
+ * Mapping between PIO_PF_* constants and system-specific PF_* constants.
*
* Uses -1 for unsupported protocols.
*/
@@ -89,6 +89,44 @@
};
/*
+ * Mapping between PIO_SOCK_* constants and system-specific SOCK_* constants.
+ * Uses -1 for unsupported socket types.
+ */
+
+static int pio_sock[PIO_SOCK_MAX+1] = {
+# ifdef SOCK_PACKET
+ SOCK_PACKET, /* PIO_SOCK_PACKET */
+# else
+ -1, /* PIO_SOCK_PACKET */
+# endif
+# ifdef SOCK_STREAM
+ SOCK_STREAM, /* PIO_SOCK_STREAM */
+# else
+ -1, /* PIO_SOCK_STREAM */
+# endif
+# ifdef SOCK_DGRAM
+ SOCK_DGRAM, /* PIO_SOCK_DGRAM */
+# else
+ -1, /* PIO_SOCK_DGRAM */
+# endif
+# ifdef SOCK_RAW
+ SOCK_RAW, /* PIO_SOCK_RAW */
+# else
+ -1, /* PIO_SOCK_RAW */
+# endif
+# ifdef SOCK_RDM
+ SOCK_RDM, /* PIO_SOCK_RDM */
+# else
+ -1, /* PIO_SOCK_RDM */
+# endif
+# ifdef SOCK_SEQPACKET
+ SOCK_SEQPACKET, /* PIO_SOCK_SEQPACKET */
+# else
+ -1, /* PIO_SOCK_SEQPACKET */
+# endif
+};
+
+/*
=item C<INTVAL Parrot_io_socket_win32(PARROT_INTERP, PMC * s, int fam,
int type, int proto)>
@@ -114,6 +152,13 @@
if (fam < 0)
return -1;
+ /* convert Parrot's socket type to system type */
+ if (type < 0 || type >= PIO_SOCK_MAX)
+ return -1;
+ type = pio_sock[type];
+ if (type < 0)
+ return -1;
+
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