start porting from Java
[premotedroid-server-perl.git] / premotedroid-server.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use IO::Socket::INET;
6 use Data::Dump qw(dump);
7
8 my $sock = IO::Socket::INET->new(
9         Listen    => 5,
10 #       LocalAddr => 'localhost',
11         LocalPort => 64788,
12         Proto     => 'tcp',
13         Reuse     => 1,
14 ) || die $!;
15
16 warn "listen on ", dump $sock->sockaddr, $sock->sockport;
17
18 sub readUTF {
19         my $client = shift;
20         read $client, my $len, 2;
21         $len = unpack( 'n', $len );
22         read $client, my $utf, $len;
23         warn "## readUTF $len [$utf]";
24         return $utf;
25 }
26
27 # from PRemoteDroid Protocol/src/org/pierre/remotedroid/protocol/action/PRemoteDroidAction.java
28 use constant MOUSE_MOVE => 0;
29 use constant MOUSE_CLICK => 1;
30 use constant MOUSE_WHEEL => 2;
31 use constant KEYBOARD => 3;
32 use constant AUTHENTIFICATION => 4;
33 use constant AUTHENTIFICATION_RESPONSE => 5;
34 use constant SCREEN_CAPTURE_REQUEST => 6;
35 use constant SCREEN_CAPTURE_RESPONSE => 7;
36 use constant FILE_EXPLORE_REQUEST => 8;
37 use constant FILE_EXPLORE_RESPONSE => 9;
38
39 while ( my $client = $sock->accept() ) {
40
41         warn "connect from ", dump $client->peeraddr, $client->peerport;
42
43         while ( read $client, my $command, 1 ) {
44                 $command = ord $command;
45                 warn "# command: $command\n";
46                 if ( $command == AUTHENTIFICATION ) {
47                         my $auth = readUTF $client;
48                         warn "AUTHENTIFICATION [$auth]\n";
49
50                 } else {
51                         die "UNSUPPORTED";
52                 }
53         }
54
55         warn "client disconnected\n";
56
57 }