<commit_before>/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 <NAME>                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include <deque>
#include <vector>

#include "caf/fwd.hpp"
#include "caf/logger.hpp"
#include "caf/net/defaults.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/fwd.hpp"
#include "caf/net/receive_policy.hpp"
#include "caf/net/stream_socket.hpp"
#include "caf/net/transport_base.hpp"
#include "caf/net/transport_worker.hpp"
#include "caf/sec.hpp"
#include "caf/span.hpp"

namespace caf::net {

template <class Application>
using stream_transport_base = transport_base<
  stream_transport<Application>, transport_worker<Application>, stream_socket,
  Application, unit_t>;

/// Implements a stream_transport that manages a stream socket.
template <class Application>
class stream_transport : public stream_transport_base<Application> {
public:
  // -- member types -----------------------------------------------------------

  using application_type = Application;

  using worker_type = transport_worker<application_type>;

  using super = stream_transport_base<application_type>;

  using id_type = typename super::id_type;

  using buffer_type = typename super::buffer_type;

  using write_queue_type = std::deque<std::pair<bool, buffer_type>>;

  // -- constructors, destructors, and assignment operators --------------------

  stream_transport(stream_socket handle, application_type application)
    : super(handle, std::move(application)),
      written_(0),
      read_threshold_(1024),
      collected_(0),
      max_(1024),
      rd_flag_(net::receive_policy_flag::exactly) {
    // nop
  }

  // -- member functions -------------------------------------------------------

  bool handle_read_event(endpoint_manager&) override {
    auto buf = this->read_buf_.data() + this->collected_;
    size_t len = this->read_threshold_ - this->collected_;
    CAF_LOG_TRACE(CAF_ARG2("handle", this->handle().id)
                  << CAF_ARG2("missing", len));
    auto ret = read(this->handle_, make_span(buf, len));
    // Update state.
    if (auto num_bytes = get_if<size_t>(&ret)) {
      CAF_LOG_DEBUG(CAF_ARG(len)<commit_msg>Add nodelay opt<commit_after>/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 <NAME>                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include <deque>
#include <vector>

#include "caf/fwd.hpp"
#include "caf/logger.hpp"
#include "caf/net/defaults.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/fwd.hpp"
#include "caf/net/receive_policy.hpp"
#include "caf/net/stream_socket.hpp"
#include "caf/net/transport_base.hpp"
#include "caf/net/transport_worker.hpp"
#include "caf/sec.hpp"
#include "caf/span.hpp"

namespace caf::net {

template <class Application>
using stream_transport_base = transport_base<
  stream_transport<Application>, transport_worker<Application>, stream_socket,
  Application, unit_t>;

/// Implements a stream_transport that manages a stream socket.
template <class Application>
class stream_transport : public stream_transport_base<Application> {
public:
  // -- member types -----------------------------------------------------------

  using application_type = Application;

  using worker_type = transport_worker<application_type>;

  using super = stream_transport_base<application_type>;

  using id_type = typename super::id_type;

  using buffer_type = typename super::buffer_type;

  using write_queue_type = std::deque<std::pair<bool, buffer_type>>;

  // -- constructors, destructors, and assignment operators --------------------

  stream_transport(stream_socket handle, application_type application)
    : super(handle, std::move(application)),
      written_(0),
      read_threshold_(1024),
      collected_(0),
      max_(1024),
      rd_flag_(net::receive_policy_flag::exactly) {
    CAF_ASSERT(handle != invalid_socket);
    nodelay(handle, true);
  }

  // -- member functions -------------------------------------------------------

  bool handle_read_event(endpoint_manager&) override {
    auto buf = this->read_buf_.data() + this->collected_;
    size_t len = this->read_threshold_ - this->collected_;
    CAF_LOG_TRACE(CAF_ARG2("handle", this->handle().id)
                  << CAF_ARG2("missing", len));
    auto ret = read(this->handle_, make_span(buf, len));
    // Update state.
    if (auto num_bytes = get_if<size_t>(&ret)) {
      CAF_LOG_DEBUG(CAF_ARG(len)
{'fraction_non_alphanumeric': 0.17969881262670143, 'fraction_numerical': 0.007529684332464524, 'mean_word_length': 1.7107535321821037, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4878151', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1709, 'n_words': 364}