Skip to content

Commit

Permalink
Merge pull request #13 from eatkins/max-socket-length
Browse files Browse the repository at this point in the history
Add maxSocketLength to native api
  • Loading branch information
eed3si9n authored Nov 24, 2020
2 parents 8e965c8 + e511655 commit 9364457
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@ Java_org_scalasbt_ipcsocket_JNIUnixDomainSocketLibraryProvider_errString(
const char *err = strerror(code);
return (*env)->NewStringUTF(env, err);
}

JNIEXPORT jint JNICALL
Java_org_scalasbt_ipcsocket_JNIUnixDomainSocketLibraryProvider_maxSocketLength(
UNUSED JNIEnv *env, UNUSED jobject object) {
struct sockaddr_un un;
return sizeof(un.sun_path);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ private int returnOrThrow(int result, int threshold) throws NativeErrorException

native int shutdownNative(int fd, int how);

public native int maxSocketLength();

native String errString(int error);

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,9 @@ public int shutdown(int fd, int how) throws NativeErrorException {
throw new NativeErrorException(e.getErrorCode(), e.getMessage());
}
}

@Override
public int maxSocketLength() {
return new UnixDomainSocketLibrary.SockaddrUn().sunPath.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ public interface UnixDomainSocketLibraryProvider {

int shutdown(int fd, int how) throws NativeErrorException;

int maxSocketLength();

static UnixDomainSocketLibraryProvider get(boolean useJNI) {
return useJNI
? JNIUnixDomainSocketLibraryProvider.instance()
: JNAUnixDomainSocketLibraryProvider.instance();
}

static int maxSocketLength(boolean useJNI) {
return get(useJNI).maxSocketLength();
}
}
Binary file modified src/main/resources/darwin/x86_64/libsbtipcsocket.dylib
Binary file not shown.
Binary file modified src/main/resources/linux/x86_64/libsbtipcsocket.so
Binary file not shown.
Binary file modified src/main/resources/win32/x86_64/sbtipcsocket.dll
Binary file not shown.
26 changes: 26 additions & 0 deletions src/test/java/org/scalasbt/ipcsocket/UnixSocketLengthTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.scalasbt.ipcsocket;

import org.junit.Test;
import static org.junit.Assert.*;

public class UnixSocketLengthTest {
final boolean isWin = System.getProperty("os.name", "").toLowerCase().startsWith("win");
final boolean isLinux = System.getProperty("os.name", "").toLowerCase().startsWith("linux");

@Test
public void testJNIMaxSocketLength() {
if (!isWin) {
int length = UnixDomainSocketLibraryProvider.maxSocketLength(true);
int expectedLength = isLinux ? 108 : 104;
assert (length == expectedLength);
}
}

@Test
public void testJNAMaxSocketLength() {
if (!isWin) {
int length = UnixDomainSocketLibraryProvider.maxSocketLength(false);
assert (length == 104);
}
}
}

0 comments on commit 9364457

Please sign in to comment.