Skip to content

Commit 634aa6d

Browse files
authored
feat: Add setNoDelay() method (#52)
1 parent 5d28d98 commit 634aa6d

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ _Note: In order to use self-signed certificates make sure to [update your metro.
184184
* [`createConnection(options[, callback])`](#createconnection)
185185
* [`write(data[, encoding][, callback])`](#write)
186186
* [`destroy()`](#destroy)
187+
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
187188

188189
#### `createConnection()`
189190
`createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options).

android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,14 @@ public void close() {
131131
mReceiverListener.onClose(getId(), e.getMessage());
132132
}
133133
}
134+
135+
/**
136+
* @param noDelay `true` will disable Nagle's algorithm for the socket (enable TCP_NODELAY)
137+
*/
138+
public void setNoDelay(final boolean noDelay) throws IOException {
139+
if (socket == null) {
140+
throw new IOException("Socket is not connected.");
141+
}
142+
socket.setTcpNoDelay(noDelay);
143+
}
134144
}

android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketModule.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ protected void doInBackgroundGuarded(Void... params) {
164164
}.executeOnExecutor(executorService);
165165
}
166166

167+
@ReactMethod
168+
public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) {
169+
final TcpSocketClient client = socketClients.get(cId);
170+
if (client == null) {
171+
onError(cId, TAG + "socket not found.");
172+
return;
173+
}
174+
try {
175+
client.setNoDelay(noDelay);
176+
} catch (IOException e) {
177+
onError(cId, e.getMessage());
178+
}
179+
}
180+
167181
private void requestNetwork(final int transportType) throws InterruptedException {
168182
final NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
169183
requestBuilder.addTransportType(transportType);

ios/TcpSocketClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,6 @@ typedef enum RCTTCPError RCTTCPError;
9292
*/
9393
- (void)destroy;
9494

95+
- (void)setNoDelay:(BOOL)noDelay;
9596

9697
@end

ios/TcpSocketClient.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import <netinet/in.h>
2+
#import <netinet/tcp.h>
23
#import <arpa/inet.h>
34
#import "TcpSocketClient.h"
45

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

128+
- (void)setNoDelay:(BOOL)noDelay
129+
{
130+
[_tcpSocket performBlock:^{
131+
int fd = [self->_tcpSocket socketFD];
132+
int on = noDelay ? 1 : 0;
133+
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)) == -1) {
134+
/* TODO: handle error */
135+
RCTLogWarn(@"setNoDelay caused an unexpected error");
136+
}
137+
}];
138+
}
139+
127140
- (BOOL)listen:(NSDictionary *)options error:(NSError **)error
128141
{
129142
if (_tcpSocket) {

ios/TcpSockets.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ - (TcpSocketClient *)createSocket:(nonnull NSNumber*)cId
116116
}
117117
}
118118

119+
RCT_EXPORT_METHOD(setNoDelay:(nonnull NSNumber*)cId noDelay:(BOOL)noDelay) {
120+
TcpSocketClient* client = [self findClient:cId];
121+
if (!client) return;
122+
123+
[client setNoDelay:noDelay];
124+
}
125+
119126
- (void)onConnect:(TcpSocketClient*) client
120127
{
121128
[self sendEventWithName:@"connect"

src/TcpSocket.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ export default class TcpSocket {
204204
return this;
205205
}
206206

207+
/**
208+
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
209+
*
210+
* Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.
211+
*
212+
* 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.
213+
*
214+
* @param {boolean} noDelay
215+
*/
216+
setNoDelay(noDelay = true) {
217+
Sockets.setNoDelay(this._id, noDelay);
218+
}
219+
207220
address() {
208221
return this._address;
209222
}

0 commit comments

Comments
 (0)