Simn / haxe

6 stars 0 forks source link

initial socket support #28

Closed ousado closed 10 years ago

ousado commented 10 years ago

Could you try running this:

    import sys.net.Socket;
    import sys.net.Host;

    import haxe.io.Bytes;

    class Main {
        public static function main() {

            var s = new Socket();
            s.connect(new Host('google.com'),80);
            trace(s.peer());
            s.setTimeout(12.0);
            s.setBlocking(true);
            for (i in 0...1) {
                s.output.writeBytes(Bytes.ofString("GET / HTTP/1.0\r\n\r\n"),0,18);
            }
            var N = 100000;
            var res = Bytes.alloc(N);
            try {
                var n  = s.input.readBytes(res,0,N);
                trace('$n ${N-n} $res');
                var n2 = s.input.readBytes(res,n,N-n);
                trace(res);
                trace('got $n, $n2 bytes, $res');
            } catch (e:Dynamic) {
                trace(e);
            }

            var N   = 20;
            var off = 200;
            var clients  = [];
            var sclients = [];
            var listeners = [for (i in 0...N) {
                var s = new Socket();
                s.bind(new Host('localhost'), off + 1800 + i);
                s.listen(50);
                s.setBlocking(false);
                var c = new Socket();
                c.connect(new Host('localhost'), off +  1800 + i);
                c.setBlocking(false);
                clients.push(c);
                s;
            }];

            var count = 0;
            for (i in 0...500) {
                var wro = Socket.select(sclients,[],[],0.0);
                for (s in wro.read) {
                    var b = s.input.readBytes(res,0,N);

                    count += b;
                }
                var wro = Socket.select(listeners,[],[],0.0);
                for (s in wro.read) {
                    var sc = s.accept();
                    sclients.push(sc);
                    sc.setBlocking(false);
                }
                var wro = Socket.select([],clients,[],0.0);
                for (s in wro.write) {
                    s.output.writeBytes(Bytes.ofString("GET / HTTP/1.0\r\n\r\n"),0,18);
                }
            }
            trace('read $count bytes total');
        }
    }
Simn commented 10 years ago

Wow that's great! Thanks a lot for your work on this.

Running your example I get this output:

Main.hx:11: { host : 173.194.113.167, port : 80 }
Main.hx:21: 517 99483 HTTP/1.0 302 Found

Cache-Control: private

Content-Type: text/html; charset=UTF-8

Location: http://www.google.de/?gfe_rd=cr&ei=kqQ_U-rGLsGK8Qes1IGwAQ

Content-Length: 258

Date: Sat, 05 Apr 2014 06:37:06 GMT

Server: GFE/2.0

Alternate-Protocol: 80:quic

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.de/?gfe_rd=cr&amp;ei=kqQ_U-rGLsGK8Qes1IGwAQ">here</A>.

</BODY></HTML>

Main.hx:26: Eof
Traceback (most recent call last):
  File "bin/python.py", line 2112, in <module>
    Main.main()
  File "bin/python.py", line 1104, in Main_statics_main
    wro = sys_net_Socket.select(sclients, [], [], 0.0)
  File "bin/python.py", line 2054, in Socket_statics_select
    t3 = select.select(read, write, others, timeout)
OSError: [WinError 10022] An invalid argument was supplied
Simn commented 10 years ago

For what it's worth I get something quite similar on neko:

Main.hx:26: Eof
Called from ? line 1
Called from Main.hx line 47
Called from C:\GitHub\haxe\std/neko/_std/sys/net/Socket.hx line 225
Uncaught exception - std@socket_select
Simn commented 10 years ago

Apparently you cannot pass three empty arrays to Socket.select on Windows. In your example sclients is empty.