Advertisement
Guest User

Untitled

a guest
Aug 9th, 2010
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.06 KB | None | 0 0
  1.  
  2. class IO2::Socket::INET
  3. {
  4.     has $!PIO;
  5.     method pio()
  6.     {
  7.         $!PIO;
  8.     }
  9.  
  10.     method open (Str $hostname, Int $port)
  11.     {
  12.         my $s = Q:PIR {
  13.             .include "socket.pasm"
  14.             .local pmc sock
  15.             .local pmc address
  16.             .local string hostname
  17.             .local int port
  18.             .local string buf
  19.             .local int ret
  20.  
  21.             .local pmc self
  22.             self = find_lex 'self'
  23.  
  24.             $P0 = find_lex "$hostname"
  25.             hostname = $P0
  26.  
  27.             $P0 = find_lex "$port"
  28.             port = $P0
  29.  
  30.             # Create the socket handle
  31.             sock = root_new ['parrot';'Socket']
  32.             $P1 = new 'Integer'
  33.             unless sock goto ERR
  34.             sock.'socket'(.PIO_PF_INET, .PIO_SOCK_STREAM, .PIO_PROTO_TCP)
  35.  
  36.             # Pack a sockaddr_in structure with IP and port
  37.             address = sock.'sockaddr'(hostname, port)
  38.             $P1 = sock.'connect'(address)
  39.             setattribute self, '$!PIO', sock
  40.             goto DONE
  41.             ERR:
  42.             $P1 = -1
  43.             DONE:
  44.             %r = $P1
  45.         };
  46.     }
  47.  
  48.     multi method write(Str $str) {
  49.         fail("Not connected") unless $!PIO;
  50.         return $!PIO.send($str);
  51.     }
  52.  
  53.     multi method write(Buf $buf) {
  54.         fail("Not connected") unless $!PIO;
  55.         my @contents = $buf.contents;
  56.         my $pio = $!PIO;
  57.         Q:PIR {
  58.             $P0 = find_lex '@contents'
  59.  
  60.             .local pmc bb
  61.             .local string s
  62.             bb = new ['ByteBuffer']
  63.             .local pmc it
  64.             .local int i
  65.             it = iter $P0
  66.             i = 0
  67.             loop:
  68.             unless it goto done
  69.             $P1 = shift it
  70.             $I1 = $P1
  71.             bb[i] = $I1
  72.             inc i
  73.             goto loop
  74.             done:
  75.             s = bb.'get_string_as'(binary:"")
  76.             .local pmc pio
  77.             pio = find_lex '$pio'
  78.             pio = deref_unless_object pio
  79.             pio.'send'(s)
  80.         };
  81.     }
  82.  
  83.     method read(Int $bytes) {
  84.         my $pio = $!PIO;
  85.         my @bytes = Q:PIR {
  86.             .local int nbytes, byte
  87.             .local pmc bytebuffer, it, result
  88.             .local pmc pio
  89.             pio = find_lex '$pio'
  90.             pio = deref_unless_object pio
  91.             $P0 = find_lex '$bytes'
  92.             nbytes = $P0
  93.             $S0 = pio.'recv'(nbytes)
  94.             bytebuffer = new ['ByteBuffer']
  95.             bytebuffer = $S0
  96.  
  97.             result = new ['Parcel']
  98.             it = iter bytebuffer
  99.             bytes_loop:
  100.             unless it goto done
  101.             byte = shift it
  102.             push result, byte
  103.             goto bytes_loop
  104.             done:
  105.             %r = result
  106.         };
  107.         return Buf.new(@bytes);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement