Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ _Note: In order to use self-signed certificates make sure to [update your metro.
* [`createConnection(options[, callback])`](#createconnection)
* [`write(data[, encoding][, callback])`](#write)
* [`destroy()`](#destroy)
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)

#### `createConnection()`
`createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,14 @@ public void close() {
mReceiverListener.onClose(getId(), e.getMessage());
}
}

/**
* @param noDelay `true` will disable Nagle's algorithm for the socket (enable TCP_NODELAY)
*/
public void setNoDelay(final boolean noDelay) throws IOException {
if (socket == null) {
throw new IOException("Socket is not connected.");
}
socket.setTcpNoDelay(noDelay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ protected void doInBackgroundGuarded(Void... params) {
}.executeOnExecutor(executorService);
}

@ReactMethod
public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) {
final TcpSocketClient client = socketClients.get(cId);
if (client == null) {
onError(cId, TAG + "socket not found.");
return;
}
try {
client.setNoDelay(noDelay);
} catch (IOException e) {
onError(cId, e.getMessage());
}
}

private void requestNetwork(final int transportType) throws InterruptedException {
final NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
requestBuilder.addTransportType(transportType);
Expand Down
1 change: 1 addition & 0 deletions ios/TcpSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ typedef enum RCTTCPError RCTTCPError;
*/
- (void)destroy;

- (void)setNoDelay:(BOOL)noDelay;

@end
13 changes: 13 additions & 0 deletions ios/TcpSocketClient.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <netinet/in.h>
#import <netinet/tcp.h>
#import <arpa/inet.h>
#import "TcpSocketClient.h"

Expand Down Expand Up @@ -124,6 +125,18 @@ - (BOOL)connect:(NSString *)host port:(int)port withOptions:(NSDictionary *)opti
@"family": @"unkown" };
}

- (void)setNoDelay:(BOOL)noDelay
{
[_tcpSocket performBlock:^{
int fd = [self->_tcpSocket socketFD];
int on = noDelay ? 1 : 0;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)) == -1) {
/* TODO: handle error */
RCTLogWarn(@"setNoDelay caused an unexpected error");
}
}];
}

- (BOOL)listen:(NSDictionary *)options error:(NSError **)error
{
if (_tcpSocket) {
Expand Down
7 changes: 7 additions & 0 deletions ios/TcpSockets.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ - (TcpSocketClient *)createSocket:(nonnull NSNumber*)cId
}
}

RCT_EXPORT_METHOD(setNoDelay:(nonnull NSNumber*)cId noDelay:(BOOL)noDelay) {
TcpSocketClient* client = [self findClient:cId];
if (!client) return;

[client setNoDelay:noDelay];
}

- (void)onConnect:(TcpSocketClient*) client
{
[self sendEventWithName:@"connect"
Expand Down
13 changes: 13 additions & 0 deletions src/TcpSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ export default class TcpSocket {
return this;
}

/**
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
*
* Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.
*
* Passing `true` for `noDelay` or not passing an argument will disable Nagle's algorithm for the socket. Passing false for noDelay will enable Nagle's algorithm.
*
* @param {boolean} noDelay
*/
setNoDelay(noDelay = true) {
Sockets.setNoDelay(this._id, noDelay);
}

address() {
return this._address;
}
Expand Down