<commit_before>local socket = require'socket'
assert(socket._VERSION:match('^LuaSocket 3%.'))
local emitter = require'emitter'
local ev = require'ev'

local isip = function(ip)
  local addrinfo,err = socket.dns.getaddrinfo(ip)
  if err then
    return false
  end
  return true
end

local isipv6 = function(ip)
  local addrinfo,err = socket.dns.getaddrinfo(ip)
  if addrinfo then
    assert(#addrinfo > 0)
    if addrinfo[1].family == 'inet6' then
      return true
    end
  end
  return false
end

local isipv4 = function(ip)
  return isip(ip) and not isipv6(ip)
end

local new = function()
  local self = emitter.new()
  local sock
  local loop = ev.Loop.default
  local connecting = false
  local closing = false
  local watchers = {}
  self.connect = function(_,port,ip)
    ip = ip or 'localhost'
    if not isip(ip) then
      error('invalid ip')
    end
    if sock and closing then
      self:once('close',function(self)
          self:_connect(port,ip)
        end)
      
    elseif not connecting then
      self:_connect(port,ip)
    end
  end
  
  self._connect = function(_,port,ip)
    assert(not sock)
    if isipv6(ip) then
      sock = socket.tcp6()
    else
      sock = socket.tcp()
    end
    sock:settimeout(0)
    connecting = true
    closing = false
    local ok,err = sock:connect(ip,port)
    if ok or err == 'already connected' then
      self:emit('connect',self)
    elseif err == 'timeout' or err == 'Operation already in progress' then
      watchers.connect = ev.IO.new(function(loop,io)
          io:stop(loop)
          self:emit('connect',self)
        end,sock:getfd(),ev.WRITE)
      watchers.connect:start(loop)
    else
      self:emit('error',err)
    end
  end
  self.write = function() end
  self.fin = function() end
  self.destroy = function()
    for _,watcher in pairs(watchers) do
      watcher:stop(loop)
    end
    if sock then
      sock:close()
      sock = nil
    end
  end
  self.pause = function() end
  self.resume = function() end
  self.set_timeout = function() end
  self.set_keepalive = function() end
  self.set_nodelay = function() end
  return self
end
<commit_msg>add more impl<commit_after>local socket = require'socket'
assert(socket._VERSION:match('^LuaSocket 3%.'))
local emitter = require'emitter'
local ev = require'ev'

local isip = function(ip)
  local addrinfo,err = socket.dns.getaddrinfo(ip)
  if err then
    return false
  end
  return true
end

local isipv6 = function(ip)
  local addrinfo,err = socket.dns.getaddrinfo(ip)
  if addrinfo then
    assert(#addrinfo > 0)
    if addrinfo[1].family == 'inet6' then
      return true
    end
  end
  return false
end

local isipv4 = function(ip)
  return isip(ip) and not isipv6(ip)
end

local new = function()
  local self = emitter.new()
  local sock
  local loop = ev.Loop.default
  local connecting = false
  local closing = false
  local watchers = {}
  
  local read = function()
    assert(sock)
    watchers.read = ev.IO.new(function()
        local data,err,part = sock:receive(8192)
        if err then
          self:emit('error',err)
          self:emit('close')
          self:destroy()
        else
          self:emit('data',data or part)
        end
      end,sock:getfd(),ev.READ)
    self:resume()
  end
  
  local pending
  local pos
  
  local write = function()
    assert(sock)
    watchers.write = ev.IO.new(function(loop,io)
        local sent,err,so_far = sock:send(pending,pos)
        if not sent and err ~= 'timeout' then
          if err ~= 'closed' then
            self:emit('error',err)
          end
          self:emit('close')
          self:destroy()
        elseif sent then
          pos = nil
          pending = nil
          io:stop(loop)
          self:emit('drain')
        else
          pos = so_far + 1
        end
      end,sock:getfd(),ev.WRITE)
  end
  
  self.connect = function(_,port,ip)
    ip = ip or 'localhost'
    if not isip(ip) then
      self:emit('error',err)
      self:emit('close')
    end
    if sock and closing then
      self:once('close',function(self)
          self:_connect(port,ip)
        end)
      
    elseif not connecting then
      self:_connect(port,ip)
    end
  end
  
  self._connect = function(_,port,ip)
    assert(not sock)
    if isipv6(ip) then
      sock = socket.tcp6()
    else
      sock = socket.tcp()
    end
    sock:settimeout(0)
    connecting = true
    closing = false
    local ok,err = sock:connect(ip,port)
    if ok or err == 'already connected' then
      read()
      write()
      self:emit('connect',self)
    elseif err == 'timeout' or err == 'Operation already in progress' then
      watchers.connect = ev.IO.new(function(loop,io)
          io:stop(loop)
          watchers.connect = nil
          read()
          write()
          self:emit('connect',self)
        end,sock:getfd(),ev.WRITE)
      watchers.connect:start(loop)
    else
      self:emit('error',err)
      self:emit('close')
      self:destroy()
    end
  end
  
  self.write = function(_,data)
    if pending then
      pending = pending..data
    else
      pending = data
      watchers.write:start(loop)
    end
  end
  
  self.fin = function()
    if pending then
      self:once('drain',function()
          sock:shutdown('send')
        end)
    else
      sock:shutdown('send')
    end
  end
  
  self.destroy = function()
    for _,watcher in pairs(watchers) do
      watcher:stop(loop)
    end
    if sock then
      sock:close()
      sock = nil
    end
  end
  
  self.pause = function()
    watchers.read:stop(loop)
  end
  
  self.resume = function()
    watchers.read:start(loop)
  end
  
  self.set_timeout = function() end
  self.set_keepalive = function() end
  self.set_nodelay = function() end
  return self
end

{'fraction_non_alphanumeric': 0.10896094839609484, 'fraction_numerical': 0.004358437935843794, 'mean_word_length': 2.410820451843044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2094713', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1869, 'n_words': 486}