Skip to content

Commit 7616104

Browse files
committed
set java.io and ktor socket optionals
1 parent 2e7b177 commit 7616104

File tree

49 files changed

+485
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+485
-201
lines changed

common/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ afterEvaluate {
5050
}
5151

5252
dependencies {
53+
implementation(libs.ktor.network)
54+
implementation(libs.ktor.network.tls)
5355
implementation(libs.androidx.annotation)
5456
implementation(libs.kotlinx.coroutines.android)
5557
testImplementation(libs.kotlinx.coroutines.test)

common/src/main/java/com/pedro/common/socket/StreamSocket.kt

Lines changed: 0 additions & 32 deletions
This file was deleted.

common/src/main/java/com/pedro/common/socket/TcpStreamSocketBase.kt

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.pedro.common.socket.base
2+
3+
enum class SocketType {
4+
KTOR, JAVA
5+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
*
3+
* * Copyright (C) 2024 pedroSG94.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * http://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package com.pedro.common.socket.base
20+
21+
import com.pedro.common.socket.java.TcpStreamSocketJava
22+
import com.pedro.common.socket.java.UdpStreamSocketJava
23+
import com.pedro.common.socket.ktor.TcpStreamSocketKtor
24+
import com.pedro.common.socket.ktor.UdpStreamSocketKtor
25+
import javax.net.ssl.TrustManager
26+
27+
/**
28+
* Created by pedro on 22/9/24.
29+
*/
30+
abstract class StreamSocket {
31+
protected val timeout = 5000L
32+
abstract suspend fun connect()
33+
abstract suspend fun close()
34+
abstract fun isConnected(): Boolean
35+
abstract fun isReachable(): Boolean
36+
37+
companion object {
38+
fun createTcpSocket(
39+
type: SocketType,
40+
host: String, port: Int, secured: Boolean, certificates: TrustManager? = null
41+
): TcpStreamSocket {
42+
return when (type) {
43+
SocketType.KTOR -> TcpStreamSocketKtor(host, port, secured, certificates)
44+
SocketType.JAVA -> TcpStreamSocketJava(host, port, secured, certificates)
45+
}
46+
}
47+
48+
fun createUdpSocket(
49+
type: SocketType,
50+
host: String, port: Int, sourcePort: Int? = null, receiveSize: Int? = null, udpType: UdpType = UdpType.UNICAST
51+
): UdpStreamSocket {
52+
return when (type) {
53+
SocketType.KTOR -> UdpStreamSocketKtor(host, port, sourcePort, receiveSize, udpType)
54+
SocketType.JAVA -> UdpStreamSocketJava(host, port, sourcePort, receiveSize, udpType)
55+
}
56+
}
57+
}
58+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.pedro.common.socket.base
2+
3+
abstract class TcpStreamSocket: StreamSocket() {
4+
abstract suspend fun write(bytes: ByteArray)
5+
abstract suspend fun write(bytes: ByteArray, offset: Int, size: Int)
6+
abstract suspend fun write(b: Int)
7+
abstract suspend fun write(string: String)
8+
abstract suspend fun flush()
9+
abstract suspend fun read(bytes: ByteArray)
10+
abstract suspend fun read(size: Int): ByteArray
11+
abstract suspend fun readLine(): String?
12+
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.pedro.common.socket.base
2+
3+
abstract class UdpStreamSocket: StreamSocket() {
4+
abstract suspend fun write(bytes: ByteArray)
5+
abstract suspend fun read(): ByteArray
6+
}

common/src/main/java/com/pedro/common/socket/UdpType.kt renamed to common/src/main/java/com/pedro/common/socket/base/UdpType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.pedro.common.socket
17+
package com.pedro.common.socket.base
1818

1919
/**
2020
* Created by pedro on 6/3/24.

common/src/main/java/com/pedro/common/socket/TcpStreamSocket.kt renamed to common/src/main/java/com/pedro/common/socket/java/TcpStreamSocketJava.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pedro.common.socket
1+
package com.pedro.common.socket.java
22

33
import java.io.IOException
44
import java.net.InetSocketAddress
@@ -10,12 +10,12 @@ import javax.net.ssl.SSLContext
1010
import javax.net.ssl.SSLSocket
1111
import javax.net.ssl.TrustManager
1212

13-
class TcpStreamSocket(
13+
class TcpStreamSocketJava(
1414
private val host: String,
1515
private val port: Int,
1616
private val secured: Boolean,
1717
private val certificates: TrustManager? = null
18-
): TcpStreamSocketBase() {
18+
): TcpStreamSocketJavaBase() {
1919

2020
override fun onConnectSocket(timeout: Long): Socket {
2121
val socket = if (secured) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.pedro.common.socket.java
2+
3+
import com.pedro.common.readUntil
4+
import com.pedro.common.socket.base.TcpStreamSocket
5+
import java.io.BufferedReader
6+
import java.io.ByteArrayInputStream
7+
import java.io.ByteArrayOutputStream
8+
import java.io.InputStreamReader
9+
import java.net.Socket
10+
11+
abstract class TcpStreamSocketJavaBase: TcpStreamSocket() {
12+
13+
private var socket = Socket()
14+
private var input = ByteArrayInputStream(byteArrayOf()).buffered()
15+
private var output = ByteArrayOutputStream().buffered()
16+
private var reader = InputStreamReader(input).buffered()
17+
18+
abstract fun onConnectSocket(timeout: Long): Socket
19+
20+
override suspend fun connect() {
21+
socket = onConnectSocket(timeout)
22+
reader = BufferedReader(InputStreamReader(socket.getInputStream()))
23+
output = socket.getOutputStream().buffered()
24+
input = socket.getInputStream().buffered()
25+
}
26+
27+
override suspend fun close() {
28+
if (socket.isConnected) {
29+
runCatching { socket.shutdownOutput() }
30+
runCatching { socket.shutdownInput() }
31+
runCatching { socket.close() }
32+
}
33+
}
34+
35+
override suspend fun write(bytes: ByteArray) {
36+
output.write(bytes)
37+
}
38+
39+
override suspend fun write(bytes: ByteArray, offset: Int, size: Int) {
40+
output.write(bytes, offset, size)
41+
}
42+
43+
override suspend fun write(b: Int) {
44+
output.write(b)
45+
}
46+
47+
override suspend fun write(string: String) {
48+
write(string.toByteArray())
49+
}
50+
51+
override suspend fun flush() {
52+
output.flush()
53+
}
54+
55+
override suspend fun read(bytes: ByteArray) {
56+
input.readUntil(bytes)
57+
}
58+
59+
override suspend fun read(size: Int): ByteArray {
60+
val data = ByteArray(size)
61+
read(data)
62+
return data
63+
}
64+
65+
override suspend fun readLine(): String? = reader.readLine()
66+
67+
override fun isConnected(): Boolean = socket.isConnected
68+
69+
override fun isReachable(): Boolean = socket.inetAddress?.isReachable(timeout.toInt()) ?: false
70+
}

0 commit comments

Comments
 (0)