add minimal java.net.Socket implementation to support Socket.setTcpNoDelay

This commit is contained in:
Joel Dice
2008-11-22 15:32:53 -07:00
parent 6162dfafbb
commit fccf906349
5 changed files with 111 additions and 5 deletions

View File

@ -26,6 +26,7 @@
# include <errno.h>
# include <netdb.h>
# include <sys/select.h>
# include <netinet/tcp.h>
#endif
#define java_nio_channels_SelectionKey_OP_READ 1L
@ -88,6 +89,26 @@ throwIOException(JNIEnv* e)
throwIOException(e, errorString(e));
}
void
throwSocketException(JNIEnv* e, const char* s)
{
throwNew(e, "java/net/SocketException", s);
}
void
throwSocketException(JNIEnv* e, jbyteArray a)
{
jbyte* s = static_cast<jbyte*>(e->GetPrimitiveArrayCritical(a, 0));
throwSocketException(e, reinterpret_cast<const char*>(s));
e->ReleasePrimitiveArrayCritical(a, s, 0);
}
void
throwSocketException(JNIEnv* e)
{
throwSocketException(e, errorString(e));
}
void
init(JNIEnv* e, sockaddr_in* address, jstring hostString, jint port)
{
@ -152,6 +173,19 @@ makeNonblocking(JNIEnv* e, int d)
return true;
}
bool
setTcpNoDelay(JNIEnv* e, int d, bool on)
{
int flag = on;
int r = setsockopt
(d, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char*>(&flag), sizeof(int));
if (r < 0) {
throwSocketException(e);
return false;
}
return true;
}
void
doListen(JNIEnv* e, int s, sockaddr_in* address)
{
@ -277,6 +311,15 @@ Java_java_nio_channels_ServerSocketChannel_natDoListen(JNIEnv *e,
return s;
}
extern "C" JNIEXPORT void JNICALL
Java_java_nio_channels_SocketChannel_natSetTcpNoDelay(JNIEnv *e,
jclass,
jint socket,
jboolean on)
{
setTcpNoDelay(e, socket, on);
}
extern "C" JNIEXPORT jint JNICALL
Java_java_nio_channels_SocketChannel_natDoConnect(JNIEnv *e,
jclass,