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

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

Issue 27085: Add socket support to platform code.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 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-freebsd.cc ('k') | src/platform-macos.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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 14 matching lines...) Expand all
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Platform specific code for Linux goes here 28 // Platform specific code for Linux goes here
29 29
30 #include <pthread.h> 30 #include <pthread.h>
31 #include <semaphore.h> 31 #include <semaphore.h>
32 #include <signal.h> 32 #include <signal.h>
33 #include <sys/time.h> 33 #include <sys/time.h>
34 #include <sys/resource.h> 34 #include <sys/resource.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
35 #include <stdlib.h> 37 #include <stdlib.h>
36 38
37 // Ubuntu Dapper requires memory pages to be marked as 39 // Ubuntu Dapper requires memory pages to be marked as
38 // executable. Otherwise, OS raises an exception when executing code 40 // executable. Otherwise, OS raises an exception when executing code
39 // in that page. 41 // in that page.
40 #include <sys/types.h> // mmap & munmap 42 #include <sys/types.h> // mmap & munmap
41 #include <sys/mman.h> // mmap & munmap 43 #include <sys/mman.h> // mmap & munmap
42 #include <sys/stat.h> // open 44 #include <sys/stat.h> // open
43 #include <sys/fcntl.h> // open 45 #include <sys/fcntl.h> // open
44 #include <unistd.h> // getpagesize 46 #include <unistd.h> // getpagesize
45 #include <execinfo.h> // backtrace, backtrace_symbols 47 #include <execinfo.h> // backtrace, backtrace_symbols
46 #include <strings.h> // index 48 #include <strings.h> // index
47 #include <errno.h> 49 #include <errno.h>
48 #include <stdarg.h> 50 #include <stdarg.h>
49 51
52 #include <arpa/inet.h>
53 #include <netinet/in.h>
54 #include <netdb.h>
55
50 #undef MAP_TYPE 56 #undef MAP_TYPE
51 57
52 #include "v8.h" 58 #include "v8.h"
53 59
54 #include "platform.h" 60 #include "platform.h"
55 61
56 62
57 namespace v8 { namespace internal { 63 namespace v8 { namespace internal {
58 64
59 // 0 is never a valid thread id on Linux since tids and pids share a 65 // 0 is never a valid thread id on Linux since tids and pids share a
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 int result = sem_wait(&sem_); 602 int result = sem_wait(&sem_);
597 if (result == 0) return; // Successfully got semaphore. 603 if (result == 0) return; // Successfully got semaphore.
598 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. 604 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
599 } 605 }
600 } 606 }
601 607
602 Semaphore* OS::CreateSemaphore(int count) { 608 Semaphore* OS::CreateSemaphore(int count) {
603 return new LinuxSemaphore(count); 609 return new LinuxSemaphore(count);
604 } 610 }
605 611
612
613 // ----------------------------------------------------------------------------
614 // Linux socket support.
615 //
616
617 class LinuxSocket : public Socket {
618 public:
619 explicit LinuxSocket() {
620 // Create the socket.
621 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
622 }
623 explicit LinuxSocket(int socket): socket_(socket) { }
624
625
626 virtual ~LinuxSocket() {
627 if (IsValid()) {
628 // Close socket.
629 close(socket_);
630 }
631 }
632
633 // Server initialization.
634 bool Bind (const int port);
635 bool Listen(int backlog) const;
636 Socket* Accept () const;
637
638 // Client initialization.
639 bool Connect(const char* host, const char* port);
640
641 // Data Transimission
642 int Send(const char* data, int len) const;
643 bool SendAll(const char* data, int len) const;
644 int Receive(char* data, int len) const;
645
646 bool IsValid() const { return socket_ != -1; }
647
648 private:
649 int socket_;
650 };
651
652
653 bool LinuxSocket::Bind(const int port) {
654 if (!IsValid()) {
655 return false;
656 }
657
658 sockaddr_in addr;
659 memset(&addr, 0, sizeof(addr));
660 addr.sin_family = AF_INET;
661 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
662 addr.sin_port = htons(port);
663 int status = bind(socket_,
664 reinterpret_cast<struct sockaddr *>(&addr),
665 sizeof (addr));
666 return status == 0;
667 }
668
669
670 bool LinuxSocket::Listen(int backlog) const {
671 if (!IsValid()) {
672 return false;
673 }
674
675 int status = listen(socket_, backlog);
676 return status == 0;
677 }
678
679
680 Socket* LinuxSocket::Accept() const {
681 if (!IsValid()) {
682 return NULL;
683 }
684
685 int socket = accept(socket_, NULL, NULL);
686 if (socket == -1) {
687 return NULL;
688 } else {
689 return new LinuxSocket(socket);
690 }
691 }
692
693
694 bool LinuxSocket::Connect(const char* host, const char* port) {
695 if (!IsValid()) {
696 return false;
697 }
698
699 // Lookup host and port.
700 struct addrinfo *result = NULL;
701 struct addrinfo hints;
702 memset(&hints, 0, sizeof(addrinfo));
703 hints.ai_family = AF_INET;
704 hints.ai_socktype = SOCK_STREAM;
705 hints.ai_protocol = IPPROTO_TCP;
706 int status = getaddrinfo(host, port, &hints, &result);
707 if (status != 0) {
708 return false;
709 }
710
711 // Connect.
712 status = connect(socket_, result->ai_addr, result->ai_addrlen);
713 return status == 0;
714 }
715
716
717 int LinuxSocket::Send(const char* data, int len) const {
718 int status = send(socket_, data, len, 0);
719 return status;
720 }
721
722
723 bool LinuxSocket::SendAll(const char* data, int len) const {
724 int sent_len = 0;
725 while (sent_len < len) {
726 int status = Send(data, len);
727 if (status <= 0) {
728 return false;
729 }
730 sent_len += status;
731 }
732 return true;
733 }
734
735
736 int LinuxSocket::Receive(char* data, int len) const {
737 int status = recv(socket_, data, len, 0);
738 return status;
739 }
740
741
742 bool Socket::Setup() {
743 // Nothing to do on Linux.
744 return true;
745 }
746
747
748 int Socket::LastError() {
749 return errno;
750 }
751
752
753 uint16_t Socket::HToN(uint16_t value) {
754 return htons(value);
755 }
756
757
758 uint16_t Socket::NToH(uint16_t value) {
759 return ntohs(value);
760 }
761
762
763 uint32_t Socket::HToN(uint32_t value) {
764 return htonl(value);
765 }
766
767
768 uint32_t Socket::NToH(uint32_t value) {
769 return ntohl(value);
770 }
771
772
773 Socket* OS::CreateSocket() {
774 return new LinuxSocket();
775 }
776
777
606 #ifdef ENABLE_LOGGING_AND_PROFILING 778 #ifdef ENABLE_LOGGING_AND_PROFILING
607 779
608 static Sampler* active_sampler_ = NULL; 780 static Sampler* active_sampler_ = NULL;
609 781
610 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { 782 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
611 USE(info); 783 USE(info);
612 if (signal != SIGPROF) return; 784 if (signal != SIGPROF) return;
613 if (active_sampler_ == NULL) return; 785 if (active_sampler_ == NULL) return;
614 786
615 TickSample sample; 787 TickSample sample;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 } 868 }
697 869
698 // This sampler is no longer the active sampler. 870 // This sampler is no longer the active sampler.
699 active_sampler_ = NULL; 871 active_sampler_ = NULL;
700 active_ = false; 872 active_ = false;
701 } 873 }
702 874
703 #endif // ENABLE_LOGGING_AND_PROFILING 875 #endif // ENABLE_LOGGING_AND_PROFILING
704 876
705 } } // namespace v8::internal 877 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-freebsd.cc ('k') | src/platform-macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698