| 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 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } | 365 } |
| 366 | 366 |
| 367 | 367 |
| 368 bool DebuggerAgentUtil::SendMessage(const Socket* conn, | 368 bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 369 const Vector<uint16_t> message) { | 369 const Vector<uint16_t> message) { |
| 370 static const int kBufferSize = 80; | 370 static const int kBufferSize = 80; |
| 371 char buffer[kBufferSize]; // Sending buffer both for header and body. | 371 char buffer[kBufferSize]; // Sending buffer both for header and body. |
| 372 | 372 |
| 373 // Calculate the message size in UTF-8 encoding. | 373 // Calculate the message size in UTF-8 encoding. |
| 374 int utf8_len = 0; | 374 int utf8_len = 0; |
| 375 int previous = unibrow::Utf16::kNoPreviousCharacter; |
| 375 for (int i = 0; i < message.length(); i++) { | 376 for (int i = 0; i < message.length(); i++) { |
| 376 utf8_len += unibrow::Utf8::Length(message[i]); | 377 uint16_t character = message[i]; |
| 378 utf8_len += unibrow::Utf8::Length(character, previous); |
| 379 previous = character; |
| 377 } | 380 } |
| 378 | 381 |
| 379 // Send the header. | 382 // Send the header. |
| 380 int len; | 383 int len; |
| 381 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), | 384 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 382 "%s: %d\r\n", kContentLength, utf8_len); | 385 "%s: %d\r\n", kContentLength, utf8_len); |
| 383 conn->Send(buffer, len); | 386 conn->Send(buffer, len); |
| 384 | 387 |
| 385 // Terminate header with empty line. | 388 // Terminate header with empty line. |
| 386 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); | 389 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
| 387 conn->Send(buffer, len); | 390 conn->Send(buffer, len); |
| 388 | 391 |
| 389 // Send message body as UTF-8. | 392 // Send message body as UTF-8. |
| 390 int buffer_position = 0; // Current buffer position. | 393 int buffer_position = 0; // Current buffer position. |
| 394 previous = unibrow::Utf16::kNoPreviousCharacter; |
| 391 for (int i = 0; i < message.length(); i++) { | 395 for (int i = 0; i < message.length(); i++) { |
| 392 // Write next UTF-8 encoded character to buffer. | 396 // Write next UTF-8 encoded character to buffer. |
| 397 uint16_t character = message[i]; |
| 393 buffer_position += | 398 buffer_position += |
| 394 unibrow::Utf8::Encode(buffer + buffer_position, message[i]); | 399 unibrow::Utf8::Encode(buffer + buffer_position, character, previous); |
| 395 ASSERT(buffer_position < kBufferSize); | 400 ASSERT(buffer_position < kBufferSize); |
| 396 | 401 |
| 397 // Send buffer if full or last character is encoded. | 402 // Send buffer if full or last character is encoded. |
| 398 if (kBufferSize - buffer_position < 3 || i == message.length() - 1) { | 403 if (kBufferSize - buffer_position < |
| 399 conn->Send(buffer, buffer_position); | 404 unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit || |
| 400 buffer_position = 0; | 405 i == message.length() - 1) { |
| 406 if (unibrow::Utf16::IsLeadSurrogate(character)) { |
| 407 const int kEncodedSurrogateLength = |
| 408 unibrow::Utf16::kUtf8BytesToCodeASurrogate; |
| 409 ASSERT(buffer_position >= kEncodedSurrogateLength); |
| 410 conn->Send(buffer, buffer_position - kEncodedSurrogateLength); |
| 411 for (int i = 0; i < kEncodedSurrogateLength; i++) { |
| 412 buffer[i] = buffer[buffer_position + i]; |
| 413 } |
| 414 buffer_position = kEncodedSurrogateLength; |
| 415 } else { |
| 416 conn->Send(buffer, buffer_position); |
| 417 buffer_position = 0; |
| 418 } |
| 401 } | 419 } |
| 420 previous = character; |
| 402 } | 421 } |
| 403 | 422 |
| 404 return true; | 423 return true; |
| 405 } | 424 } |
| 406 | 425 |
| 407 | 426 |
| 408 bool DebuggerAgentUtil::SendMessage(const Socket* conn, | 427 bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 409 const v8::Handle<v8::String> request) { | 428 const v8::Handle<v8::String> request) { |
| 410 static const int kBufferSize = 80; | 429 static const int kBufferSize = 80; |
| 411 char buffer[kBufferSize]; // Sending buffer both for header and body. | 430 char buffer[kBufferSize]; // Sending buffer both for header and body. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 439 return total_received; | 458 return total_received; |
| 440 } | 459 } |
| 441 total_received += received; | 460 total_received += received; |
| 442 } | 461 } |
| 443 return total_received; | 462 return total_received; |
| 444 } | 463 } |
| 445 | 464 |
| 446 } } // namespace v8::internal | 465 } } // namespace v8::internal |
| 447 | 466 |
| 448 #endif // ENABLE_DEBUGGER_SUPPORT | 467 #endif // ENABLE_DEBUGGER_SUPPORT |
| OLD | NEW |