<commit_before>package com.rimmer.yttrium.server.binary

import com.rimmer.yttrium.HttpException
import com.rimmer.yttrium.InvalidStateException
import com.rimmer.yttrium.NotFoundException
import com.rimmer.yttrium.UnauthorizedException
import com.rimmer.yttrium.router.Route
import com.rimmer.yttrium.router.RouteContext
import com.rimmer.yttrium.router.Router
import com.rimmer.yttrium.router.listener.RouteListener
import com.rimmer.yttrium.serialize.*
import com.rimmer.yttrium.server.http.checkQueries
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.EventLoop
import java.net.InetSocketAddress

enum class ResponseCode {
    Success, NoRoute, NotFound, InvalidArgs, NoPermission, InternalError, MiscError
}

fun routeHash(r: Route) = routeHash(r.name, r.version)
fun routeHash(name: String, version: Int = 0) = name.hashCode() + version * 3452056

class BinaryRouter(
    val router: Router,
    val listener: RouteListener? = null
): (ChannelHandlerContext, ByteBuf, ByteBuf, () -> Unit) -> Unit {
    private val segmentMap = router.routes.associateBy { routeHash(it) }

    override fun invoke(context: ChannelHandlerContext, source: ByteBuf, target: ByteBuf, f: () -> Unit) {
        val remote = (context.channel().remoteAddress() as? InetSocketAddress)?.hostName ?: ""
        val id = source.readVarInt()
        val route = segmentMap[id]
        if(route == null) {
            error(ResponseCode.NoRoute, "Route $id not found", target, f)
            return
        }

        val eventLoop = context.channel().eventLoop()
        val callId = listener?.onStart(eventLoop, route) ?: 0<commit_msg>Handle incorrect binary calls correctly<commit_after>package com.rimmer.yttrium.server.binary

import com.rimmer.yttrium.HttpException
import com.rimmer.yttrium.InvalidStateException
import com.rimmer.yttrium.NotFoundException
import com.rimmer.yttrium.UnauthorizedException
import com.rimmer.yttrium.router.Route
import com.rimmer.yttrium.router.RouteContext
import com.rimmer.yttrium.router.Router
import com.rimmer.yttrium.router.listener.RouteListener
import com.rimmer.yttrium.serialize.*
import com.rimmer.yttrium.server.http.checkQueries
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.EventLoop
import java.net.InetSocketAddress

enum class ResponseCode {
    Success, NoRoute, NotFound, InvalidArgs, NoPermission, InternalError, MiscError
}

fun routeHash(r: Route) = routeHash(r.name, r.version)
fun routeHash(name: String, version: Int = 0) = name.hashCode() + version * 3452056

class BinaryRouter(
    val router: Router,
    val listener: RouteListener? = null
): (ChannelHandlerContext, ByteBuf, ByteBuf, () -> Unit) -> Unit {
    private val segmentMap = router.routes.associateBy { routeHash(it) }

    override fun invoke(context: ChannelHandlerContext, source: ByteBuf, target: ByteBuf, f: () -> Unit) {
        val remote = (context.channel().remoteAddress() as? InetSocketAddress)?.hostName ?: ""
        val id = source.readVarInt()
        val route = segmentMap[id]
        if(route == null) {
            source.readObject { false }
            error(ResponseCode.NoRoute, "Route $id not found", target, f)
            return
        }

        val eventLoop = context.channel().eventLoop()
        val callId = listener?.onStart(eventLoop, route) ?: 0
{'fraction_non_alphanumeric': 0.10695344725987035, 'fraction_numerical': 0.005303476723629935, 'mean_word_length': 4.191131498470948, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4259423', 'n_tokens_mistral': 1034, 'n_tokens_neox': 993, 'n_words': 229}