UVSample.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import hl.uv.*;
  2. class UVSample {
  3. static var T0 = haxe.Timer.stamp();
  4. static function log( msg : String ) {
  5. Sys.println("["+Std.int((haxe.Timer.stamp() - T0) * 100)+"] "+msg);
  6. }
  7. static function main() {
  8. var loop = Loop.getDefault();
  9. var tcp = new Tcp(loop);
  10. /*
  11. tcp.connect(new sys.net.Host("google.com"), 80, function(b) {
  12. log("Connected=" + b);
  13. var bytes = haxe.io.Bytes.ofString("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
  14. tcp.write(bytes, function(b) trace("Sent=" + b));
  15. var buf = new haxe.io.BytesBuffer();
  16. tcp.readStart(function(bytes:hl.Bytes, len) {
  17. if( len < 0 ) {
  18. var str = buf.getBytes().toString();
  19. if( str.length > 1000 )
  20. str = str.substr(0, 500) + "\n...\n" + str.substr(str.length - 500);
  21. log("#" + str + "#");
  22. tcp.readStop();
  23. return;
  24. }
  25. log("Read="+len);
  26. var bytes = bytes.toBytes(len);
  27. buf.addBytes(bytes, 0, len);
  28. });
  29. });
  30. */
  31. var host = new sys.net.Host("localhost");
  32. var port = 6001;
  33. var totR = 0, totW = 0, totRB = 0;
  34. log("Starting server");
  35. tcp.bind(host, port);
  36. tcp.listen(5, function() {
  37. log("Client connected");
  38. var s = tcp.accept();
  39. s.readStart(function(bytes) {
  40. if( bytes == null ) {
  41. s.close();
  42. return;
  43. }
  44. totR += bytes.length;
  45. // write back
  46. s.write(bytes, function(b) if( !b ) throw "Write failure");
  47. });
  48. });
  49. function startClient() {
  50. var numbers = [];
  51. var client = new Tcp(loop);
  52. //log("Connecting...");
  53. client.connect(host, port, function(b) {
  54. //log("Connected to server");
  55. function send() {
  56. var b = haxe.io.Bytes.alloc(1);
  57. var k = Std.random(255);
  58. numbers.push(k);
  59. totW++;
  60. b.set(0, k);
  61. client.write(b, function(b) if( !b ) log("Write failure"));
  62. }
  63. function sendBatch() {
  64. for( i in 0...1+Std.random(10) )
  65. send();
  66. }
  67. sendBatch();
  68. client.readStart(function(b) {
  69. totRB += b.length;
  70. for( i in 0...b.length ) {
  71. var k = b.get(i);
  72. if( !numbers.remove(k) )
  73. throw "!";
  74. }
  75. if( numbers.length == 0 ) {
  76. if( Std.random(10000) == 0 ) {
  77. startClient();
  78. client.close();
  79. } else
  80. sendBatch();
  81. }
  82. });
  83. });
  84. }
  85. for( i in 0...4 )
  86. startClient();
  87. log("Enter Loop");
  88. var K = 0;
  89. var maxRead = 1000000;
  90. while( loop.run(NoWait) != 0 ) {
  91. if( K++ % 10000 == 0 ) log("Read=" + totR);
  92. if( totR > maxRead ) {
  93. log("Total Read > " + maxRead);
  94. break;
  95. }
  96. }
  97. log("Done");
  98. Sys.exit(0);
  99. }
  100. }