mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-24 16:40:05 +08:00
add android support
This commit is contained in:
parent
29c93fb875
commit
8b96411067
@ -10,17 +10,19 @@ class SecurityNioServer(
|
||||
port: Int,
|
||||
backlog: Int = 50,
|
||||
coroutineScope: CoroutineScope = GlobalScope,
|
||||
autoCloseSocket: Boolean = true,
|
||||
@Suppress("MemberVisibilityCanBePrivate") val rsa: RSA = RSA(),
|
||||
val handler: suspend AsyncSocket.() -> Unit
|
||||
) : NioServer(port, backlog, coroutineScope, {
|
||||
) : NioServer(port, backlog, coroutineScope, autoCloseSocket, {
|
||||
AsyncSocketSecurityUtil.initActiveAESSocket(this, rsa)
|
||||
handler()
|
||||
}) {
|
||||
constructor(
|
||||
port: Int,
|
||||
keySize: Int,
|
||||
backlog: Int = 50,
|
||||
coroutineScope: CoroutineScope = GlobalScope,
|
||||
keySize: Int,
|
||||
autoCloseSocket: Boolean = true,
|
||||
handler: suspend AsyncSocket.() -> Unit
|
||||
) : this(port, backlog, coroutineScope, RSA(keySize), handler)
|
||||
) : this(port, backlog, coroutineScope, autoCloseSocket, RSA(keySize), handler)
|
||||
}
|
@ -75,7 +75,7 @@ public class EchoServer implements Closeable, Runnable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRead(@NotNull SelectionKey key, @NotNull NioThread nioThread) throws Throwable {
|
||||
public void handleRead(@NotNull SelectionKey key, @NotNull NioThread nioThread) {
|
||||
ByteBuffer buffer = (ByteBuffer) key.attachment();
|
||||
SocketChannel channel = (SocketChannel) key.channel();
|
||||
ByteBufferExtensionKt.read(channel, buffer);
|
||||
@ -83,7 +83,7 @@ public class EchoServer implements Closeable, Runnable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleWrite(@NotNull SelectionKey key, @NotNull NioThread nioThread) throws Throwable {
|
||||
public void handleWrite(@NotNull SelectionKey key, @NotNull NioThread nioThread) {
|
||||
ByteBuffer buffer = (ByteBuffer) key.attachment();
|
||||
SocketChannel channel = (SocketChannel) key.channel();
|
||||
ByteBufferExtensionKt.write(channel, buffer);
|
||||
|
@ -0,0 +1,80 @@
|
||||
package cn.tursom.socket.server;
|
||||
|
||||
import cn.tursom.core.buffer.ByteBuffer;
|
||||
import cn.tursom.core.buffer.ByteBufferExtensionKt;
|
||||
import cn.tursom.core.buffer.impl.DirectByteBuffer;
|
||||
import cn.tursom.niothread.NioProtocol;
|
||||
import cn.tursom.niothread.NioThread;
|
||||
import cn.tursom.niothread.WorkerLoopNioThread;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
public class NioLoopServerTest implements Runnable, Closeable {
|
||||
private static final NioProtocol protocol = new NioProtocol() {
|
||||
@Override
|
||||
public void handleConnect(@NotNull SelectionKey key, @NotNull NioThread nioThread) {
|
||||
key.interestOps(SelectionKey.OP_READ);
|
||||
key.attach(new DirectByteBuffer(1024));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRead(@NotNull SelectionKey key, @NotNull NioThread nioThread) {
|
||||
ByteBuffer buffer = (ByteBuffer) key.attachment();
|
||||
SocketChannel channel = (SocketChannel) key.channel();
|
||||
ByteBufferExtensionKt.read(channel, buffer);
|
||||
key.interestOps(SelectionKey.OP_WRITE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleWrite(@NotNull SelectionKey key, @NotNull NioThread nioThread) {
|
||||
ByteBuffer buffer = (ByteBuffer) key.attachment();
|
||||
SocketChannel channel = (SocketChannel) key.channel();
|
||||
ByteBufferExtensionKt.write(channel, buffer);
|
||||
key.interestOps(SelectionKey.OP_WRITE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCause(@NotNull SelectionKey key, @NotNull NioThread nioThread, @NotNull Throwable e) throws Throwable {
|
||||
e.printStackTrace();
|
||||
key.cancel();
|
||||
key.channel().close();
|
||||
}
|
||||
};
|
||||
private final int port;
|
||||
private final NioLoopServer server;
|
||||
|
||||
public NioLoopServerTest(int port) {
|
||||
this.port = port;
|
||||
server = new NioLoopServer(port, protocol, 50, (threadName, workLoop) -> {
|
||||
try {
|
||||
return new WorkerLoopNioThread(threadName, Selector.open(), true, 3000, workLoop);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
server.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
server.run();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
NioLoopServerTest server = new NioLoopServerTest(12345);
|
||||
server.run();
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package cn.tursom.core.encrypt
|
||||
|
||||
import sun.security.provider.DSAPublicKey
|
||||
import java.security.KeyFactory
|
||||
import java.security.KeyPair
|
||||
import java.security.KeyPairGenerator
|
||||
import java.security.interfaces.DSAPrivateKey
|
||||
import java.security.interfaces.DSAPublicKey
|
||||
import java.security.spec.X509EncodedKeySpec
|
||||
|
||||
@Suppress("unused", "MemberVisibilityCanBePrivate")
|
||||
|
@ -39,21 +39,34 @@ class ECC(
|
||||
generator.generateKeyPair()
|
||||
})
|
||||
|
||||
constructor(keySize: Int = 256, standardCurveLine: String = StandardCurveLine.secp256k1.name) : this(keySize, ECGenParameterSpec(standardCurveLine))
|
||||
constructor(keySize: Int = 256, standardCurveLine: StandardCurveLine) : this(keySize, standardCurveLine.name)
|
||||
constructor(publicKey: ByteArray) : this(KeyFactory.getInstance("EC").generatePublic(X509EncodedKeySpec(publicKey)) as ECPublicKey)
|
||||
constructor(
|
||||
keySize: Int = 256,
|
||||
standardCurveLine: String = StandardCurveLine.secp256k1.name.replace('_', ' ')
|
||||
) : this(
|
||||
keySize,
|
||||
ECGenParameterSpec(standardCurveLine)
|
||||
)
|
||||
|
||||
constructor(keySize: Int = 256, standardCurveLine: StandardCurveLine) : this(
|
||||
keySize,
|
||||
standardCurveLine.name.replace('_', ' ')
|
||||
)
|
||||
|
||||
constructor(publicKey: ByteArray) : this(
|
||||
KeyFactory.getInstance("EC").generatePublic(X509EncodedKeySpec(publicKey)) as ECPublicKey
|
||||
)
|
||||
|
||||
override fun signature(digest: String): String = "${digest}withECDSA"
|
||||
|
||||
@Suppress("EnumEntryName", "SpellCheckingInspection")
|
||||
enum class StandardCurveLine {
|
||||
secp224r1, `NIST B-233`, secp160r1, secp160r2, `NIST K-233`, sect163r2, secp128r1, sect163r1, `NIST P-256`,
|
||||
sect409r1, `NIST B-163`, `NIST B-283`, secp128r2, brainpoolP192r1, secp192r1, brainpoolP256r1, `NIST K-283`,
|
||||
secp256r1, `NIST P-384`, sect113r2, sect163k1, `NIST K-163`, `NIST B-409`, secp224k1, sect239k1, sect193r2,
|
||||
`NIST K-409`, secp112r2, sect113r1, brainpoolP320r1, secp112r1, secp160k1, `NIST P-224`, sect193r1, sect233k1,
|
||||
sect571r1, `NIST P-192`, sect409k1, `NIST B-571`, brainpoolP224r1, sect233r1, sect571k1, brainpoolP160r1,
|
||||
`NIST K-571`, secp256k1, secp192k1, sect283k1, sect283r1, secp384r1, secp521r1, sect131r1, sect131r2,
|
||||
brainpoolP384r1, brainpoolP512r1, `NIST P-521`
|
||||
secp224r1, `NIST_B-233`, secp160r1, secp160r2, `NIST_K-233`, sect163r2, secp128r1, sect163r1, `NIST_P-256`,
|
||||
sect409r1, `NIST_B-163`, `NIST_B-283`, secp128r2, brainpoolP192r1, secp192r1, brainpoolP256r1, `NIST_K-283`,
|
||||
secp256r1, `NIST_P-384`, sect113r2, sect163k1, `NIST_K-163`, `NIST_B-409`, secp224k1, sect239k1, sect193r2,
|
||||
`NIST_K-409`, secp112r2, sect113r1, brainpoolP320r1, secp112r1, secp160k1, `NIST_P-224`, sect193r1, sect233k1,
|
||||
sect571r1, `NIST_P-192`, sect409k1, `NIST_B-571`, brainpoolP224r1, sect233r1, sect571k1, brainpoolP160r1,
|
||||
`NIST_K-571`, secp256k1, secp192k1, sect283k1, sect283r1, secp384r1, secp521r1, sect131r1, sect131r2,
|
||||
brainpoolP384r1, brainpoolP512r1, `NIST_P-521`
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
Loading…
Reference in New Issue
Block a user