Package org.apache.tomcat.jni.socket
Class AprSocket
- java.lang.Object
-
- org.apache.tomcat.jni.socket.AprSocket
-
- All Implemented Interfaces:
java.lang.Runnable
public class AprSocket extends java.lang.Object implements java.lang.Runnable
Native socket, using JNI + APR + openssl. The socket is non-blocking - you can register either a blocking or non blocking callback. There is no explicit method to register/unregister poll interest - it is done automatically, when read/write methods return 0. To keep the socket polling you must read all the available data, until read() returns 0. If you want to pause - don't read all input. To resume - read again until it returns 0. Same for write - when write() returns 0 the socket is registered for write interest. You can also use the blocking read/write methods.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
ACCEPTED
(package private) static int
CLOSED
(package private) static int
CONNECTED
(package private) static int
CONNECTING
private AprSocketContext
context
(package private) static int
ERROR
(package private) AprSocketContext.BlockingPollHandler
handler
private HostInfo
hostInfo
private static java.util.logging.Logger
log
private static byte[][]
NO_CERTS
(package private) static int
POLL
(package private) AprSocketContext.AprPoller
poller
(package private) static int
POLLIN
(package private) static int
POLLIN_ACTIVE
(package private) static int
POLLOUT
(package private) static int
POLLOUT_ACTIVE
(package private) static int
READING
(package private) long
socket
(package private) static int
SSL_ATTACHED
private int
status
(package private) static int
WRITING
-
Constructor Summary
Constructors Constructor Description AprSocket(AprSocketContext context)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
afterConnect()
void
blockingStartTLS()
This is a blocking call ! ( can be made non-blocking, but too complex ) Will be called automatically after connect() or accept if 'secure' is true.(package private) boolean
checkBitAndSocket(int bit)
(package private) boolean
checkPreConnect(int bit)
(package private) void
clearStatus(int bit)
void
close()
void
connect()
java.lang.String
getCipherSuite()
AprSocketContext
getContext()
AprSocketContext.BlockingPollHandler
getHandler()
HostInfo
getHost()
long
getIOTimeout()
int
getKeySize()
java.lang.String
getLocalAddress()
java.lang.String
getLocalHostname()
int
getLocalPort()
byte[][]
getPeerCert(boolean check)
java.security.cert.X509Certificate[]
getPeerX509Cert()
java.lang.String
getRemoteAddress()
java.lang.String
getRemoteHostname()
int
getRemotePort()
private void
handshakeDone()
boolean
isBlocking()
boolean
isClosed()
boolean
isError()
boolean
isPolling()
Check if the socket is currently registered with a poller.(package private) void
maybeDestroy()
private void
notifyConnected(boolean server)
(package private) void
notifyError(java.lang.Throwable err, boolean needsThread)
(package private) void
notifyIO()
private int
processReadResult(byte[] data, int off, int len, int read)
int
read(byte[] data, int off, int len)
int
read(byte[] data, int off, int len, long to)
int
readNB(byte[] data, int off, int len)
void
recycle()
(package private) int
requestedPolling()
void
reset()
Close input and output, potentially sending RST, than close the socket.void
run()
void
setHandler(AprSocketContext.BlockingPollHandler l)
(package private) AprSocket
setHost(HostInfo hi)
private void
setNonBlocking()
(package private) boolean
setStatus(int bit)
java.lang.String
toString()
private void
updatePolling()
int
write(byte[] data, int off, int len)
int
write(byte[] data, int off, int len, long to)
Write as much data as possible to the socket.private int
writeInternal(byte[] data, int off, int len)
-
-
-
Field Detail
-
log
private static final java.util.logging.Logger log
-
NO_CERTS
private static final byte[][] NO_CERTS
-
CONNECTING
static final int CONNECTING
- See Also:
- Constant Field Values
-
CONNECTED
static final int CONNECTED
- See Also:
- Constant Field Values
-
POLLIN_ACTIVE
static final int POLLIN_ACTIVE
- See Also:
- Constant Field Values
-
POLLOUT_ACTIVE
static final int POLLOUT_ACTIVE
- See Also:
- Constant Field Values
-
POLL
static final int POLL
- See Also:
- Constant Field Values
-
SSL_ATTACHED
static final int SSL_ATTACHED
- See Also:
- Constant Field Values
-
POLLIN
static final int POLLIN
- See Also:
- Constant Field Values
-
POLLOUT
static final int POLLOUT
- See Also:
- Constant Field Values
-
ACCEPTED
static final int ACCEPTED
- See Also:
- Constant Field Values
-
ERROR
static final int ERROR
- See Also:
- Constant Field Values
-
CLOSED
static final int CLOSED
- See Also:
- Constant Field Values
-
READING
static final int READING
- See Also:
- Constant Field Values
-
WRITING
static final int WRITING
- See Also:
- Constant Field Values
-
context
private final AprSocketContext context
-
handler
AprSocketContext.BlockingPollHandler handler
-
poller
AprSocketContext.AprPoller poller
-
status
private int status
-
socket
long socket
-
hostInfo
private HostInfo hostInfo
-
-
Constructor Detail
-
AprSocket
AprSocket(AprSocketContext context)
-
-
Method Detail
-
recycle
public void recycle()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
setHandler
public void setHandler(AprSocketContext.BlockingPollHandler l)
-
setNonBlocking
private void setNonBlocking()
-
isPolling
public boolean isPolling()
Check if the socket is currently registered with a poller.
-
getHandler
public AprSocketContext.BlockingPollHandler getHandler()
-
getContext
public AprSocketContext getContext()
-
connect
public void connect() throws java.io.IOException
- Throws:
java.io.IOException
-
afterConnect
void afterConnect() throws java.io.IOException
- Throws:
java.io.IOException
-
getHost
public HostInfo getHost()
-
write
public int write(byte[] data, int off, int len, long to) throws java.io.IOException
Write as much data as possible to the socket.- Parameters:
data
-off
-len
-- Returns:
- For both blocking and non-blocking, returns the number of bytes written. If no data can be written (e.g. if the buffers are full) 0 will be returned.
- Throws:
java.io.IOException
-
write
public int write(byte[] data, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
writeInternal
private int writeInternal(byte[] data, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read(byte[] data, int off, int len, long to) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read(byte[] data, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
processReadResult
private int processReadResult(byte[] data, int off, int len, int read) throws java.io.IOException
- Throws:
java.io.IOException
-
readNB
public int readNB(byte[] data, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close()
-
maybeDestroy
void maybeDestroy()
-
reset
public void reset()
Close input and output, potentially sending RST, than close the socket. The proper way to close when gracefully done is by calling writeEnd() and reading all remaining input until -1 (EOF) is received. If EOF is received, the proper way to close is send whatever is remaining and call writeEnd();
-
isClosed
public boolean isClosed()
-
getIOTimeout
public long getIOTimeout() throws java.io.IOException
- Throws:
java.io.IOException
-
getPeerCert
public byte[][] getPeerCert(boolean check) throws java.io.IOException
- Throws:
java.io.IOException
-
getPeerX509Cert
public java.security.cert.X509Certificate[] getPeerX509Cert() throws java.io.IOException
- Throws:
java.io.IOException
-
getCipherSuite
public java.lang.String getCipherSuite() throws java.io.IOException
- Throws:
java.io.IOException
-
getKeySize
public int getKeySize() throws java.io.IOException
- Throws:
java.io.IOException
-
getRemotePort
public int getRemotePort() throws java.io.IOException
- Throws:
java.io.IOException
-
getRemoteAddress
public java.lang.String getRemoteAddress() throws java.io.IOException
- Throws:
java.io.IOException
-
getRemoteHostname
public java.lang.String getRemoteHostname() throws java.io.IOException
- Throws:
java.io.IOException
-
getLocalPort
public int getLocalPort() throws java.io.IOException
- Throws:
java.io.IOException
-
getLocalAddress
public java.lang.String getLocalAddress() throws java.io.IOException
- Throws:
java.io.IOException
-
getLocalHostname
public java.lang.String getLocalHostname() throws java.io.IOException
- Throws:
java.io.IOException
-
isBlocking
public boolean isBlocking()
-
isError
public boolean isError()
-
notifyError
void notifyError(java.lang.Throwable err, boolean needsThread)
-
notifyIO
void notifyIO() throws java.io.IOException
- Throws:
java.io.IOException
-
notifyConnected
private void notifyConnected(boolean server) throws java.io.IOException
- Throws:
java.io.IOException
-
updatePolling
private void updatePolling() throws java.io.IOException
- Throws:
java.io.IOException
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
blockingStartTLS
public void blockingStartTLS() throws java.io.IOException
This is a blocking call ! ( can be made non-blocking, but too complex ) Will be called automatically after connect() or accept if 'secure' is true. Can be called manually to upgrade the channel- Throws:
java.io.IOException
-
handshakeDone
private void handshakeDone() throws java.io.IOException
- Throws:
java.io.IOException
-
requestedPolling
int requestedPolling()
-
checkBitAndSocket
boolean checkBitAndSocket(int bit)
-
checkPreConnect
boolean checkPreConnect(int bit)
-
clearStatus
void clearStatus(int bit)
-
setStatus
boolean setStatus(int bit)
-
-