| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | 2 |
| 3 #include "v8.h" | 3 #include "v8.h" |
| 4 #include "platform.h" | 4 #include "platform.h" |
| 5 #include "cctest.h" | 5 #include "cctest.h" |
| 6 | 6 |
| 7 | 7 |
| 8 using namespace ::v8::internal; | 8 using namespace ::v8::internal; |
| 9 | 9 |
| 10 | 10 |
| 11 static const char* kPort = "5858"; | 11 static const char* kPort = "5858"; |
| 12 static const char* kLocalhost = "localhost"; | 12 static const char* kLocalhost = "localhost"; |
| 13 | 13 |
| 14 class SocketListenerThread : public Thread { | 14 class SocketListenerThread : public Thread { |
| 15 public: | 15 public: |
| 16 SocketListenerThread(int data_size) : data_size_(data_size), | 16 explicit SocketListenerThread(int data_size) |
| 17 server_(NULL), client_(NULL), | 17 : data_size_(data_size), server_(NULL), client_(NULL), |
| 18 listening_(OS::CreateSemaphore(0)) { | 18 listening_(OS::CreateSemaphore(0)) { |
| 19 data_ = new char[data_size_]; | 19 data_ = new char[data_size_]; |
| 20 } | 20 } |
| 21 ~SocketListenerThread() { | 21 ~SocketListenerThread() { |
| 22 // Close both sockets. | 22 // Close both sockets. |
| 23 delete client_; | 23 delete client_; |
| 24 delete server_; | 24 delete server_; |
| 25 } | 25 } |
| 26 | 26 |
| 27 void Run(); | 27 void Run(); |
| 28 void WaitForListening() { listening_->Wait(); } | 28 void WaitForListening() { listening_->Wait(); } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 43 // Create the server socket and bind it to the requested port. | 43 // Create the server socket and bind it to the requested port. |
| 44 server_ = OS::CreateSocket(); | 44 server_ = OS::CreateSocket(); |
| 45 CHECK(server_ != NULL); | 45 CHECK(server_ != NULL); |
| 46 ok = server_->Bind(5858); | 46 ok = server_->Bind(5858); |
| 47 CHECK(ok); | 47 CHECK(ok); |
| 48 | 48 |
| 49 // Listen for new connections. | 49 // Listen for new connections. |
| 50 ok = server_->Listen(1); | 50 ok = server_->Listen(1); |
| 51 CHECK(ok); | 51 CHECK(ok); |
| 52 listening_->Signal(); | 52 listening_->Signal(); |
| 53 | 53 |
| 54 // Accept a connection. | 54 // Accept a connection. |
| 55 client_ = server_->Accept(); | 55 client_ = server_->Accept(); |
| 56 CHECK(client_ != NULL); | 56 CHECK(client_ != NULL); |
| 57 | 57 |
| 58 // Read the expected niumber of bytes of data. | 58 // Read the expected niumber of bytes of data. |
| 59 int bytes_read = 0; | 59 int bytes_read = 0; |
| 60 while (bytes_read < data_size_) { | 60 while (bytes_read < data_size_) { |
| 61 bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read); | 61 bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 static void SendAndReceive(char *data, int len) { | 66 static void SendAndReceive(char *data, int len) { |
| 67 bool ok; | 67 bool ok; |
| 68 | 68 |
| 69 // Create a socket listener. | 69 // Create a socket listener. |
| 70 SocketListenerThread* listener = new SocketListenerThread(len); | 70 SocketListenerThread* listener = new SocketListenerThread(len); |
| 71 listener->Start(); | 71 listener->Start(); |
| 72 listener->WaitForListening(); | 72 listener->WaitForListening(); |
| 73 | 73 |
| 74 // Connect and write some data. | 74 // Connect and write some data. |
| 75 Socket* client = OS::CreateSocket(); | 75 Socket* client = OS::CreateSocket(); |
| 76 CHECK(client != NULL); | 76 CHECK(client != NULL); |
| 77 ok = client->Connect(kLocalhost, kPort); | 77 ok = client->Connect(kLocalhost, kPort); |
| 78 CHECK(ok); | 78 CHECK(ok); |
| 79 | 79 |
| 80 // Send all the data. | 80 // Send all the data. |
| 81 ok = client->SendAll(data, len); | 81 ok = client->SendAll(data, len); |
| 82 CHECK(ok); | 82 CHECK(ok); |
| 83 | 83 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 125 } |
| 126 | 126 |
| 127 | 127 |
| 128 TEST(HToNNToH) { | 128 TEST(HToNNToH) { |
| 129 uint16_t x = 1234; | 129 uint16_t x = 1234; |
| 130 CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); | 130 CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); |
| 131 | 131 |
| 132 uint32_t y = 12345678; | 132 uint32_t y = 12345678; |
| 133 CHECK(y == Socket::NToH(Socket::HToN(y))); | 133 CHECK(y == Socket::NToH(Socket::HToN(y))); |
| 134 } | 134 } |
| OLD | NEW |