Skip to content

BT Socket Factory creation and connecting with java code#457

Open
Aniket392 wants to merge 3 commits intowip/p2p_btfrom
wip/socket_factory
Open

BT Socket Factory creation and connecting with java code#457
Aniket392 wants to merge 3 commits intowip/p2p_btfrom
wip/socket_factory

Conversation

@Aniket392
Copy link
Contributor

  • Add C4BTSocketFactory, registers C4SocketFactory with LiteCore and handles all JNI callbacks (open, write, close, requestClose, dispose, attached)
  • Add LiteCoreBTSocket, bridges BluetoothSocket L2CAP channel with LiteCore's C4Socket; implements SocketFromCore with read loop, write queue, and flow control
  • Add NativeC4BTSocketFactory, JNI implementation for nRegisterFactory and nFromNative

@Aniket392 Aniket392 requested a review from pasin March 11, 2026 09:19
@pasin
Copy link
Contributor

pasin commented Mar 11, 2026

@pasin pasin requested a review from borrrden March 11, 2026 17:34
Copy link
Contributor

@pasin pasin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are lot of detail and setup in this PR, and I think we will need to setup an person review.

However, I have looked up where we put the current WebSocketFactory and CBLWebSocket classes. I think we should move LiteCoreBTSocket and C4BTSocketFactory to the EE repo under android's com.couchbase.lite.internal.. We may need to rename it without C4 and Core prefix (e.g. BluetoothSocketFactory and BluetoothSocket : Use the names that are consistent with others).

As a reference, where CBLWebSocket / SocketFactory are :

https://github.com/couchbase/couchbase-lite-java-common/blob/b7bcaf74dd4f4752ba75e05f5adaaf6988931df6/android/main/java/com/couchbase/lite/internal/replicator/CBLWebSocket.java

https://github.com/couchbaselabs/couchbase-lite-java-ee-root/blob/master/ee/common/main/java/com/couchbase/lite/internal/SocketFactory.java

cls_C4BTSocketFactory, m_open,
(jlong) socket,
(jlong) context, // factoryToken = context set at registration
jPeerID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on https://developer.android.com/ndk/guides/jni-tips, it's a good practice to call ExceptionCheck() and ExceptionClear() to handle both caught and runtime exception when calling Java code such as

if (env->ExceptionCheck()) {
        env->ExceptionDescribe(); // Log to logcat or stderr
        env->ExceptionClear();
    }

You may check if we have don't this anywhere else or have any helper function for this. If not, I suggest to create a helper function to do this or create a ticket to take care this later if the method doesn't declare throws.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently in code base i can't see similar checks
I will create ticket for same

// Heap-allocate a mutable copy so context can be set to itself.
auto* factory = new C4SocketFactory(kBTSocketFactory);
factory->context = factory; // token == pointer to the factory copy
c4socket_registerFactory(*factory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause the default c4socket factory to be a Bluetooth factory and I don't think we would want that. The correct way to return the socket factory for bluetooth is via C4PeerDiscoveryProvider's getSocketFactory() function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what i can understand is, we can override the getSocketFactory in c4PeerDiscovery.hh in BluetoothProvider
and then we don't need to register it, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is right. We don't need to register.

factory.context = context;

C4Socket* c4socket = c4socket_fromNative(factory, context, &addr);
c4socket_retain(c4socket);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket needs to be wrapped with TLS enabled socket to get the TLS encryption. This is what is done in iOS but currently c4SocketFromNativeWithTLS () is in the TLSCodec.hh, and the header is not included in the package as a public header.

Ref<C4Socket> C4PeerDiscoveryProvider::createIncomingSocketWithTLS(C4SocketFactory const& factory,
                                                                   void* nativeHandle,
                                                                   C4Address const& address)
{
    return net::c4SocketFromNativeWithTLS(factory, nativeHandle, address, _discovery.tlsContext().get());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants