|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "encoding/binary" |
6 | 5 | "fmt" |
7 | 6 | "net" |
@@ -52,6 +51,8 @@ func configureTransport( |
52 | 51 | // Start handling network packets using UDP server |
53 | 52 | configureUDPServerTransport(adj, transp, config) |
54 | 53 |
|
| 54 | + // Start the application-level TCP keep-alive (empty order with id 1) |
| 55 | + go transp.HandleKeepAlive(time.Duration(config.TCP.KeepAliveIntervalMs) * time.Millisecond) |
55 | 56 | } |
56 | 57 |
|
57 | 58 | func configureTCPClientTransport( |
@@ -86,11 +87,6 @@ func configureTCPClientTransport( |
86 | 87 | clientConfig.Timeout = time.Duration(config.TCP.ConnectionTimeout) * time.Millisecond |
87 | 88 | } |
88 | 89 |
|
89 | | - // Apply custom keep-alive if specified |
90 | | - if config.TCP.KeepAlive > 0 { |
91 | | - clientConfig.KeepAlive = time.Duration(config.TCP.KeepAlive) * time.Millisecond |
92 | | - } |
93 | | - |
94 | 90 | // Apply custom backoff parameters |
95 | 91 | if config.TCP.BackoffMinMs > 0 || config.TCP.BackoffMaxMs > 0 || config.TCP.BackoffMultiplier > 0 { |
96 | 92 | minBackoff := 100 * time.Millisecond // default |
@@ -118,18 +114,12 @@ func configureTCPClientTransport( |
118 | 114 | } |
119 | 115 | } |
120 | 116 |
|
121 | | -// configureTCPServerTransport starts the TCP server handler using a ListenConfig with KeepAlive. |
| 117 | +// configureTCPServerTransport starts the TCP server handler. |
122 | 118 | func configureTCPServerTransport( |
123 | 119 | adj adj_module.ADJ, |
124 | 120 | transp *transport.Transport, |
125 | 121 | ) { |
126 | | - go transp.HandleServer(tcp.ServerConfig{ |
127 | | - ListenConfig: net.ListenConfig{ |
128 | | - KeepAlive: time.Second, |
129 | | - }, |
130 | | - Context: context.TODO(), |
131 | | - }, fmt.Sprintf("%s:%d", adj.Info.Addresses[BACKEND], adj.Info.Ports[TcpServer])) |
132 | | - |
| 122 | + go transp.HandleServer(tcp.NewServerConfig(), fmt.Sprintf("%s:%d", adj.Info.Addresses[BACKEND], adj.Info.Ports[TcpServer])) |
133 | 123 | } |
134 | 124 |
|
135 | 125 | // configureUDPServerTransport creates and starts the UDP server then delegates handling to transport. |
@@ -246,6 +236,15 @@ func getTransportDecEnc(info adj_module.Info, podData pod_data.PodData) (*presen |
246 | 236 | encoder.SetPacketEncoder(id, dataEncoder) |
247 | 237 | } |
248 | 238 |
|
| 239 | + // The TCP keep-alive order is an empty packet, so give it an empty |
| 240 | + // descriptor unless the ADJ already defines packet id 1 |
| 241 | + if !common.Contains(ids, transport.KeepAliveId) { |
| 242 | + dataDecoder.SetDescriptor(transport.KeepAliveId, data.Descriptor{}) |
| 243 | + dataEncoder.SetDescriptor(transport.KeepAliveId, data.Descriptor{}) |
| 244 | + decoder.SetPacketDecoder(transport.KeepAliveId, dataDecoder) |
| 245 | + encoder.SetPacketEncoder(transport.KeepAliveId, dataEncoder) |
| 246 | + } |
| 247 | + |
249 | 248 | // TODO Solve this foking mess, I have tried... |
250 | 249 | stateOrdersDecoder := order.NewDecoder(binary.LittleEndian) |
251 | 250 | stateOrdersDecoder.SetActionId(abstraction.PacketId(info.MessageIds[AddStateOrder]), stateOrdersDecoder.DecodeAdd) |
|
0 commit comments