/*******************************************************************************
 * Copyright (c) 2022, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <algorithm>
#include <iterator>
#include <cstring>

#include "socket.h"
#include "connection.h"
#include "eventapi.h"
#include "exception.h"

#ifdef DEBUG
#include <iostream>
#endif

/** \brief a method to add Data to Sendqueue
  * \param data an const char* to add to sendqueue
  * \param datasize an size_t to set datasize
  * \return the last ConnectionData Block from Sendqueue
  * 
  * This method does unbelievably useful things.  
  * And returns exceptionally the new connection data block.
  * Use it everyday with good health.
  */

netplus::condata<char>::condata() : vector(){
}

netplus::condata<char>::condata(const condata<char> &src) : vector(src) {
  pos=src.pos;;
}

netplus::condata<char>::condata(const std::vector<char> &src){
  insert(end(), src.begin(), src.end());
}

netplus::condata<char>::iterator netplus::condata<char>::begin(){
    return std::vector<char,condataAlloc<char>>::begin();
}

netplus::condata<char>::const_iterator netplus::condata<char>::begin() const{
    return std::vector<char,condataAlloc<char>>::begin();
}

netplus::condata<char>::iterator netplus::condata<char>::end(){
    return std::vector<char,condataAlloc<char>>::end();
}

netplus::condata<char>::const_iterator netplus::condata<char>::end() const{
    return std::vector<char,condataAlloc<char>>::end();
}

void netplus::condata<char>::append(const char* data, size_t datalen) {
    if (!data || datalen == 0)
        return;
    this->insert(this->end(), data, data + datalen);
}

size_t netplus::condata<char>::search(const char* word,size_t pos){
    size_t wsize=strlen(word);
    if(wsize == 0 || pos + wsize > size())
        return std::string::npos;
    auto it = std::search(begin() + pos, end(), word, word + wsize);
    if(it == end())
        return std::string::npos;
    return static_cast<size_t>(it - begin());
}


size_t netplus::condata<char>::search(const char* word){
    return search(word,0);
}

netplus::con::con()
    : slots(1)
{
    lasteventime = time(nullptr);
}

netplus::con::con(eventapi *event)
    : slots(1)
{
    lasteventime = time(nullptr);
}

netplus::con::~con(){
}

