1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 27 28 #ifndef V8_PLATFORM_SOCKET_H_ 29 #define V8_PLATFORM_SOCKET_H_ 30 31 #include "globals.h" 32 #if V8_OS_WIN 33 #include "win32-headers.h" 34 #endif 35 36 namespace v8 { 37 namespace internal { 38 39 // ---------------------------------------------------------------------------- 40 // Socket 41 // 42 43 class Socket V8_FINAL { 44 public: 45 Socket(); ~Socket()46 ~Socket() { Shutdown(); } 47 48 // Server initialization. 49 bool Bind(int port) V8_WARN_UNUSED_RESULT; 50 bool Listen(int backlog) V8_WARN_UNUSED_RESULT; 51 Socket* Accept() V8_WARN_UNUSED_RESULT; 52 53 // Client initialization. 54 bool Connect(const char* host, const char* port) V8_WARN_UNUSED_RESULT; 55 56 // Shutdown socket for both read and write. This causes blocking Send and 57 // Receive calls to exit. After |Shutdown()| the Socket object cannot be 58 // used for any communication. 59 bool Shutdown(); 60 61 // Data Transimission 62 // Return 0 on failure. 63 int Send(const char* buffer, int length) V8_WARN_UNUSED_RESULT; 64 int Receive(char* buffer, int length) V8_WARN_UNUSED_RESULT; 65 66 // Set the value of the SO_REUSEADDR socket option. 67 bool SetReuseAddress(bool reuse_address); 68 IsValid()69 V8_INLINE bool IsValid() const { 70 return native_handle_ != kInvalidNativeHandle; 71 } 72 73 static int GetLastError(); 74 75 // The implementation-defined native handle type. 76 #if V8_OS_POSIX 77 typedef int NativeHandle; 78 static const NativeHandle kInvalidNativeHandle = -1; 79 #elif V8_OS_WIN 80 typedef SOCKET NativeHandle; 81 static const NativeHandle kInvalidNativeHandle = INVALID_SOCKET; 82 #endif 83 native_handle()84 NativeHandle& native_handle() { 85 return native_handle_; 86 } native_handle()87 const NativeHandle& native_handle() const { 88 return native_handle_; 89 } 90 91 private: Socket(NativeHandle native_handle)92 explicit Socket(NativeHandle native_handle) : native_handle_(native_handle) {} 93 94 NativeHandle native_handle_; 95 96 DISALLOW_COPY_AND_ASSIGN(Socket); 97 }; 98 99 } } // namespace v8::internal 100 101 #endif // V8_PLATFORM_SOCKET_H_ 102