1 /* 2 * Copyright (c) 2011-2017 Contributors to the Eclipse Foundation 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the Eclipse Public License 2.0 which is available at 6 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 7 * which is available at https://www.apache.org/licenses/LICENSE-2.0. 8 * 9 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 10 */ 11 12 module hunt.net.NetworkOptions; 13 14 /** 15 * @author <a href="http://tfox.org">Tim Fox</a> 16 */ 17 class NetworkOptions { 18 19 /** 20 * The default value of TCP send buffer size 21 */ 22 enum int DEFAULT_SEND_BUFFER_SIZE = -1; 23 24 /** 25 * The default value of TCP receive buffer size 26 */ 27 enum int DEFAULT_RECEIVE_BUFFER_SIZE = -1; 28 29 30 /** 31 * The default value of decoder buffer size 32 */ 33 enum int DEFAULT_DECODER_BUFFER_SIZE = -1; 34 35 /** 36 * The default value of encoder buffer size 37 */ 38 enum int DEFAULT_ENCODER_BUFFER_SIZE = -1; 39 40 /** 41 * The default value of traffic class 42 */ 43 enum int DEFAULT_TRAFFIC_CLASS = -1; 44 45 /** 46 * The default value of reuse address 47 */ 48 enum bool DEFAULT_REUSE_ADDRESS = true; 49 50 /** 51 * The default value of reuse port 52 */ 53 enum bool DEFAULT_REUSE_PORT = false; 54 55 /** 56 * The default log enabled = false 57 */ 58 enum bool DEFAULT_LOG_ENABLED = false; 59 60 private int sendBufferSize; 61 private int receiveBufferSize; 62 private int encoderBufferSize; 63 private int decoderBufferSize; 64 private int trafficClass; 65 private bool reuseAddress; 66 private bool logActivity; 67 private bool reusePort; 68 69 /** 70 * Default constructor 71 */ 72 this() { 73 sendBufferSize = DEFAULT_SEND_BUFFER_SIZE; 74 receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE; 75 encoderBufferSize = DEFAULT_ENCODER_BUFFER_SIZE; 76 decoderBufferSize = DEFAULT_DECODER_BUFFER_SIZE; 77 reuseAddress = DEFAULT_REUSE_ADDRESS; 78 trafficClass = DEFAULT_TRAFFIC_CLASS; 79 logActivity = DEFAULT_LOG_ENABLED; 80 reusePort = DEFAULT_REUSE_PORT; 81 } 82 83 /** 84 * Copy constructor 85 * 86 * @param other the options to copy 87 */ 88 this(NetworkOptions other) { 89 this.sendBufferSize = other.getSendBufferSize(); 90 this.receiveBufferSize = other.getReceiveBufferSize(); 91 this.encoderBufferSize = other.getEncoderBufferSize(); 92 this.decoderBufferSize = other.getDecoderBufferSize(); 93 this.reuseAddress = other.isReuseAddress(); 94 this.reusePort = other.isReusePort(); 95 this.trafficClass = other.getTrafficClass(); 96 this.logActivity = other.logActivity; 97 } 98 99 /** 100 * Return the TCP send buffer size, in bytes. 101 * 102 * @return the send buffer size 103 */ 104 int getSendBufferSize() { 105 return sendBufferSize; 106 } 107 108 /** 109 * Set the TCP send buffer size 110 * 111 * @param sendBufferSize the buffers size, in bytes 112 * @return a reference to this, so the API can be used fluently 113 */ 114 NetworkOptions setSendBufferSize(int sendBufferSize) { 115 assert(sendBufferSize > 0 || sendBufferSize == DEFAULT_SEND_BUFFER_SIZE, 116 "sendBufferSize must be > 0"); 117 this.sendBufferSize = sendBufferSize; 118 return this; 119 } 120 121 /** 122 * Return the TCP receive buffer size, in bytes 123 * 124 * @return the receive buffer size 125 */ 126 int getReceiveBufferSize() { 127 return receiveBufferSize; 128 } 129 130 /** 131 * Set the TCP receive buffer size 132 * 133 * @param receiveBufferSize the buffers size, in bytes 134 * @return a reference to this, so the API can be used fluently 135 */ 136 NetworkOptions setReceiveBufferSize(int receiveBufferSize) { 137 assert(receiveBufferSize > 0 || receiveBufferSize == DEFAULT_RECEIVE_BUFFER_SIZE, 138 "receiveBufferSize must be > 0"); 139 this.receiveBufferSize = receiveBufferSize; 140 return this; 141 } 142 143 144 /** 145 * Return the encoder buffer size, in bytes 146 * 147 * @return the receive buffer size 148 */ 149 int getEncoderBufferSize() { 150 return encoderBufferSize; 151 } 152 153 /** 154 * Set the encoder buffer size 155 * 156 * @param size the buffers size, in bytes 157 * @return a reference to this, so the API can be used fluently 158 */ 159 NetworkOptions setEncoderBufferSize(int size) { 160 assert(size > 0 || size == DEFAULT_ENCODER_BUFFER_SIZE, 161 "encoderBufferSize must be > 0"); 162 this.encoderBufferSize = size; 163 return this; 164 } 165 166 /** 167 * Return the decoder buffer size, in bytes 168 * 169 * @return the receive buffer size 170 */ 171 int getDecoderBufferSize() { 172 return decoderBufferSize; 173 } 174 175 /** 176 * Set the decoder buffer size 177 * 178 * @param size the buffers size, in bytes 179 * @return a reference to this, so the API can be used fluently 180 */ 181 NetworkOptions setDecoderBufferSize(int size) { 182 assert(size > 0 || size == DEFAULT_DECODER_BUFFER_SIZE, 183 "decoderBufferSize must be > 0"); 184 this.decoderBufferSize = size; 185 return this; 186 } 187 188 /** 189 * @return the value of traffic class 190 */ 191 int getTrafficClass() { 192 return trafficClass; 193 } 194 195 /** 196 * Set the value of traffic class 197 * 198 * @param trafficClass the value of traffic class 199 * @return a reference to this, so the API can be used fluently 200 */ 201 NetworkOptions setTrafficClass(int trafficClass) { 202 assert(trafficClass > DEFAULT_TRAFFIC_CLASS && trafficClass <= 255, 203 "trafficClass tc must be 0 <= tc <= 255"); 204 this.trafficClass = trafficClass; 205 return this; 206 } 207 208 /** 209 * @return true when network activity logging is enabled 210 */ 211 bool getLogActivity() { 212 return logActivity; 213 } 214 215 /** 216 * Set to true to enabled network activity logging: Netty's pipeline is configured for logging on Netty's logger. 217 * 218 * @param logActivity true for logging the network activity 219 * @return a reference to this, so the API can be used fluently 220 */ 221 NetworkOptions setLogActivity(bool logActivity) { 222 this.logActivity = logActivity; 223 return this; 224 } 225 226 /** 227 * @return the value of reuse address 228 */ 229 bool isReuseAddress() { 230 return reuseAddress; 231 } 232 233 /** 234 * Set the value of reuse address 235 * @param reuseAddress the value of reuse address 236 * @return a reference to this, so the API can be used fluently 237 */ 238 NetworkOptions setReuseAddress(bool reuseAddress) { 239 this.reuseAddress = reuseAddress; 240 return this; 241 } 242 243 /** 244 * @return the value of reuse address - only supported by native transports 245 */ 246 bool isReusePort() { 247 return reusePort; 248 } 249 250 /** 251 * Set the value of reuse port. 252 * <p/> 253 * This is only supported by native transports. 254 * 255 * @param reusePort the value of reuse port 256 * @return a reference to this, so the API can be used fluently 257 */ 258 NetworkOptions setReusePort(bool reusePort) { 259 this.reusePort = reusePort; 260 return this; 261 } 262 263 override bool opEquals(Object o) { 264 if (this is o) 265 return true; 266 267 NetworkOptions that = cast(NetworkOptions) o; 268 if (that is null) 269 return false; 270 271 if (receiveBufferSize != that.receiveBufferSize) 272 return false; 273 if (reuseAddress != that.reuseAddress) 274 return false; 275 if (reusePort != that.reusePort) 276 return false; 277 if (sendBufferSize != that.sendBufferSize) 278 return false; 279 if (trafficClass != that.trafficClass) 280 return false; 281 282 return true; 283 } 284 285 override size_t toHash() @trusted nothrow { 286 size_t result = sendBufferSize; 287 result = 31 * result + receiveBufferSize; 288 result = 31 * result + trafficClass; 289 result = 31 * result + (reuseAddress ? 1 : 0); 290 result = 31 * result + (reusePort ? 1 : 0); 291 return result; 292 } 293 }