<commit_before>//
//  ActiveSocket.swift
//  TestSwiftyCocoa
//
//  Created by <NAME> on 6/11/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Darwin
import Dispatch

/**
* Represents an active STREAM socket based on the standard Unix sockets
* library.
*
* An active socket can be either a socket gained by calling accept on a
* passive socket or by explicitly connecting one to an address (a client
* socket).
* Therefore an active socket has two addresses, the local and the remote one.
*
* There are three methods to perform a close, this is rooted in the fact that
* a socket actually is full-duplex, it provides a send and a receive channel.
* The stream-mode is updated according to what channels are open/closed.
* Initially the socket is full-duplex and you cannot reopen a channel that was
* shutdown. If you have shutdown both channels the socket can be considered
* closed.
*/
class ActiveSocket: Socket, OutputStream {
  
  var remoteAddress:  sockaddr_in?       = nil
  var queue:          dispatch_queue_t?  = nil
  var readSource:     dispatch_source_t? = nil
  var sendCount:      Int                = 0
  var closeRequested: Bool               = false
  
  var isConnected: Bool {
    return isValid ? (remoteAddress != nil) : false
  }
  
  var onRead: ((ActiveSocket) -> Void)? = nil {
    didSet {
      if onRead {
        if readSource == nil {
          startEventHandler()
        }
      }
      else if (readSource) {
        stopEventHandler()
      }
    }
  }
  
  // let the socket own the read buffer, what is the best buffer type?
  let readBufferSize : Int = 4096 // available space, a bit more for '\0'
  var readBuffer     : CChar[] =  CChar[](count: 4096 + 2, repeatedValue: 42)

  
  /* init */
  
  convenience init(fd: CInt?, remoteAddress: sockaddr_in?,
                   queue: dispatch_queue_t? = nil)
  {
    self.init(fd: fd)
    
    self.remoteAddress  = remoteAddress
    self.queue          = queue<commit_msg>Setting the readBufferSize will adjust the buffer accordingly.<commit_after>//
//  ActiveSocket.swift
//  TestSwiftyCocoa
//
//  Created by <NAME> on 6/11/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Darwin
import Dispatch

/**
* Represents an active STREAM socket based on the standard Unix sockets
* library.
*
* An active socket can be either a socket gained by calling accept on a
* passive socket or by explicitly connecting one to an address (a client
* socket).
* Therefore an active socket has two addresses, the local and the remote one.
*
* There are three methods to perform a close, this is rooted in the fact that
* a socket actually is full-duplex, it provides a send and a receive channel.
* The stream-mode is updated according to what channels are open/closed.
* Initially the socket is full-duplex and you cannot reopen a channel that was
* shutdown. If you have shutdown both channels the socket can be considered
* closed.
*/
class ActiveSocket: Socket, OutputStream {
  
  var remoteAddress:  sockaddr_in?       = nil
  var queue:          dispatch_queue_t?  = nil
  var readSource:     dispatch_source_t? = nil
  var sendCount:      Int                = 0
  var closeRequested: Bool               = false
  
  var isConnected: Bool {
    return isValid ? (remoteAddress != nil) : false
  }
  
  var onRead: ((ActiveSocket) -> Void)? = nil {
    didSet {
      if onRead {
        if readSource == nil {
          startEventHandler()
        }
      }
      else if (readSource) {
        stopEventHandler()
      }
    }
  }
  
  // let the socket own the read buffer, what is the best buffer type?
  var readBuffer     : CChar[] =  CChar[](count: 4096 + 2, repeatedValue: 42)
  var readBufferSize : Int = 4096 { // available space, a bit more for '\0'
    didSet {
      if readBufferSize != oldValue {
        readBuffer = CChar[](count: readBufferSize + 2, repeatedValue: 42)
      }
    }
  }

  
  /* init */
  
  convenience init(fd: CInt?, remoteAddress: sockaddr_in?,
                   queue: dispatch_queue_t? = nil)
  {
    self.init(fd: fd)
    
    self.remoteAddress  = remoteAddress
    self.queue          = queue
{'fraction_non_alphanumeric': 0.08335340881715249, 'fraction_numerical': 0.011322572874006263, 'mean_word_length': 2.5067567567567566, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4200872', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1188, 'n_words': 478}