Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(954)

Side by Side Diff: runtime/bin/socket_patch.dart

Issue 1320023008: Refactor the io resource classes (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: address comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | runtime/observatory/tests/service/tcp_socket_service_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 patch class RawServerSocket { 5 patch class RawServerSocket {
6 /* patch */ static Future<RawServerSocket> bind(address, 6 /* patch */ static Future<RawServerSocket> bind(address,
7 int port, 7 int port,
8 {int backlog: 0, 8 {int backlog: 0,
9 bool v6Only: false, 9 bool v6Only: false,
10 bool shared: false}) { 10 bool shared: false}) {
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 306
307 int tokens = 0; 307 int tokens = 0;
308 308
309 bool sendReadEvents = false; 309 bool sendReadEvents = false;
310 bool readEventIssued = false; 310 bool readEventIssued = false;
311 311
312 bool sendWriteEvents = false; 312 bool sendWriteEvents = false;
313 bool writeEventIssued = false; 313 bool writeEventIssued = false;
314 bool writeAvailable = false; 314 bool writeAvailable = false;
315 315
316 static final Stopwatch sw = new Stopwatch()..start();
317
318 static bool connectedResourceHandler = false; 316 static bool connectedResourceHandler = false;
319 _ReadWriteResourceInfo resourceInfo; 317 _ReadWriteResourceInfo resourceInfo;
320 318
321 // The owner object is the object that the Socket is being used by, e.g. 319 // The owner object is the object that the Socket is being used by, e.g.
322 // a HttpServer, a WebSocket connection, a process pipe, etc. 320 // a HttpServer, a WebSocket connection, a process pipe, etc.
323 Object owner; 321 Object owner;
324 322
325 static double get timestamp => sw.elapsedMicroseconds / 1000000.0; 323 static double get timestamp => sw.elapsedMicroseconds / 1000000.0;
326 324
327 static Future<List<InternetAddress>> lookup( 325 static Future<List<InternetAddress>> lookup(
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 available -= result.length; 575 available -= result.length;
578 // TODO(ricow): Remove when we track internal and pipe uses. 576 // TODO(ricow): Remove when we track internal and pipe uses.
579 assert(resourceInfo != null || isPipe || isInternal); 577 assert(resourceInfo != null || isPipe || isInternal);
580 if (resourceInfo != null) { 578 if (resourceInfo != null) {
581 resourceInfo.totalRead += result.length; 579 resourceInfo.totalRead += result.length;
582 } 580 }
583 } 581 }
584 // TODO(ricow): Remove when we track internal and pipe uses. 582 // TODO(ricow): Remove when we track internal and pipe uses.
585 assert(resourceInfo != null || isPipe || isInternal); 583 assert(resourceInfo != null || isPipe || isInternal);
586 if (resourceInfo != null) { 584 if (resourceInfo != null) {
587 resourceInfo.readCount++; 585 resourceInfo.didRead();
588 resourceInfo.lastRead = timestamp;
589 } 586 }
590 return result; 587 return result;
591 } 588 }
592 589
593 Datagram receive() { 590 Datagram receive() {
594 if (isClosing || isClosed) return null; 591 if (isClosing || isClosed) return null;
595 var result = nativeRecvFrom(); 592 var result = nativeRecvFrom();
596 if (result is OSError) { 593 if (result is OSError) {
597 reportError(result, "Receive failed"); 594 reportError(result, "Receive failed");
598 return null; 595 return null;
599 } 596 }
600 if (result != null) { 597 if (result != null) {
601 // Read the next available. Available is only for the next datagram, not 598 // Read the next available. Available is only for the next datagram, not
602 // the sum of all datagrams pending, so we need to call after each 599 // the sum of all datagrams pending, so we need to call after each
603 // receive. If available becomes > 0, the _NativeSocket will continue to 600 // receive. If available becomes > 0, the _NativeSocket will continue to
604 // emit read events. 601 // emit read events.
605 available = nativeAvailable(); 602 available = nativeAvailable();
606 // TODO(ricow): Remove when we track internal and pipe uses. 603 // TODO(ricow): Remove when we track internal and pipe uses.
607 assert(resourceInfo != null || isPipe || isInternal); 604 assert(resourceInfo != null || isPipe || isInternal);
608 if (resourceInfo != null) { 605 if (resourceInfo != null) {
609 resourceInfo.totalRead += result.data.length; 606 resourceInfo.totalRead += result.data.length;
610 } 607 }
611 } 608 }
612 // TODO(ricow): Remove when we track internal and pipe uses. 609 // TODO(ricow): Remove when we track internal and pipe uses.
613 assert(resourceInfo != null || isPipe || isInternal); 610 assert(resourceInfo != null || isPipe || isInternal);
614 if (resourceInfo != null) { 611 if (resourceInfo != null) {
615 resourceInfo.readCount++; 612 resourceInfo.didRead();
616 resourceInfo.lastRead = timestamp;
617 } 613 }
618 return result; 614 return result;
619 } 615 }
620 616
621 int write(List<int> buffer, int offset, int bytes) { 617 int write(List<int> buffer, int offset, int bytes) {
622 if (buffer is! List) throw new ArgumentError(); 618 if (buffer is! List) throw new ArgumentError();
623 if (offset == null) offset = 0; 619 if (offset == null) offset = 0;
624 if (bytes == null) { 620 if (bytes == null) {
625 if (offset > buffer.length) { 621 if (offset > buffer.length) {
626 throw new RangeError.value(offset); 622 throw new RangeError.value(offset);
(...skipping 22 matching lines...) Expand all
649 // purpose. In such case, don't mark writeAvailable as false, as we don't 645 // purpose. In such case, don't mark writeAvailable as false, as we don't
650 // know if we'll receive an event. It's better to just retry. 646 // know if we'll receive an event. It's better to just retry.
651 if (result >= 0 && result < bytes) { 647 if (result >= 0 && result < bytes) {
652 writeAvailable = false; 648 writeAvailable = false;
653 } 649 }
654 // Negate the result, as stated above. 650 // Negate the result, as stated above.
655 if (result < 0) result = -result; 651 if (result < 0) result = -result;
656 // TODO(ricow): Remove when we track internal and pipe uses. 652 // TODO(ricow): Remove when we track internal and pipe uses.
657 assert(resourceInfo != null || isPipe || isInternal); 653 assert(resourceInfo != null || isPipe || isInternal);
658 if (resourceInfo != null) { 654 if (resourceInfo != null) {
659 resourceInfo.totalWritten += result; 655 resourceInfo.addWrite(result);
660 resourceInfo.writeCount++;
661 resourceInfo.lastWrite = timestamp;
662 } 656 }
663 return result; 657 return result;
664 } 658 }
665 659
666 int send(List<int> buffer, int offset, int bytes, 660 int send(List<int> buffer, int offset, int bytes,
667 InternetAddress address, int port) { 661 InternetAddress address, int port) {
668 if (isClosing || isClosed) return 0; 662 if (isClosing || isClosed) return 0;
669 _BufferAndStart bufferAndStart = 663 _BufferAndStart bufferAndStart =
670 _ensureFastAndSerializableByteData( 664 _ensureFastAndSerializableByteData(
671 buffer, offset, bytes); 665 buffer, offset, bytes);
672 var result = nativeSendTo( 666 var result = nativeSendTo(
673 bufferAndStart.buffer, bufferAndStart.start, bytes, 667 bufferAndStart.buffer, bufferAndStart.start, bytes,
674 address._in_addr, port); 668 address._in_addr, port);
675 if (result is OSError) { 669 if (result is OSError) {
676 scheduleMicrotask(() => reportError(result, "Send failed")); 670 scheduleMicrotask(() => reportError(result, "Send failed"));
677 result = 0; 671 result = 0;
678 } 672 }
679 // TODO(ricow): Remove when we track internal and pipe uses. 673 // TODO(ricow): Remove when we track internal and pipe uses.
680 assert(resourceInfo != null || isPipe || isInternal); 674 assert(resourceInfo != null || isPipe || isInternal);
681 if (resourceInfo != null) { 675 if (resourceInfo != null) {
682 resourceInfo.totalWritten += result; 676 resourceInfo.addWrite(result);
683 resourceInfo.writeCount++;
684 resourceInfo.lastWrite = timestamp;
685 } 677 }
686 return result; 678 return result;
687 } 679 }
688 680
689 _NativeSocket accept() { 681 _NativeSocket accept() {
690 // Don't issue accept if we're closing. 682 // Don't issue accept if we're closing.
691 if (isClosing || isClosed) return null; 683 if (isClosing || isClosed) return null;
692 assert(available > 0); 684 assert(available > 0);
693 available--; 685 available--;
694 tokens++; 686 tokens++;
695 returnTokens(LISTENING_TOKEN_BATCH_SIZE); 687 returnTokens(LISTENING_TOKEN_BATCH_SIZE);
696 var socket = new _NativeSocket.normal(); 688 var socket = new _NativeSocket.normal();
697 if (nativeAccept(socket) != true) return null; 689 if (nativeAccept(socket) != true) return null;
698 socket.localPort = localPort; 690 socket.localPort = localPort;
699 socket.localAddress = address; 691 socket.localAddress = address;
700 setupResourceInfo(socket); 692 setupResourceInfo(socket);
701 // TODO(ricow): Remove when we track internal and pipe uses. 693 // TODO(ricow): Remove when we track internal and pipe uses.
702 assert(resourceInfo != null || isPipe || isInternal); 694 assert(resourceInfo != null || isPipe || isInternal);
703 if (resourceInfo != null) { 695 if (resourceInfo != null) {
704 resourceInfo.totalRead += 1; 696 // We track this as read one byte.
705 resourceInfo.lastRead = timestamp; 697 resourceInfo.addRead(1);
706 } 698 }
707 return socket; 699 return socket;
708 } 700 }
709 701
710 int get port { 702 int get port {
711 if (localPort != 0) return localPort; 703 if (localPort != 0) return localPort;
712 if (isClosing || isClosed) throw const SocketException.closed(); 704 if (isClosing || isClosed) throw const SocketException.closed();
713 var result = nativeGetPort(); 705 var result = nativeGetPort();
714 if (result is OSError) throw result; 706 if (result is OSError) throw result;
715 return localPort = result; 707 return localPort = result;
(...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after
1909 Datagram _makeDatagram(List<int> data, 1901 Datagram _makeDatagram(List<int> data,
1910 String address, 1902 String address,
1911 List<int> in_addr, 1903 List<int> in_addr,
1912 int port) { 1904 int port) {
1913 return new Datagram( 1905 return new Datagram(
1914 data, 1906 data,
1915 new _InternetAddress(address, null, in_addr), 1907 new _InternetAddress(address, null, in_addr),
1916 port); 1908 port);
1917 } 1909 }
1918 1910
OLDNEW
« no previous file with comments | « no previous file | runtime/observatory/tests/service/tcp_socket_service_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698