OLD | NEW |
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 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 | 732 |
733 | 733 |
734 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | 734 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
735 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); | 735 pthread_key_t pthread_key = LocalKeyToPthreadKey(key); |
736 int result = pthread_setspecific(pthread_key, value); | 736 int result = pthread_setspecific(pthread_key, value); |
737 ASSERT_EQ(0, result); | 737 ASSERT_EQ(0, result); |
738 USE(result); | 738 USE(result); |
739 } | 739 } |
740 | 740 |
741 | 741 |
742 // ---------------------------------------------------------------------------- | |
743 // POSIX socket support. | |
744 // | |
745 | |
746 class POSIXSocket : public Socket { | |
747 public: | |
748 explicit POSIXSocket() { | |
749 // Create the socket. | |
750 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
751 if (IsValid()) { | |
752 // Allow rapid reuse. | |
753 static const int kOn = 1; | |
754 int ret = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, | |
755 &kOn, sizeof(kOn)); | |
756 ASSERT(ret == 0); | |
757 USE(ret); | |
758 } | |
759 } | |
760 explicit POSIXSocket(int socket): socket_(socket) { } | |
761 virtual ~POSIXSocket() { Shutdown(); } | |
762 | |
763 // Server initialization. | |
764 bool Bind(const int port); | |
765 bool Listen(int backlog) const; | |
766 Socket* Accept() const; | |
767 | |
768 // Client initialization. | |
769 bool Connect(const char* host, const char* port); | |
770 | |
771 // Shutdown socket for both read and write. | |
772 bool Shutdown(); | |
773 | |
774 // Data Transimission | |
775 int Send(const char* data, int len) const; | |
776 int Receive(char* data, int len) const; | |
777 | |
778 bool SetReuseAddress(bool reuse_address); | |
779 | |
780 bool IsValid() const { return socket_ != -1; } | |
781 | |
782 private: | |
783 int socket_; | |
784 }; | |
785 | |
786 | |
787 bool POSIXSocket::Bind(const int port) { | |
788 if (!IsValid()) { | |
789 return false; | |
790 } | |
791 | |
792 sockaddr_in addr; | |
793 memset(&addr, 0, sizeof(addr)); | |
794 addr.sin_family = AF_INET; | |
795 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
796 addr.sin_port = htons(port); | |
797 int status = bind(socket_, | |
798 BitCast<struct sockaddr *>(&addr), | |
799 sizeof(addr)); | |
800 return status == 0; | |
801 } | |
802 | |
803 | |
804 bool POSIXSocket::Listen(int backlog) const { | |
805 if (!IsValid()) { | |
806 return false; | |
807 } | |
808 | |
809 int status = listen(socket_, backlog); | |
810 return status == 0; | |
811 } | |
812 | |
813 | |
814 Socket* POSIXSocket::Accept() const { | |
815 if (!IsValid()) { | |
816 return NULL; | |
817 } | |
818 | |
819 int socket; | |
820 do { | |
821 socket = accept(socket_, NULL, NULL); | |
822 } while (socket == -1 && errno == EINTR); | |
823 | |
824 if (socket == -1) { | |
825 return NULL; | |
826 } else { | |
827 return new POSIXSocket(socket); | |
828 } | |
829 } | |
830 | |
831 | |
832 bool POSIXSocket::Connect(const char* host, const char* port) { | |
833 if (!IsValid()) { | |
834 return false; | |
835 } | |
836 | |
837 // Lookup host and port. | |
838 struct addrinfo *result = NULL; | |
839 struct addrinfo hints; | |
840 memset(&hints, 0, sizeof(addrinfo)); | |
841 hints.ai_family = AF_INET; | |
842 hints.ai_socktype = SOCK_STREAM; | |
843 hints.ai_protocol = IPPROTO_TCP; | |
844 int status = getaddrinfo(host, port, &hints, &result); | |
845 if (status != 0) { | |
846 return false; | |
847 } | |
848 | |
849 // Connect. | |
850 do { | |
851 status = connect(socket_, result->ai_addr, result->ai_addrlen); | |
852 } while (status == -1 && errno == EINTR); | |
853 freeaddrinfo(result); | |
854 return status == 0; | |
855 } | |
856 | |
857 | |
858 bool POSIXSocket::Shutdown() { | |
859 if (IsValid()) { | |
860 // Shutdown socket for both read and write. | |
861 int status = shutdown(socket_, SHUT_RDWR); | |
862 close(socket_); | |
863 socket_ = -1; | |
864 return status == 0; | |
865 } | |
866 return true; | |
867 } | |
868 | |
869 | |
870 int POSIXSocket::Send(const char* data, int len) const { | |
871 if (len <= 0) return 0; | |
872 int written = 0; | |
873 while (written < len) { | |
874 int status = send(socket_, data + written, len - written, 0); | |
875 if (status == 0) { | |
876 break; | |
877 } else if (status > 0) { | |
878 written += status; | |
879 } else if (errno != EINTR) { | |
880 return 0; | |
881 } | |
882 } | |
883 return written; | |
884 } | |
885 | |
886 | |
887 int POSIXSocket::Receive(char* data, int len) const { | |
888 if (len <= 0) return 0; | |
889 int status; | |
890 do { | |
891 status = recv(socket_, data, len, 0); | |
892 } while (status == -1 && errno == EINTR); | |
893 return (status < 0) ? 0 : status; | |
894 } | |
895 | |
896 | |
897 bool POSIXSocket::SetReuseAddress(bool reuse_address) { | |
898 int on = reuse_address ? 1 : 0; | |
899 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
900 return status == 0; | |
901 } | |
902 | |
903 | |
904 bool Socket::SetUp() { | |
905 // Nothing to do on POSIX. | |
906 return true; | |
907 } | |
908 | |
909 | |
910 int Socket::LastError() { | |
911 return errno; | |
912 } | |
913 | |
914 | |
915 uint16_t Socket::HToN(uint16_t value) { | |
916 return htons(value); | |
917 } | |
918 | |
919 | |
920 uint16_t Socket::NToH(uint16_t value) { | |
921 return ntohs(value); | |
922 } | |
923 | |
924 | |
925 uint32_t Socket::HToN(uint32_t value) { | |
926 return htonl(value); | |
927 } | |
928 | |
929 | |
930 uint32_t Socket::NToH(uint32_t value) { | |
931 return ntohl(value); | |
932 } | |
933 | |
934 | |
935 Socket* OS::CreateSocket() { | |
936 return new POSIXSocket(); | |
937 } | |
938 | |
939 | |
940 } } // namespace v8::internal | 742 } } // namespace v8::internal |
OLD | NEW |