<commit_before>/*
 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;

/**
 * @class FsoFramework.BaseTransport
 *
 * Base class for FsoFramework Transport Classes
 **/
public class FsoFramework.BaseTransport : FsoFramework.Transport
{
    protected string name;
    protected uint speed;
    protected bool raw;
    protected bool hard;
    protected int fd = -1;

    private bool buffered;

    private IOChannel channel;

    private uint readwatch;
    private int readpriority;
    private uint writewatch;
    private int writepriority;

    protected TransportFunc hupfunc;
    protected TransportFunc readfunc;

    protected ByteArray buffer;

    protected void restartWriter()
    {
        writewatch = channel.add_watch_full( writepriority, IOCondition.OUT, writeCallback );
    }

    private bool actionCallback( IOChannel? source, IOCondition condition )
    {
        if ( !isOpen() )
        {
            logger.warning( "Ignoring actionCallback while transport is closed" );
            return false;
        }

        if ( ( condition & IOCondition.HUP ) == IOCondition.HUP )
        {
            logger.error( "HUP => closing channel" );
            close();
            if ( hupfunc != null )
                hupfunc( this );
            return false;
        }

        if ( ( condition & IOCondition.IN ) == IOCondition.IN )
        {
            if ( readfunc != null )
                readfunc( this );
            return true;
        }

        logger.critical( @"ActionCallback called with unknown condition %d".printf( condition ) );

        return false;
    }

    private bool writeCallback( IOChannel source, IOCondition condition )
    {
        assert( logger.debug( "WriteCallback called with %u bytes in buffer".printf( buffer.len ) ) );
        /*
        for( int i = 0; i < buffer.len; ++i )
        logger.debug( "byte: 0x%02x".printf( buffer.data[i] ) );
        */
        int len = 64 > buffer.len? (int)buffer.len : 64;
        var byteswritten = _write( buffer.data, len );
        assert( logger.debug( "WriteCallback wrote %d bytes".printf( (int)byteswritten ) ) );
        buffer.remove_range( 0, (int)byteswritten );

        return ( buffer.len != 0 );
    }

    internal int _write( void* data, int len )
    {
        assert( fd != -1 );
        ssize_t byteswritten = Posix.write( fd, data, len );
        return (int)byteswritten;
    }

    protected virtual void configure()
    {<commit_msg>libfsotransport: don't bail out if someone attempts to write to an invalid transport; warn and ignore instead<commit_after>/*
 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;

/**
 * @class FsoFramework.BaseTransport
 *
 * Base class for FsoFramework Transport Classes
 **/
public class FsoFramework.BaseTransport : FsoFramework.Transport
{
    protected string name;
    protected uint speed;
    protected bool raw;
    protected bool hard;
    protected int fd = -1;

    private bool buffered;

    private IOChannel channel;

    private uint readwatch;
    private int readpriority;
    private uint writewatch;
    private int writepriority;

    protected TransportFunc hupfunc;
    protected TransportFunc readfunc;

    protected ByteArray buffer;

    protected void restartWriter()
    {
        writewatch = channel.add_watch_full( writepriority, IOCondition.OUT, writeCallback );
    }

    private bool actionCallback( IOChannel? source, IOCondition condition )
    {
        if ( !isOpen() )
        {
            logger.warning( "Ignoring actionCallback while transport is closed" );
            return false;
        }

        if ( ( condition & IOCondition.HUP ) == IOCondition.HUP )
        {
            logger.error( "HUP => closing channel" );
            close();
            if ( hupfunc != null )
                hupfunc( this );
            return false;
        }

        if ( ( condition & IOCondition.IN ) == IOCondition.IN )
        {
            if ( readfunc != null )
                readfunc( this );
            return true;
        }

        logger.critical( @"ActionCallback called with unknown condition %d".printf( condition ) );

        return false;
    }

    private bool writeCallback( IOChannel source, IOCondition condition )
    {
        assert( logger.debug( "WriteCallback called with %u bytes in buffer".printf( buffer.len ) ) );
        /*
        for( int i = 0; i < buffer.len; ++i )
        logger.debug( "byte: 0x%02x".printf( buffer.data[i] ) );
        */
        int len = 64 > buffer.len? (int)buffer.len : 64;
        var byteswritten = _write( buffer.data, len );
        assert( logger.debug( "WriteCallback wrote %d bytes".printf( (int)byteswritten ) ) );
        buffer.remove_range( 0, (int)byteswritten );

        return ( buffer.len != 0 );
    }

    internal int _write( void* data, int len )
    {
        if ( fd != -1 )
        {
            ssize_t byteswritten = Posix.write( fd, data, len );
            return (int)byteswritten;
        }
        else
        {
            logger.warning( @"Attempting to write $len bytes to an invalid transport; discarding data" );
            return 0;
        }
    }

    protected virtual void configure()
    {
{'fraction_non_alphanumeric': 0.08800356771220455, 'fraction_numerical': 0.009959863237698825, 'mean_word_length': 2.389420654911839, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6880718', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1770, 'n_words': 667}