<commit_before>/* LibMemcached
 * Copyright (C) 2006-2010 <NAME>
 * All rights reserved.
 *
 * Use and distribution licensed under the BSD license.  See
 * the COPYING file in the parent directory for full text.
 *
 * Summary:
 *
 */

#include <libmemcached/common.h>

memcached_return_t memcached_vdo(org::libmemcached::Instance* instance,
                                 libmemcached_io_vector_st vector[],
                                 const size_t count,
                                 const bool with_flush)
{
  memcached_return_t rc;

  assert_msg(vector, "Invalid vector passed");

  if (memcached_failed(rc= memcached_connect(instance)))
  {
    WATCHPOINT_ERROR(rc);
    assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none.");
    return rc;
  }

  /*
  ** Since non buffering ops in UDP mode dont check to make sure they will fit
  ** before they start writing, if there is any data in buffer, clear it out,
  ** otherwise we might get a partial write.
  **/
  if (memcached_is_udp(instance->root))
  {
    if (vector[0].buffer or vector[0].length)
    {
      return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, 
                                 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
    }

    struct msghdr msg;
    memset(&msg, 0, sizeof(msg));

    increment_udp_message_id(instance);
    vector[0].buffer= instance->write_buffer;
    vector[0].length= UDP_DATAGRAM_HEADER_LENGTH;

    msg.msg_iov= (struct iovec*)vector;
    msg.msg_iovlen= count;

    uint32_t retry= 5;
    while (--retry)
    {
      ssize_t sendmsg_length= ::sendmsg(instance->fd, &msg, 0);
      if (sendmsg_length > 0)
      {
        break;
      }
      else if (sendmsg_length < 0)<commit_msg>Check if a connection has been closed before we send on it<commit_after>/* LibMemcached
 * Copyright (C) 2006-2010 <NAME>
 * All rights reserved.
 *
 * Use and distribution licensed under the BSD license.  See
 * the COPYING file in the parent directory for full text.
 *
 * Summary:
 *
 */

#include <libmemcached/common.h>

memcached_return_t memcached_vdo(org::libmemcached::Instance* instance,
                                 libmemcached_io_vector_st vector[],
                                 const size_t count,
                                 const bool with_flush)
{
  memcached_return_t rc;

  assert_msg(vector, "Invalid vector passed");

  // Check if the socket has been shutdown or has failed under our feet.
  if (instance->fd != INVALID_SOCKET)
  {
    // Do a non-blocking read to determine the state of the socket.  This can
    // give four possible results:
    // 1.  There is data waiting to be read. The socket is healthy.
    // 2.  recv() fails with a "would block" error. The socket is healthy but
    //     there is no data right now.
    // 3.  recv() fails with another error. The socket is not healthy.
    // 4.  recv() returns no data.  This means the socket has been closed by the
    //     remote host.
    //
    // Reset the socket if we get 3) or 4).  We use the MSG_PEEK flag so that
    // the read doesn't remove any data from the socket.
    uint8_t buf[1];
    ssize_t rc = recv(instance->fd, buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT);

    if ((rc < 0) && (errno != EAGAIN) & (errno != EWOULDBLOCK))
    {
      memcached_io_reset(instance);
    }
    else if (rc == 0)
    {
      memcached_io_reset(instance);
    }
  }

  if (memcached_failed(rc= memcached_connect(instance)))
  {
    WATCHPOINT_ERROR(rc);
    assert_msg(instance->error_messages, "memcached_connect() returned an error but the Instance showed none.");
    return rc;
  }

  /*
  ** Since non buffering ops in UDP mode dont check to make sure they will fit
  ** before they start writing, if there is any data in buffer, clear it out,
  ** otherwise we might get a partial write.
  **/
  if (memcached_is_udp(instance->root))
  {
    if (vector[0].buffer or vector[0].length)
    {
      return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
                                 memcached_literal_param("UDP messages was attempted, but vector was not setup for it"));
    }

    struct msghdr msg;
    memset(&msg, 0, sizeof(msg));

    increment_udp_message_id(instance);
    vector[0].buffer= instance->write_buffer;
    vector[0].length= UDP_DATAGRAM_HEADER_LENGTH;

    msg.msg_iov= (struct iovec*)vector;
    msg.msg_iovlen= count;

    uint32_t retry= 5;
    while (--retry)
    {
      ssize_t sendmsg_length= ::sendmsg(instance->fd, &msg, 0);
      if (sendmsg_length > 0)
      {
        break;
      }
      else if (sendmsg_length < 0)
{'fraction_non_alphanumeric': 0.09840933191940615, 'fraction_numerical': 0.010180275715800637, 'mean_word_length': 2.6055045871559632, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7275891', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1445, 'n_words': 458}