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

Side by Side Diff: src/platform-macos.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-linux.cc ('k') | src/platform-win32.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 27 matching lines...) Expand all
38 # include <execinfo.h> // backtrace, backtrace_symbols 38 # include <execinfo.h> // backtrace, backtrace_symbols
39 #endif 39 #endif
40 40
41 #include <pthread.h> 41 #include <pthread.h>
42 #include <semaphore.h> 42 #include <semaphore.h>
43 #include <signal.h> 43 #include <signal.h>
44 #include <mach/semaphore.h> 44 #include <mach/semaphore.h>
45 #include <mach/task.h> 45 #include <mach/task.h>
46 #include <sys/time.h> 46 #include <sys/time.h>
47 #include <sys/resource.h> 47 #include <sys/resource.h>
48 #include <sys/socket.h>
49 #include <sys/types.h>
48 #include <stdarg.h> 50 #include <stdarg.h>
49 #include <stdlib.h> 51 #include <stdlib.h>
50 52
53 #include <arpa/inet.h>
54 #include <netinet/in.h>
55 #include <netdb.h>
56 #include <errno.h>
57
51 #undef MAP_TYPE 58 #undef MAP_TYPE
52 59
53 #include "v8.h" 60 #include "v8.h"
54 61
55 #include "platform.h" 62 #include "platform.h"
56 63
57 namespace v8 { namespace internal { 64 namespace v8 { namespace internal {
58 65
59 // 0 is never a valid thread id on MacOSX since a ptread_t is 66 // 0 is never a valid thread id on MacOSX since a ptread_t is
60 // a pointer. 67 // a pointer.
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 569
563 private: 570 private:
564 semaphore_t semaphore_; 571 semaphore_t semaphore_;
565 }; 572 };
566 573
567 574
568 Semaphore* OS::CreateSemaphore(int count) { 575 Semaphore* OS::CreateSemaphore(int count) {
569 return new MacOSSemaphore(count); 576 return new MacOSSemaphore(count);
570 } 577 }
571 578
579
580 // ----------------------------------------------------------------------------
581 // MacOS socket support.
582 //
583
584 class MacOSSocket : public Socket {
585 public:
586 explicit MacOSSocket() {
587 // Create the socket.
588 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
589 }
590 explicit MacOSSocket(int socket): socket_(socket) { }
591
592
593 virtual ~MacOSSocket() {
594 if (IsValid()) {
595 // Close socket.
596 close(socket_);
597 }
598 }
599
600 // Server initialization.
601 bool Bind (const int port);
602 bool Listen(int backlog) const;
603 Socket* Accept () const;
604
605 // Client initialization.
606 bool Connect(const char* host, const char* port);
607
608 // Data Transimission
609 int Send(const char* data, int len) const;
610 bool SendAll(const char* data, int len) const;
611 int Receive(char* data, int len) const;
612
613 bool IsValid() const { return socket_ != -1; }
614
615 private:
616 int socket_;
617 };
618
619
620 bool MacOSSocket::Bind(const int port) {
621 if (!IsValid()) {
622 return false;
623 }
624
625 int on = 1;
626 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
627 if (status != 0) {
628 return false;
629 }
630
631 sockaddr_in addr;
632 memset(&addr, 0, sizeof(addr));
633 addr.sin_family = AF_INET;
634 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
635 addr.sin_port = htons(port);
636 status = bind(socket_,
637 reinterpret_cast<struct sockaddr *>(&addr),
638 sizeof (addr));
639 return status == 0;
640 }
641
642
643 bool MacOSSocket::Listen(int backlog) const {
644 if (!IsValid()) {
645 return false;
646 }
647
648 int status = listen(socket_, backlog);
649 return status == 0;
650 }
651
652
653 Socket* MacOSSocket::Accept() const {
654 if (!IsValid()) {
655 return NULL;
656 }
657
658 int socket = accept(socket_, NULL, NULL);
659 if (socket == -1) {
660 return NULL;
661 } else {
662 return new MacOSSocket(socket);
663 }
664 }
665
666
667 bool MacOSSocket::Connect(const char* host, const char* port) {
668 if (!IsValid()) {
669 return false;
670 }
671
672 // Lookup host and port.
673 struct addrinfo *result = NULL;
674 struct addrinfo hints;
675 memset(&hints, 0, sizeof(addrinfo));
676 hints.ai_family = AF_INET;
677 hints.ai_socktype = SOCK_STREAM;
678 hints.ai_protocol = IPPROTO_TCP;
679 int status = getaddrinfo(host, port, &hints, &result);
680 if (status != 0) {
681 return false;
682 }
683
684 // Connect.
685 status = connect(socket_, result->ai_addr, result->ai_addrlen);
686 return status == 0;
687 }
688
689
690 int MacOSSocket::Send(const char* data, int len) const {
691 int status = send(socket_, data, len, 0);
692 return status;
693 }
694
695
696 bool MacOSSocket::SendAll(const char* data, int len) const {
697 int sent_len = 0;
698 while (sent_len < len) {
699 int status = Send(data, len);
700 if (status <= 0) {
701 return false;
702 }
703 sent_len += status;
704 }
705 return true;
706 }
707
708
709 int MacOSSocket::Receive(char* data, int len) const {
710 int status = recv(socket_, data, len, 0);
711 return status;
712 }
713
714
715 bool Socket::Setup() {
716 // Nothing to do on MacOS.
717 return true;
718 }
719
720
721 int Socket::LastError() {
722 return errno;
723 }
724
725
726 uint16_t Socket::HToN(uint16_t value) {
727 return htons(value);
728 }
729
730
731 uint16_t Socket::NToH(uint16_t value) {
732 return ntohs(value);
733 }
734
735
736 uint32_t Socket::HToN(uint32_t value) {
737 return htonl(value);
738 }
739
740
741 uint32_t Socket::NToH(uint32_t value) {
742 return ntohl(value);
743 }
744
745
746 Socket* OS::CreateSocket() {
747 return new MacOSSocket();
748 }
749
750
572 #ifdef ENABLE_LOGGING_AND_PROFILING 751 #ifdef ENABLE_LOGGING_AND_PROFILING
573 752
574 static Sampler* active_sampler_ = NULL; 753 static Sampler* active_sampler_ = NULL;
575 754
576 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { 755 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
577 USE(info); 756 USE(info);
578 if (signal != SIGPROF) return; 757 if (signal != SIGPROF) return;
579 if (active_sampler_ == NULL) return; 758 if (active_sampler_ == NULL) return;
580 759
581 TickSample sample; 760 TickSample sample;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 } 841 }
663 842
664 // This sampler is no longer the active sampler. 843 // This sampler is no longer the active sampler.
665 active_sampler_ = NULL; 844 active_sampler_ = NULL;
666 active_ = false; 845 active_ = false;
667 } 846 }
668 847
669 #endif // ENABLE_LOGGING_AND_PROFILING 848 #endif // ENABLE_LOGGING_AND_PROFILING
670 849
671 } } // namespace v8::internal 850 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-linux.cc ('k') | src/platform-win32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698