OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 23 matching lines...) Expand all Loading... |
34 | 34 |
35 | 35 |
36 class SocketListenerThread : public Thread { | 36 class SocketListenerThread : public Thread { |
37 public: | 37 public: |
38 SocketListenerThread(int port, int data_size) | 38 SocketListenerThread(int port, int data_size) |
39 : Thread("SocketListenerThread"), | 39 : Thread("SocketListenerThread"), |
40 port_(port), | 40 port_(port), |
41 data_size_(data_size), | 41 data_size_(data_size), |
42 server_(NULL), | 42 server_(NULL), |
43 client_(NULL), | 43 client_(NULL), |
44 listening_(OS::CreateSemaphore(0)) { | 44 listening_(0) { |
45 data_ = new char[data_size_]; | 45 data_ = new char[data_size_]; |
46 } | 46 } |
47 ~SocketListenerThread() { | 47 ~SocketListenerThread() { |
48 // Close both sockets. | 48 // Close both sockets. |
49 delete client_; | 49 delete client_; |
50 delete server_; | 50 delete server_; |
51 delete listening_; | |
52 delete[] data_; | 51 delete[] data_; |
53 } | 52 } |
54 | 53 |
55 void Run(); | 54 void Run(); |
56 void WaitForListening() { listening_->Wait(); } | 55 void WaitForListening() { listening_.Wait(); } |
57 char* data() { return data_; } | 56 char* data() { return data_; } |
58 | 57 |
59 private: | 58 private: |
60 int port_; | 59 int port_; |
61 char* data_; | 60 char* data_; |
62 int data_size_; | 61 int data_size_; |
63 Socket* server_; // Server socket used for bind/accept. | 62 Socket* server_; // Server socket used for bind/accept. |
64 Socket* client_; // Single client connection used by the test. | 63 Socket* client_; // Single client connection used by the test. |
65 Semaphore* listening_; // Signalled when the server socket is in listen mode. | 64 Semaphore listening_; // Signalled when the server socket is in listen mode. |
66 }; | 65 }; |
67 | 66 |
68 | 67 |
69 void SocketListenerThread::Run() { | 68 void SocketListenerThread::Run() { |
70 bool ok; | 69 bool ok; |
71 | 70 |
72 // Create the server socket and bind it to the requested port. | 71 // Create the server socket and bind it to the requested port. |
73 server_ = OS::CreateSocket(); | 72 server_ = OS::CreateSocket(); |
74 server_->SetReuseAddress(true); | 73 server_->SetReuseAddress(true); |
75 CHECK(server_ != NULL); | 74 CHECK(server_ != NULL); |
76 ok = server_->Bind(port_); | 75 ok = server_->Bind(port_); |
77 CHECK(ok); | 76 CHECK(ok); |
78 | 77 |
79 // Listen for new connections. | 78 // Listen for new connections. |
80 ok = server_->Listen(1); | 79 ok = server_->Listen(1); |
81 CHECK(ok); | 80 CHECK(ok); |
82 listening_->Signal(); | 81 listening_.Signal(); |
83 | 82 |
84 // Accept a connection. | 83 // Accept a connection. |
85 client_ = server_->Accept(); | 84 client_ = server_->Accept(); |
86 CHECK(client_ != NULL); | 85 CHECK(client_ != NULL); |
87 | 86 |
88 // Read the expected niumber of bytes of data. | 87 // Read the expected niumber of bytes of data. |
89 int bytes_read = 0; | 88 int bytes_read = 0; |
90 while (bytes_read < data_size_) { | 89 while (bytes_read < data_size_) { |
91 bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read); | 90 bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read); |
92 } | 91 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 181 } |
183 | 182 |
184 | 183 |
185 TEST(HToNNToH) { | 184 TEST(HToNNToH) { |
186 uint16_t x = 1234; | 185 uint16_t x = 1234; |
187 CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); | 186 CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); |
188 | 187 |
189 uint32_t y = 12345678; | 188 uint32_t y = 12345678; |
190 CHECK(y == Socket::NToH(Socket::HToN(y))); | 189 CHECK(y == Socket::NToH(Socket::HToN(y))); |
191 } | 190 } |
OLD | NEW |