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

Side by Side Diff: src/platform-posix.cc

Issue 151603004: A64: Synchronize with r16587. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/a64
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « src/platform-openbsd.cc ('k') | src/platform-solaris.cc » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 734
735 735
736 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { 736 void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
737 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); 737 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
738 int result = pthread_setspecific(pthread_key, value); 738 int result = pthread_setspecific(pthread_key, value);
739 ASSERT_EQ(0, result); 739 ASSERT_EQ(0, result);
740 USE(result); 740 USE(result);
741 } 741 }
742 742
743 743
744 // ----------------------------------------------------------------------------
745 // POSIX socket support.
746 //
747
748 class POSIXSocket : public Socket {
749 public:
750 explicit POSIXSocket() {
751 // Create the socket.
752 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
753 if (IsValid()) {
754 // Allow rapid reuse.
755 static const int kOn = 1;
756 int ret = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR,
757 &kOn, sizeof(kOn));
758 ASSERT(ret == 0);
759 USE(ret);
760 }
761 }
762 explicit POSIXSocket(int socket): socket_(socket) { }
763 virtual ~POSIXSocket() { Shutdown(); }
764
765 // Server initialization.
766 bool Bind(const int port);
767 bool Listen(int backlog) const;
768 Socket* Accept() const;
769
770 // Client initialization.
771 bool Connect(const char* host, const char* port);
772
773 // Shutdown socket for both read and write.
774 bool Shutdown();
775
776 // Data Transimission
777 int Send(const char* data, int len) const;
778 int Receive(char* data, int len) const;
779
780 bool SetReuseAddress(bool reuse_address);
781
782 bool IsValid() const { return socket_ != -1; }
783
784 private:
785 int socket_;
786 };
787
788
789 bool POSIXSocket::Bind(const int port) {
790 if (!IsValid()) {
791 return false;
792 }
793
794 sockaddr_in addr;
795 memset(&addr, 0, sizeof(addr));
796 addr.sin_family = AF_INET;
797 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
798 addr.sin_port = htons(port);
799 int status = bind(socket_,
800 BitCast<struct sockaddr *>(&addr),
801 sizeof(addr));
802 return status == 0;
803 }
804
805
806 bool POSIXSocket::Listen(int backlog) const {
807 if (!IsValid()) {
808 return false;
809 }
810
811 int status = listen(socket_, backlog);
812 return status == 0;
813 }
814
815
816 Socket* POSIXSocket::Accept() const {
817 if (!IsValid()) {
818 return NULL;
819 }
820
821 int socket;
822 do {
823 socket = accept(socket_, NULL, NULL);
824 } while (socket == -1 && errno == EINTR);
825
826 if (socket == -1) {
827 return NULL;
828 } else {
829 return new POSIXSocket(socket);
830 }
831 }
832
833
834 bool POSIXSocket::Connect(const char* host, const char* port) {
835 if (!IsValid()) {
836 return false;
837 }
838
839 // Lookup host and port.
840 struct addrinfo *result = NULL;
841 struct addrinfo hints;
842 memset(&hints, 0, sizeof(addrinfo));
843 hints.ai_family = AF_INET;
844 hints.ai_socktype = SOCK_STREAM;
845 hints.ai_protocol = IPPROTO_TCP;
846 int status = getaddrinfo(host, port, &hints, &result);
847 if (status != 0) {
848 return false;
849 }
850
851 // Connect.
852 do {
853 status = connect(socket_, result->ai_addr, result->ai_addrlen);
854 } while (status == -1 && errno == EINTR);
855 freeaddrinfo(result);
856 return status == 0;
857 }
858
859
860 bool POSIXSocket::Shutdown() {
861 if (IsValid()) {
862 // Shutdown socket for both read and write.
863 int status = shutdown(socket_, SHUT_RDWR);
864 close(socket_);
865 socket_ = -1;
866 return status == 0;
867 }
868 return true;
869 }
870
871
872 int POSIXSocket::Send(const char* data, int len) const {
873 if (len <= 0) return 0;
874 int written = 0;
875 while (written < len) {
876 int status = send(socket_, data + written, len - written, 0);
877 if (status == 0) {
878 break;
879 } else if (status > 0) {
880 written += status;
881 } else if (errno != EINTR) {
882 return 0;
883 }
884 }
885 return written;
886 }
887
888
889 int POSIXSocket::Receive(char* data, int len) const {
890 if (len <= 0) return 0;
891 int status;
892 do {
893 status = recv(socket_, data, len, 0);
894 } while (status == -1 && errno == EINTR);
895 return (status < 0) ? 0 : status;
896 }
897
898
899 bool POSIXSocket::SetReuseAddress(bool reuse_address) {
900 int on = reuse_address ? 1 : 0;
901 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
902 return status == 0;
903 }
904
905
906 bool Socket::SetUp() {
907 // Nothing to do on POSIX.
908 return true;
909 }
910
911
912 int Socket::LastError() {
913 return errno;
914 }
915
916
917 uint16_t Socket::HToN(uint16_t value) {
918 return htons(value);
919 }
920
921
922 uint16_t Socket::NToH(uint16_t value) {
923 return ntohs(value);
924 }
925
926
927 uint32_t Socket::HToN(uint32_t value) {
928 return htonl(value);
929 }
930
931
932 uint32_t Socket::NToH(uint32_t value) {
933 return ntohl(value);
934 }
935
936
937 Socket* OS::CreateSocket() {
938 return new POSIXSocket();
939 }
940
941
942 } } // namespace v8::internal 744 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-openbsd.cc ('k') | src/platform-solaris.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698