Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.firefly.net.tcp.SecureTcpConnectionImpl Maven / Gradle / Ivy
package com.firefly.net.tcp;
import com.firefly.net.SecureSession;
import com.firefly.net.Session;
import com.firefly.net.buffer.FileRegion;
import com.firefly.utils.concurrent.Callback;
import com.firefly.utils.concurrent.Promise;
import com.firefly.utils.function.Action0;
import com.firefly.utils.function.Action1;
import com.firefly.utils.function.Action2;
import com.firefly.utils.io.BufferUtils;
import com.firefly.utils.io.IO;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class SecureTcpConnectionImpl extends AbstractTcpConnection {
protected final SecureSession secureSession;
public SecureTcpConnectionImpl(Session session, SecureSession secureSession) {
super(session);
this.secureSession = secureSession;
}
@Override
public CompletableFuture writeToFuture(ByteBuffer byteBuffer) {
Promise.Completable c = new Promise.Completable<>();
write(byteBuffer, () -> c.succeeded(true), c::failed);
return c;
}
@Override
public CompletableFuture writeToFuture(ByteBuffer[] byteBuffer) {
Promise.Completable c = new Promise.Completable<>();
write(byteBuffer, () -> c.succeeded(true), c::failed);
return c;
}
@Override
public CompletableFuture writeToFuture(Collection byteBuffer) {
return writeToFuture(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY));
}
@Override
public CompletableFuture writeToFuture(String message) {
return writeToFuture(message, DEFAULT_CHARSET);
}
@Override
public CompletableFuture writeToFuture(String message, String charset) {
ByteBuffer byteBuffer = BufferUtils.toBuffer(message, Charset.forName(charset));
return writeToFuture(byteBuffer);
}
@Override
public CompletableFuture writeToFuture(FileRegion file) {
Promise.Completable c = new Promise.Completable<>();
write(file, () -> c.succeeded(true), c::failed);
return c;
}
@Override
public TcpConnection write(ByteBuffer byteBuffer, Action0 succeeded, Action1 failed) {
_write(byteBuffer, this::_write, succeeded, failed);
return this;
}
@Override
public TcpConnection write(ByteBuffer[] byteBuffer, Action0 succeeded, Action1 failed) {
_write(byteBuffer, this::_write, succeeded, failed);
return this;
}
private void _write(T byteBuffer, Action2 writeAction, Action0 succeeded, Action1 failed) {
Callback callback = new Callback() {
@Override
public void succeeded() {
if (succeeded != null) {
succeeded.call();
}
}
@Override
public void failed(Throwable x) {
if (failed != null) {
failed.call(x);
}
}
};
writeAction.call(byteBuffer, callback);
}
private void _write(ByteBuffer[] byteBuffers, Callback callback) {
try {
secureSession.write(byteBuffers, callback);
} catch (Exception e) {
callback.failed(e);
}
}
private void _write(ByteBuffer byteBuffer, Callback callback) {
try {
secureSession.write(byteBuffer, callback);
} catch (Exception e) {
callback.failed(e);
}
}
private void _write(FileRegion file, Callback callback) {
try {
secureSession.transferFileRegion(file, callback);
} catch (Exception e) {
callback.failed(e);
}
}
@Override
public TcpConnection write(Collection byteBuffer, Action0 succeeded, Action1 failed) {
return write(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY), succeeded, failed);
}
@Override
public TcpConnection write(String message, Action0 succeeded, Action1 failed) {
write(message, DEFAULT_CHARSET, succeeded, failed);
return this;
}
@Override
public TcpConnection write(String message, String charset, Action0 succeeded, Action1 failed) {
write(BufferUtils.toBuffer(message, Charset.forName(charset)), succeeded, failed);
return this;
}
@Override
public TcpConnection write(FileRegion file, Action0 succeeded, Action1 failed) {
_write(file, this::_write, succeeded, failed);
return this;
}
@Override
public TcpConnection write(ByteBuffer byteBuffer, Action0 succeeded) {
return write(byteBuffer, succeeded, null);
}
@Override
public TcpConnection write(ByteBuffer[] byteBuffer, Action0 succeeded) {
return write(byteBuffer, succeeded, null);
}
@Override
public TcpConnection write(Collection byteBuffer, Action0 succeeded) {
return write(byteBuffer, succeeded, null);
}
@Override
public TcpConnection write(String message, Action0 succeeded) {
return write(message, DEFAULT_CHARSET, succeeded, null);
}
@Override
public TcpConnection write(String message, String charset, Action0 succeeded) {
return write(message, charset, succeeded, null);
}
@Override
public TcpConnection write(FileRegion file, Action0 succeeded) {
return write(file, succeeded, null);
}
@Override
public TcpConnection write(ByteBuffer byteBuffer) {
_write(byteBuffer, Callback.NOOP);
return this;
}
@Override
public TcpConnection write(ByteBuffer[] byteBuffer) {
_write(byteBuffer, Callback.NOOP);
return this;
}
@Override
public TcpConnection write(Collection byteBuffer) {
return write(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY));
}
@Override
public TcpConnection write(String message) {
return write(message, DEFAULT_CHARSET, null, null);
}
@Override
public TcpConnection write(String message, String charset) {
return write(message, charset, null, null);
}
@Override
public TcpConnection write(FileRegion file) {
return write(file, null, null);
}
@Override
public boolean isSecureConnection() {
return true;
}
@Override
public String getApplicationProtocol() {
return secureSession.getApplicationProtocol();
}
@Override
public List getSupportedApplicationProtocols() {
return secureSession.getSupportedApplicationProtocols();
}
@Override
public void close() {
IO.close(secureSession);
session.close();
}
}