summaryrefslogtreecommitdiff
path: root/src/main/kotlin/MailServer.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/MailServer.kt')
-rw-r--r--src/main/kotlin/MailServer.kt30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/main/kotlin/MailServer.kt b/src/main/kotlin/MailServer.kt
new file mode 100644
index 0000000..94a53c9
--- /dev/null
+++ b/src/main/kotlin/MailServer.kt
@@ -0,0 +1,30 @@
+import kotlinx.coroutines.*
+import java.net.ServerSocket
+import java.net.Socket
+import kotlin.coroutines.EmptyCoroutineContext
+
+class MailServer(
+ val localhostName: String,
+ val scope: CoroutineScope = CoroutineScope(EmptyCoroutineContext)
+) {
+
+ fun createAndLaunchHandlerFor(socket: Socket): Job {
+ val protocol = SMTPReceiveProtocol(localhostName, socket.inetAddress)
+ return protocol.executeAsync(scope + CoroutineName("connection handler from ${socket.inetAddress}"), Protocol.IO.FromSocket(socket))
+ }
+
+ suspend fun createServer(port: Int) {
+ listenToServerSocket(ServerSocket(port))
+ }
+
+ suspend fun listenToServerSocket(serverSocket: ServerSocket) {
+ withContext(Dispatchers.Unconfined) {
+ while (true) {
+ val newIncomingConnection =
+ withContext(Dispatchers.IO) { serverSocket.accept() }
+ createAndLaunchHandlerFor(newIncomingConnection)
+ }
+ }
+ }
+
+}