Docs, logging config, store ref to Server.kt ret by Connection.kt

This commit is contained in:
2026-09-12 20:37:06 +01:00
parent 3bf9d83450
commit d5397f99a3
@@ -1,5 +1,7 @@
package com.shr4pnel.ferretirc
import com.shr4pnel.ferretirc.irc.Server
import com.shr4pnel.ferretirc.irc.util.Helpers
import com.shr4pnel.ferretirc.net.messages.ClientMessage
import com.shr4pnel.ferretirc.net.Connection
import com.shr4pnel.ferretirc.net.messages.ServerMessage
@@ -9,30 +11,48 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
class IrcClient(hostname: String, port: Int) {
class IrcClient(hostname: String, port: Int, enableLogging: Boolean = true) {
init {
KotlinLoggingConfiguration.logStartupMessage = false
Helpers.enableLogging = enableLogging
}
val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
lateinit var server: Server
@PublishedApi
internal val connection = Connection(hostname, port, scope)
/**
* Connect to the socket
*/
suspend fun connect() {
connection.connect()
server = connection.connect()
}
/**
* Send a message to the IRC server writer
* This is asynchronous and ordered
* @param message The client message to send to the server
*/
suspend fun queueMessage(message: ClientMessage) {
connection.writer.outgoingMessages.send(message)
}
/**
* Send a message to the IRC server writer
* This is asynchronous and ordered
* @param messages The client messages to send to the server
*/
suspend fun queueMessages(vararg messages: ClientMessage) {
messages.forEach {
connection.writer.outgoingMessages.send(it)
}
}
/**
* Close the coroutine scope controlling listeners, readers, writers etc
*/
fun close() {
scope.cancel()
}