<commit_before>// This file is part of the "x0" project, http://github.com/christianparpart/x0>
//   (c) 2009-2017 <NAME> <<EMAIL>>
//
// Licensed under the MIT License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of
// the License at: http://opensource.org/licenses/MIT

#include <xzero/net/InetEndPoint.h>
#include <xzero/net/InetConnector.h>
#include <xzero/net/InetUtil.h>
#include <xzero/net/Connection.h>
#include <xzero/net/Connector.h>
#include <xzero/util/BinaryReader.h>
#include <xzero/io/FileUtil.h>
#include <xzero/executor/Executor.h>
#include <xzero/logging.h>
#include <xzero/RuntimeError.h>
#include <xzero/Buffer.h>
#include <xzero/sysconfig.h>
#include <xzero/RefPtr.h>
#include <stdexcept>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

namespace xzero {

#define ERROR(msg...) logError("net.InetEndPoint", msg)

#if !defined(NDEBUG)
#define TRACE(msg...) logTrace("net.InetEndPoint", msg)
#define DEBUG(msg...) logDebug("net.InetEndPoint", msg)
#else
#define TRACE(msg...) do {} while (0)
#define DEBUG(msg...) do {} while (0)
#endif

InetEndPoint::InetEndPoint(int socket,
                           InetConnector* connector,
                           Executor* executor)
    : EndPoint(),
      connector_(connector),
      executor_(executor),
      readTimeout_(connector->readTimeout()),
      writeTimeout_(connector->writeTimeout()),
      io_(),
      inputBuffer_(),
      inputOffset_(0),
      handle_(socket),
      addressFamily_(connector->addressFamily()),
      isCorking_(false) {
  TRACE("$0 ctor fd=$1", this, handle_);
}

InetEndPoint::InetEndPoint(int socket,
                           int addressFamily,
                           Duration readTimeout,
                           Duration writeTimeout,
                           Executor* executor)
    : EndPoint(),
      connector_(nullptr),
      executor_(executor),
      readTimeout_(readTimeout),
      writeTimeout_(writeTimeout),
      io_(),
      inputBuffer_(),
      inputOffset_(0),
      handle_(socket),
      addressFamily_(addressFamily),
      isCorking_(false) {
  TRACE("$0 ctor", this);
}

void InetEndPoint::onTimeout() {
  if (connection()) {
    if (connection()->onReadTimeout()) {
      close();
    }
  }
}

InetEndPoint::~InetEndPoint() {
  TRACE("$0 dtor", this);
  if (isOpen()) {
    close();
  }
}

Option<InetAddress> InetEndPoint::remoteAddress() const {
  return InetUtil::getRemoteAddress(handle_, addressFamily());
}

Option<InetAddress> InetEndPoint::localAddress() const {
  return InetUtil::getLocalAddress(handle_, addressFamily());
}

bool InetEndPoint::isOpen() const XZERO_NOEXCEPT {
  return handle_ >= 0;
}

void InetEndPoint::close() {
  if (isOpen()) {
    TRACE("close() fd=$0", handle_);
    FileUtil::close(handle_);
    handle_ = -1;

    if (connector_) {
      connector_->onEndPointClosed(this);
    }
  }
}

bool InetEndPoint::isBlocking() const {
  return !(fcntl(handle_, F_GETFL) & O_NONBLOCK);
}

void InetEndPoint::setBlocking(bool enable) {
  FileUtil::setBlocking(handle_, enable);
}

bool InetEndPoint::isCorking() const {
  return isCorking_;
}

void InetEndPoint::setCorking(bool enable) {
  InetUtil::setCorking(handle_, enable);
  isCorking_ = enable;
}

bool InetEndPoint::isTcpNoDelay() const {
  return InetUtil::isTcpNoDelay(handle_);
}

void InetEndPoint::setTcpNoDelay(bool enable) {
  InetUtil::setTcpNoDelay(handle_, enable);
}

std::string InetEndPoint::toString() const {<commit_msg>[xzero] InetEndPoint: minor resurrections of prior impl before refactor<commit_after>// This file is part of the "x0" project, http://github.com/christianparpart/x0>
//   (c) 2009-2017 <NAME> <<EMAIL>>
//
// Licensed under the MIT License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of
// the License at: http://opensource.org/licenses/MIT

#include <xzero/net/InetEndPoint.h>
#include <xzero/net/InetConnector.h>
#include <xzero/net/InetUtil.h>
#include <xzero/net/Connection.h>
#include <xzero/net/Connector.h>
#include <xzero/util/BinaryReader.h>
#include <xzero/io/FileUtil.h>
#include <xzero/executor/Executor.h>
#include <xzero/logging.h>
#include <xzero/RuntimeError.h>
#include <xzero/Buffer.h>
#include <xzero/sysconfig.h>
#include <xzero/RefPtr.h>
#include <stdexcept>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

namespace xzero {

#define ERROR(msg...) logError("net.InetEndPoint", msg)

#if !defined(NDEBUG)
#define TRACE(msg...) logTrace("net.InetEndPoint", msg)
#define DEBUG(msg...) logDebug("net.InetEndPoint", msg)
#else
#define TRACE(msg...) do {} while (0)
#define DEBUG(msg...) do {} while (0)
#endif

InetEndPoint::InetEndPoint(int socket,
                           InetConnector* connector,
                           Executor* executor)
    : EndPoint(),
      connector_(connector),
      executor_(executor),
      readTimeout_(connector->readTimeout()),
      writeTimeout_(connector->writeTimeout()),
      io_(),
      inputBuffer_(),
      inputOffset_(0),
      handle_(socket),
      addressFamily_(connector->addressFamily()),
      isCorking_(false) {
  TRACE("$0 ctor fd=$1", this, handle_);
}

InetEndPoint::InetEndPoint(int socket,
                           int addressFamily,
                           Duration readTimeout,
                           Duration writeTimeout,
                           Executor* executor)
    : EndPoint(),
      connector_(nullptr),
      executor_(executor),
      readTimeout_(readTimeout),
      writeTimeout_(writeTimeout),
      io_(),
      inputBuffer_(),
      inputOffset_(0),
      handle_(socket),
      addressFamily_(addressFamily),
      isCorking_(false) {
  TRACE("$0 ctor", this);
}

void InetEndPoint::onTimeout() {
  if (connection()) {
    if (connection()->onReadTimeout()) {
      close();
    }
  }
}

InetEndPoint::~InetEndPoint() {
  TRACE("$0 dtor", this);
  if (isOpen()) {
    close();
  }
}

Option<InetAddress> InetEndPoint::remoteAddress() const {
  return InetUtil::getRemoteAddress(handle_, addressFamily());
}

Option<InetAddress> InetEndPoint::localAddress() const {
  return InetUtil::getLocalAddress(handle_, addressFamily());
}

bool InetEndPoint::isOpen() const XZERO_NOEXCEPT {
  return handle_ >= 0;
}

void InetEndPoint::close() {
  if (isOpen()) {
    TRACE("close() fd=$0", handle_);
    FileUtil::close(handle_);
    handle_ = -1;

    if (connector_) {
      connector_->onEndPointClosed(this);
    }
  }
}

bool InetEndPoint::isBlocking() const {
  return !(fcntl(handle_, F_GETFL) & O_NONBLOCK);
}

void InetEndPoint::setBlocking(bool enable) {
  FileUtil::setBlocking(handle_, enable);
}

bool InetEndPoint::isCorking() const {
  return isCorking_;
}

void InetEndPoint::setCorking(bool enable) {
  if (isCorking_ != enable) {
    InetUtil::setCorking(handle_, enable);
    isCorking_ = enable;
  }
}

bool InetEndPoint::isTcpNoDelay() const {
  return InetUtil::isTcpNoDelay(handle_);
}

void InetEndPoint::setTcpNoDelay(bool enable) {
  InetUtil::setTcpNoDelay(handle_, enable);
}

std::string InetEndPoint::toString() const {
{'fraction_non_alphanumeric': 0.14660194174757282, 'fraction_numerical': 0.005825242718446602, 'mean_word_length': 3.6194746957078796, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4797806', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2398, 'n_words': 483}