view lib/Abyss/Server.pm6 @ 3:d7762f4e8bfa

redirect answer to client ok
author e165727 <e165727@ie.u-ryukyu.ac.jp>
date Thu, 13 Feb 2020 20:49:23 +0900
parents f3eb367c309f
children 1ad45c44e12f
line wrap: on
line source

use v6.c;
unit class Abyss::Server:ver<0.0.1>:auth<cpan:ANATOFUZ>;
use MONKEY-SEE-NO-EVAL;
use IO::Socket::Unix;
use NativeCall;

sub close(int32) returns int32 is native { ... }
sub dup(int32 $old) returns int32 is native { ... }
sub dup2(int32 $new, int32 $old) returns int32 is native { ... }

method readeval 
{
    my $listen = IO::Socket::Unix.new(  :listen,
                                        :localhost<localhost>,
                                        :localport(3333) );
                                     
    my $backup = dup(1);#stdoutのバックアップを作成
    say DateTime.now;
    my $sumTime = 0;
    my $counter = 0;
  
    loop 
    {
        my $conn = $listen.accept;
        
        #my $start = now;

        say "next recv msg";
        my $sock_msg;
        
        my $buf = $conn.recv();
        $sock_msg = $buf;
        
        say $sock_msg;

        #my $end = now;
        #EVAL $sock_msg;
        #say $conn.native-descriptor();
        close(1);
        dup2($conn.native-descriptor(), 1);#stdoutをsocketに切り替え
        EVALFILE $sock_msg;
        #my $Time = $end - $start;
        #$sumTime = $sumTime + $Time;
        #say $Time; 
        #say "hello";
        
        #$conn.print: 'send to Client';
        #$conn.print: EVALFILE $sock_msg;
        
        dup2($backup, 1); #file descripterを元に戻す
        close($backup); #backup消す
        
        $conn.close;
    }
    $listen.close;
}


=begin pod

=head1 NAME

Abyss::Server - blah blah blah

=head1 SYNOPSIS

=begin code :lang<perl6>

use Abyss::Server;

=end code

=head1 DESCRIPTION

Abyss::Server is ...

=head1 AUTHOR

AnaTofuZ <anatofuz@gmail.com>

=head1 COPYRIGHT AND LICENSE

Copyright 2020 AnaTofuZ

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

=end pod