Skip to content

Instantly share code, notes, and snippets.

@bacek
Created March 26, 2009 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bacek/86041 to your computer and use it in GitHub Desktop.
Save bacek/86041 to your computer and use it in GitHub Desktop.
bacek@icering:~/src/parrot$ cat s2.patch
diff --git a/examples/io/httpd.pir b/examples/io/httpd.pir
index fe6a1c1..01a596c 100644
--- a/examples/io/httpd.pir
+++ b/examples/io/httpd.pir
@@ -111,11 +111,13 @@ The code was heavily hacked by bernhard and leo.
port = 1234
# TODO provide sys/socket constants
- listener = socket 2, 1, 6 # PF_INET, SOCK_STREAM, tcp
+ .local pmc sock
+ sock = new 'Socket'
+ listener = sock.'socket'(2, 1, 6) # PF_INET, SOCK_STREAM, tcp
unless listener goto ERR_NO_SOCKET
# Pack a sockaddr_in structure with IP and port
- address = sockaddr host, port
+ address = sock.'sockaddr'(host, port)
ret = listener.'bind'(address)
if ret == -1 goto ERR_bind
$S0 = port
diff --git a/src/ops/io.ops b/src/ops/io.ops
index 52e3b2e..fdd30e0 100644
--- a/src/ops/io.ops
+++ b/src/ops/io.ops
@@ -494,78 +494,6 @@ op tell(out INT, out INT, invar PMC) :base_io {
}
}
-########################################
-
-=item B<sockaddr>(out PMC, in STR, in INT)
-
-Create new Sockaddr PMC.
-
-=cut
-
-op sockaddr(out PMC, in STR, in INT) {
- $1 = Parrot_io_sockaddr_in(interp, $2, $3);
-}
-
-=item B<socket>(out PMC, in INT, in INT, in INT)
-
-Create new socket
-
-=cut
-
-op socket(out PMC, in INT, in INT, in INT) {
- $1 = Parrot_io_socket(interp, $2, $3, $4);
-}
-
-=item B<bind>(out INT, in PMC, in PMC)
-
-Bind socket to address
-
-=cut
-
-op bind(out INT, in PMC, in PMC) {
- $1 = Parrot_io_bind(interp, $2, $3);
-}
-
-=item B<listen>(out INT, in PMC, in INT)
-
-Start listening on previously bound socket
-
-=cut
-
-op listen(out INT, in PMC, in INT) {
- $1 = Parrot_io_listen(interp, $2, $3);
-}
-
-=item B<accept>(out PMC, in PMC)
-
-Accept incoming connection
-
-=cut
-
-op accept(out PMC, in PMC) {
- $1 = Parrot_io_accept(interp, $2);
-}
-
-=item B<recv>(out INT, in PMC, out STR)
-
-Receive data from socket
-
-=cut
-
-op recv(out INT, in PMC, out STR) {
- $1 = Parrot_io_recv(interp, $2, &$3);
-}
-
-=item B<send>(out INT, in PMC, in STR)
-
-Send data to socket
-
-=cut
-
-op send(out INT, in PMC, in STR) {
- $1 = Parrot_io_send(interp, $2, $3);
-}
-
=back
=cut
diff --git a/src/ops/ops.num b/src/ops/ops.num
index df8be16..cea0c7d 100644
--- a/src/ops/ops.num
+++ b/src/ops/ops.num
@@ -1268,31 +1268,3 @@ find_sub_not_null_p_s 1243
find_sub_not_null_p_sc 1244
load_language_s 1245
load_language_sc 1246
-socket_p_i_i_i 1247
-socket_p_ic_i_i 1248
-socket_p_i_ic_i 1249
-socket_p_i_i_ic 1250
-socket_p_ic_i_ic 1251
-socket_p_i_ic_ic 1252
-socket_p_ic_ic_ic 1253
-socket_p_ic_ic_i 1254
-sockaddr_p_s_i 1255
-sockaddr_p_sc_i 1256
-sockaddr_p_s_ic 1257
-sockaddr_p_sc_ic 1258
-bind_i_p_p 1259
-bind_i_pc_p 1260
-bind_i_p_pc 1261
-bind_i_pc_pc 1262
-listen_i_p_i 1263
-listen_i_pc_i 1264
-listen_i_p_ic 1265
-listen_i_pc_ic 1266
-accept_p_p 1267
-accept_p_pc 1268
-recv_i_p_s 1269
-recv_i_pc_s 1270
-send_i_p_s 1271
-send_i_pc_s 1272
-send_i_p_sc 1273
-send_i_pc_sc 1274
diff --git a/src/pmc/socket.pmc b/src/pmc/socket.pmc
index 9f71c17..b429927 100644
--- a/src/pmc/socket.pmc
+++ b/src/pmc/socket.pmc
@@ -72,7 +72,9 @@ Create a copy of the filehandle.
PMC * const copy = Parrot_io_new_socket_pmc(interp, old_struct->flags);
Parrot_Socket_attributes * const data_struct = PARROT_SOCKET(copy);
- data_struct->os_handle = Parrot_dup(old_struct->os_handle);
+ data_struct->os_handle = Parrot_dup(old_struct->os_handle);
+ data_struct->local = VTABLE_clone(interp, old_struct->local);
+ data_struct->remote = VTABLE_clone(interp, old_struct->remote);
return SELF;
}
@@ -130,6 +132,35 @@ Free structures.
=over 4
+=item C<socket>
+
+=cut
+
+*/
+
+ METHOD socket(INTVAL type, INTVAL fam, INTVAL proto) {
+ PMC * res = Parrot_io_socket(interp, type, fam, proto);
+ RETURN(PMC * res);
+ }
+
+/*
+
+=item C<sockaddr>
+
+C<sockaddr> returns an object representing a socket address, generated
+from a port number (integer) and an address (string).
+
+=cut
+
+*/
+
+ METHOD sockaddr(STRING * address, INTVAL port) {
+ PMC * res = Parrot_io_sockaddr_in(interp, address, port);
+ RETURN(PMC * res);
+ }
+
+/*
+
=item C<connect>
Connects a socket object to an address.
@@ -191,16 +222,68 @@ complete, it invokes the callback, passing it a status object.
RETURN(INTVAL res);
}
+/*
+
+=item C<bind>
+
+C<bind> binds a socket object to the port and address specified by an
+address object (the packed result of C<sockaddr>).
+
+The asynchronous version takes an additional final PMC callback
+argument, and only returns a status object. When the bind operation is
+complete, it invokes the callback, passing it a status object and the
+socket object it was called on. [If you want notification when a bind
+operation is completed, you probably want to do something with that
+bound socket object.]
+
+=cut
+
+*/
+
METHOD bind(PMC *host) {
INTVAL res = Parrot_io_bind(INTERP, SELF, host);
RETURN(INTVAL res);
}
+/*
+
+=item C<listen>
+
+C<listen> specifies that a socket object is willing to accept incoming
+connections. The integer argument gives the maximum size of the queue
+for pending connections.
+
+There is no asynchronous version. C<listen> marks a set of attributes on
+the socket object.
+
+=cut
+
+*/
+
METHOD listen(INTVAL backlog) {
INTVAL res = Parrot_io_listen(INTERP, SELF, backlog);
RETURN(INTVAL res);
}
+/*
+
+=item C<accept>
+
+C<accept> accepts a new connection on a given socket object, and returns
+a newly created socket object for the connection.
+
+The asynchronous version takes an additional final PMC callback
+argument, and only returns a status object. When the accept operation
+receives a new connection, it invokes the callback, passing it a status
+object and a newly created socket object for the connection. [While the
+synchronous C<accept> has to be called repeatedly in a loop (once for
+each connection received), the asynchronous version is only called once,
+but continues to send new connection events until the socket is closed.]
+
+=cut
+
+*/
+
METHOD accept() {
PMC * res = Parrot_io_accept(INTERP, SELF);
RETURN(PMC * res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment