Chromium Code Reviews| 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::Utf8::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::Utf8::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 < 3 || i == message.length() - 1) { |
| 399 conn->Send(buffer, buffer_position); | 404 if (unibrow::Utf16::IsLeadSurrogate(character)) { |
| 400 buffer_position = 0; | 405 int kEncodedSurrogateLength = 3; |
|
rossberg
2012/03/07 13:32:47
Maybe move this declaration up and use it instead
Erik Corry
2012/03/11 19:29:22
The 3 above is actually a different 3. I gave the
| |
| 406 ASSERT(buffer_position >= kEncodedSurrogateLength); | |
| 407 conn->Send(buffer, buffer_position - kEncodedSurrogateLength); | |
| 408 for (int i = 0; i < kEncodedSurrogateLength; i++) { | |
| 409 buffer[i] = buffer[buffer_position + i]; | |
| 410 } | |
| 411 buffer_position = kEncodedSurrogateLength; | |
| 412 } else { | |
| 413 conn->Send(buffer, buffer_position); | |
| 414 buffer_position = 0; | |
| 415 } | |
| 401 } | 416 } |
| 417 previous = character; | |
| 402 } | 418 } |
| 403 | 419 |
| 404 return true; | 420 return true; |
| 405 } | 421 } |
| 406 | 422 |
| 407 | 423 |
| 408 bool DebuggerAgentUtil::SendMessage(const Socket* conn, | 424 bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 409 const v8::Handle<v8::String> request) { | 425 const v8::Handle<v8::String> request) { |
| 410 static const int kBufferSize = 80; | 426 static const int kBufferSize = 80; |
| 411 char buffer[kBufferSize]; // Sending buffer both for header and body. | 427 char buffer[kBufferSize]; // Sending buffer both for header and body. |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 439 return total_received; | 455 return total_received; |
| 440 } | 456 } |
| 441 total_received += received; | 457 total_received += received; |
| 442 } | 458 } |
| 443 return total_received; | 459 return total_received; |
| 444 } | 460 } |
| 445 | 461 |
| 446 } } // namespace v8::internal | 462 } } // namespace v8::internal |
| 447 | 463 |
| 448 #endif // ENABLE_DEBUGGER_SUPPORT | 464 #endif // ENABLE_DEBUGGER_SUPPORT |
| OLD | NEW |