ZeroCopy.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/io/async/test/ZeroCopy.h>
  17. namespace folly {
  18. // ZeroCopyTest
  19. ZeroCopyTest::ZeroCopyTest(
  20. size_t numClients,
  21. int numLoops,
  22. bool zeroCopy,
  23. size_t bufferSize)
  24. : numClients_(numClients),
  25. counter_(numClients),
  26. numLoops_(numLoops),
  27. zeroCopy_(zeroCopy),
  28. bufferSize_(bufferSize),
  29. listenSock_(new folly::AsyncServerSocket(&evb_)),
  30. server_(&evb_, numLoops_, bufferSize_, zeroCopy) {
  31. clients_.reserve(numClients_);
  32. for (size_t i = 0; i < numClients_; i++) {
  33. clients_.emplace_back(std::make_unique<ZeroCopyTestAsyncSocket>(
  34. &counter_, &evb_, numLoops_, bufferSize_, zeroCopy));
  35. }
  36. if (listenSock_) {
  37. server_.addCallbackToServerSocket(*listenSock_);
  38. }
  39. }
  40. bool ZeroCopyTest::run() {
  41. evb_.runInEventBaseThread([this]() {
  42. if (listenSock_) {
  43. listenSock_->bind(0);
  44. listenSock_->setZeroCopy(zeroCopy_);
  45. listenSock_->listen(10);
  46. listenSock_->startAccepting();
  47. connectAll();
  48. }
  49. });
  50. evb_.loopForever();
  51. for (auto& client : clients_) {
  52. if (client->isZeroCopyWriteInProgress()) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. } // namespace folly