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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // Terminate the session. | 143 // Terminate the session. |
144 ScopedLock with(session_access_); | 144 ScopedLock with(session_access_); |
145 ASSERT(session == session_); | 145 ASSERT(session == session_); |
146 if (session == session_) { | 146 if (session == session_) { |
147 CloseSession(); | 147 CloseSession(); |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 | 151 |
152 void DebuggerAgentSession::Run() { | 152 void DebuggerAgentSession::Run() { |
| 153 // Send the hello message. |
| 154 bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_); |
| 155 if (!ok) return; |
| 156 |
153 while (true) { | 157 while (true) { |
154 // Read data from the debugger front end. | 158 // Read data from the debugger front end. |
155 SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_); | 159 SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_); |
156 if (*message == NULL) { | 160 if (*message == NULL) { |
157 // Session is closed. | 161 // Session is closed. |
158 agent_->OnSessionClosed(this); | 162 agent_->OnSessionClosed(this); |
159 return; | 163 return; |
160 } | 164 } |
161 | 165 |
162 // Convert UTF-8 to UTF-16. | 166 // Convert UTF-8 to UTF-16. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 if (strlen(value) > 7) { | 249 if (strlen(value) > 7) { |
246 return SmartPointer<char>(); | 250 return SmartPointer<char>(); |
247 } | 251 } |
248 for (int i = 0; value[i] != '\0'; i++) { | 252 for (int i = 0; value[i] != '\0'; i++) { |
249 // Bail out if illegal data. | 253 // Bail out if illegal data. |
250 if (value[i] < '0' || value[i] > '9') { | 254 if (value[i] < '0' || value[i] > '9') { |
251 return SmartPointer<char>(); | 255 return SmartPointer<char>(); |
252 } | 256 } |
253 content_length = 10 * content_length + (value[i] - '0'); | 257 content_length = 10 * content_length + (value[i] - '0'); |
254 } | 258 } |
| 259 } else { |
| 260 // For now just print all other headers than Content-Length. |
| 261 PrintF("%s: %s\n", key, value); |
255 } | 262 } |
256 | 263 |
257 // Start collecting new header. | 264 // Start collecting new header. |
258 header_buffer_position = 0; | 265 header_buffer_position = 0; |
259 } else { | 266 } else { |
260 // Add character to header buffer (reserve room for terminating '\0'). | 267 // Add character to header buffer (reserve room for terminating '\0'). |
261 if (header_buffer_position < kHeaderBufferSize - 1) { | 268 if (header_buffer_position < kHeaderBufferSize - 1) { |
262 header_buffer[header_buffer_position++] = c; | 269 header_buffer[header_buffer_position++] = c; |
263 } | 270 } |
264 } | 271 } |
265 } | 272 } |
266 | 273 |
| 274 // Return now if no body. |
| 275 if (content_length == 0) { |
| 276 return SmartPointer<char>(); |
| 277 } |
| 278 |
267 // Read body. | 279 // Read body. |
268 char* buffer = NewArray<char>(content_length + 1); | 280 char* buffer = NewArray<char>(content_length + 1); |
269 received = ReceiveAll(conn, buffer, content_length); | 281 received = ReceiveAll(conn, buffer, content_length); |
270 if (received < content_length) { | 282 if (received < content_length) { |
271 PrintF("Error %d\n", Socket::LastError()); | 283 PrintF("Error %d\n", Socket::LastError()); |
272 return SmartPointer<char>(); | 284 return SmartPointer<char>(); |
273 } | 285 } |
274 buffer[content_length] = '\0'; | 286 buffer[content_length] = '\0'; |
275 | 287 |
276 return SmartPointer<char>(buffer); | 288 return SmartPointer<char>(buffer); |
277 } | 289 } |
278 | 290 |
279 | 291 |
| 292 bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, |
| 293 const char* embedding_host) { |
| 294 static const int kBufferSize = 80; |
| 295 char buffer[kBufferSize]; // Sending buffer. |
| 296 bool ok; |
| 297 int len; |
| 298 |
| 299 // Send the header. |
| 300 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 301 "Type: connect\n"); |
| 302 ok = conn->Send(buffer, len); |
| 303 if (!ok) return false; |
| 304 |
| 305 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 306 "V8-Version: %s\n", v8::V8::GetVersion()); |
| 307 ok = conn->Send(buffer, len); |
| 308 if (!ok) return false; |
| 309 |
| 310 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 311 "Protocol-Version: 1\n"); |
| 312 ok = conn->Send(buffer, len); |
| 313 if (!ok) return false; |
| 314 |
| 315 if (embedding_host != NULL) { |
| 316 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 317 "Embedding-Host: %s\n", embedding_host); |
| 318 ok = conn->Send(buffer, len); |
| 319 if (!ok) return false; |
| 320 } |
| 321 |
| 322 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 323 "%s: 0\n", kContentLength); |
| 324 ok = conn->Send(buffer, len); |
| 325 if (!ok) return false; |
| 326 |
| 327 // Terminate header with empty line. |
| 328 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); |
| 329 ok = conn->Send(buffer, len); |
| 330 if (!ok) return false; |
| 331 |
| 332 // No body for connect message. |
| 333 |
| 334 return true; |
| 335 } |
| 336 |
| 337 |
280 bool DebuggerAgentUtil::SendMessage(const Socket* conn, | 338 bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
281 const Vector<uint16_t> message) { | 339 const Vector<uint16_t> message) { |
282 static const int kBufferSize = 80; | 340 static const int kBufferSize = 80; |
283 char buffer[kBufferSize]; // Sending buffer both for header and body. | 341 char buffer[kBufferSize]; // Sending buffer both for header and body. |
284 | 342 |
285 // Calculate the message size in UTF-8 encoding. | 343 // Calculate the message size in UTF-8 encoding. |
286 int utf8_len = 0; | 344 int utf8_len = 0; |
287 for (int i = 0; i < message.length(); i++) { | 345 for (int i = 0; i < message.length(); i++) { |
288 utf8_len += unibrow::Utf8::Length(message[i]); | 346 utf8_len += unibrow::Utf8::Length(message[i]); |
289 } | 347 } |
290 | 348 |
291 // Send the header. | 349 // Send the header. |
292 int len; | 350 int len; |
293 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 351 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
294 "Content-Length: %d\n", utf8_len); | 352 "%s: %d\n", kContentLength, utf8_len); |
295 conn->Send(buffer, len); | 353 conn->Send(buffer, len); |
296 | 354 |
297 // Terminate header with empty line. | 355 // Terminate header with empty line. |
298 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); | 356 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n"); |
299 conn->Send(buffer, len); | 357 conn->Send(buffer, len); |
300 | 358 |
301 // Send message body as UTF-8. | 359 // Send message body as UTF-8. |
302 int buffer_position = 0; // Current buffer position. | 360 int buffer_position = 0; // Current buffer position. |
303 for (int i = 0; i < message.length(); i++) { | 361 for (int i = 0; i < message.length(); i++) { |
304 // Write next UTF-8 encoded character to buffer. | 362 // Write next UTF-8 encoded character to buffer. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 if (received <= 0) { | 408 if (received <= 0) { |
351 return total_received; | 409 return total_received; |
352 } | 410 } |
353 total_received += received; | 411 total_received += received; |
354 } | 412 } |
355 return total_received; | 413 return total_received; |
356 } | 414 } |
357 | 415 |
358 | 416 |
359 } } // namespace v8::internal | 417 } } // namespace v8::internal |
OLD | NEW |