<commit_before>//=======================================================================
// Copyright <NAME> 2013-2016.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://www.opensource.org/licenses/MIT)
//=======================================================================

#ifndef NET_CONNECTION_HANDLER_H
#define NET_CONNECTION_HANDLER_H

#include <list.hpp>

#include "conc/rw_lock.hpp"

namespace network {

template <typename C>
struct connection_handler {
    using connection_type = C;

    connection_type* get_connection_for_packet(size_t source_port, size_t target_port) {
        auto lock = connections_lock.reader_lock();
        std::lock_guard<reader_rw_lock> l(lock);

        for (auto& connection : connections) {
            if (connection.server_port == source_port && connection.local_port == target_port) {
                return &connection;
            }
        }

        return nullptr;
    }

    connection_type& create_connection() {
        auto lock = connections_lock.writer_lock();
        std::lock_guard<writer_rw_lock> l(lock);

        return connections.emplace_back();<commit_msg>Server support for connections<commit_after>//=======================================================================
// Copyright <NAME> 2013-2016.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://www.opensource.org/licenses/MIT)
//=======================================================================

#ifndef NET_CONNECTION_HANDLER_H
#define NET_CONNECTION_HANDLER_H

#include <list.hpp>

#include "conc/rw_lock.hpp"

namespace network {

template <typename C>
struct connection_handler {
    using connection_type = C;

    connection_type* get_connection_for_packet(size_t source_port, size_t target_port) {
        auto lock = connections_lock.reader_lock();
        std::lock_guard<reader_rw_lock> l(lock);

        for (auto& connection : connections) {
            if(connection.server){
                if (connection.server_port == target_port && connection.local_port == source_port) {
                    return &connection;
                }
            } else {
                if (connection.server_port == source_port && connection.local_port == target_port) {
                    return &connection;
                }
            }
        }

        return nullptr;
    }

    connection_type& create_connection() {
        auto lock = connections_lock.writer_lock();
        std::lock_guard<writer_rw_lock> l(lock);

        return connections.emplace_back();
{'fraction_non_alphanumeric': 0.1965648854961832, 'fraction_numerical': 0.0061068702290076335, 'mean_word_length': 3.2003205128205128, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '811433', 'n_tokens_mistral': 715, 'n_tokens_neox': 665, 'n_words': 151}