1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.kqueue;
17
18 import io.netty.buffer.ByteBufAllocator;
19 import io.netty.channel.ChannelException;
20 import io.netty.channel.ChannelOption;
21 import io.netty.channel.FixedRecvByteBufAllocator;
22 import io.netty.channel.MessageSizeEstimator;
23 import io.netty.channel.RecvByteBufAllocator;
24 import io.netty.channel.WriteBufferWaterMark;
25 import io.netty.channel.unix.DomainDatagramChannelConfig;
26
27 import java.io.IOException;
28 import java.util.Map;
29
30 import static io.netty.channel.ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION;
31 import static io.netty.channel.ChannelOption.SO_SNDBUF;
32
33 public final class KQueueDomainDatagramChannelConfig
34 extends KQueueChannelConfig implements DomainDatagramChannelConfig {
35
36 private boolean activeOnOpen;
37
38 KQueueDomainDatagramChannelConfig(KQueueDomainDatagramChannel channel) {
39 super(channel, new FixedRecvByteBufAllocator(2048));
40 }
41
42 @Override
43 @SuppressWarnings("deprecation")
44 public Map<ChannelOption<?>, Object> getOptions() {
45 return getOptions(super.getOptions(),
46 DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, SO_SNDBUF);
47 }
48
49 @Override
50 @SuppressWarnings({"unchecked", "deprecation"})
51 public <T> T getOption(ChannelOption<T> option) {
52 if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
53 return (T) Boolean.valueOf(activeOnOpen);
54 }
55 if (option == SO_SNDBUF) {
56 return (T) Integer.valueOf(getSendBufferSize());
57 }
58 return super.getOption(option);
59 }
60
61 @Override
62 @SuppressWarnings("deprecation")
63 public <T> boolean setOption(ChannelOption<T> option, T value) {
64 validate(option, value);
65
66 if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
67 setActiveOnOpen((Boolean) value);
68 } else if (option == SO_SNDBUF) {
69 setSendBufferSize((Integer) value);
70 } else {
71 return super.setOption(option, value);
72 }
73
74 return true;
75 }
76
77 private void setActiveOnOpen(boolean activeOnOpen) {
78 if (channel.isRegistered()) {
79 throw new IllegalStateException("Can only changed before channel was registered");
80 }
81 this.activeOnOpen = activeOnOpen;
82 }
83
84 boolean getActiveOnOpen() {
85 return activeOnOpen;
86 }
87
88 @Override
89 public KQueueDomainDatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
90 super.setAllocator(allocator);
91 return this;
92 }
93
94 @Override
95 public KQueueDomainDatagramChannelConfig setAutoClose(boolean autoClose) {
96 super.setAutoClose(autoClose);
97 return this;
98 }
99
100 @Override
101 public KQueueDomainDatagramChannelConfig setAutoRead(boolean autoRead) {
102 super.setAutoRead(autoRead);
103 return this;
104 }
105
106 @Override
107 public KQueueDomainDatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
108 super.setConnectTimeoutMillis(connectTimeoutMillis);
109 return this;
110 }
111
112 @Override
113 @Deprecated
114 public KQueueDomainDatagramChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
115 super.setMaxMessagesPerRead(maxMessagesPerRead);
116 return this;
117 }
118
119 @Override
120 public KQueueDomainDatagramChannelConfig setMaxMessagesPerWrite(int maxMessagesPerWrite) {
121 super.setMaxMessagesPerWrite(maxMessagesPerWrite);
122 return this;
123 }
124
125 @Override
126 public KQueueDomainDatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
127 super.setMessageSizeEstimator(estimator);
128 return this;
129 }
130
131 @Override
132 public KQueueDomainDatagramChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
133 super.setRcvAllocTransportProvidesGuess(transportProvidesGuess);
134 return this;
135 }
136
137 @Override
138 public KQueueDomainDatagramChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
139 super.setRecvByteBufAllocator(allocator);
140 return this;
141 }
142
143 @Override
144 public KQueueDomainDatagramChannelConfig setSendBufferSize(int sendBufferSize) {
145 try {
146 ((KQueueDomainDatagramChannel) channel).socket.setSendBufferSize(sendBufferSize);
147 return this;
148 } catch (IOException e) {
149 throw new ChannelException(e);
150 }
151 }
152
153 @Override
154 public int getSendBufferSize() {
155 try {
156 return ((KQueueDomainDatagramChannel) channel).socket.getSendBufferSize();
157 } catch (IOException e) {
158 throw new ChannelException(e);
159 }
160 }
161
162 @Override
163 public KQueueDomainDatagramChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
164 super.setWriteBufferWaterMark(writeBufferWaterMark);
165 return this;
166 }
167
168 @Override
169 public KQueueDomainDatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
170 super.setWriteSpinCount(writeSpinCount);
171 return this;
172 }
173 }