1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.transport.socket.apr;
21
22 import java.net.InetSocketAddress;
23
24 import org.apache.mina.core.RuntimeIoException;
25 import org.apache.mina.core.buffer.IoBuffer;
26 import org.apache.mina.core.service.DefaultTransportMetadata;
27 import org.apache.mina.core.service.IoProcessor;
28 import org.apache.mina.core.service.IoService;
29 import org.apache.mina.core.service.TransportMetadata;
30 import org.apache.mina.core.session.IoSession;
31 import org.apache.mina.core.session.IoSessionConfig;
32 import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
33 import org.apache.mina.transport.socket.SocketSessionConfig;
34 import org.apache.tomcat.jni.Socket;
35
36
37
38
39
40
41
42 class AprSocketSession extends AprSession {
43 static final TransportMetadata METADATA = new DefaultTransportMetadata("apr", "socket", false, true,
44 InetSocketAddress.class, SocketSessionConfig.class, IoBuffer.class);
45
46
47
48
49 AprSocketSession(IoService service, IoProcessor<AprSession> processor, long descriptor) throws Exception {
50 super(service, processor, descriptor);
51 config = new SessionConfigImpl();
52 this.config.setAll(service.getSessionConfig());
53 }
54
55
56
57
58 public SocketSessionConfig getConfig() {
59 return (SocketSessionConfig) config;
60 }
61
62
63
64
65 public TransportMetadata getTransportMetadata() {
66 return METADATA;
67 }
68
69
70
71
72
73 private class SessionConfigImpl extends AbstractSocketSessionConfig {
74
75
76
77 public boolean isKeepAlive() {
78 try {
79 return Socket.optGet(getDescriptor(), Socket.APR_SO_KEEPALIVE) == 1;
80 } catch (Exception e) {
81 throw new RuntimeIoException("Failed to get SO_KEEPALIVE.", e);
82 }
83 }
84
85
86
87
88 public void setKeepAlive(boolean on) {
89 Socket.optSet(getDescriptor(), Socket.APR_SO_KEEPALIVE, on ? 1 : 0);
90 }
91
92
93
94
95
96 public boolean isOobInline() {
97 return false;
98 }
99
100
101
102
103
104 public void setOobInline(boolean on) {
105 }
106
107
108
109
110 public boolean isReuseAddress() {
111 try {
112 return Socket.optGet(getDescriptor(), Socket.APR_SO_REUSEADDR) == 1;
113 } catch (Exception e) {
114 throw new RuntimeIoException("Failed to get SO_REUSEADDR.", e);
115 }
116 }
117
118
119
120
121 public void setReuseAddress(boolean on) {
122 Socket.optSet(getDescriptor(), Socket.APR_SO_REUSEADDR, on ? 1 : 0);
123 }
124
125
126
127
128 public int getSoLinger() {
129 try {
130 return Socket.optGet(getDescriptor(), Socket.APR_SO_LINGER);
131 } catch (Exception e) {
132 throw new RuntimeIoException("Failed to get SO_LINGER.", e);
133 }
134 }
135
136
137
138
139 public void setSoLinger(int linger) {
140
141 Socket.optSet(getDescriptor(), Socket.APR_SO_LINGER, linger);
142 }
143
144
145
146
147 public boolean isTcpNoDelay() {
148 try {
149 return Socket.optGet(getDescriptor(), Socket.APR_TCP_NODELAY) == 1;
150 } catch (Exception e) {
151 throw new RuntimeIoException("Failed to get TCP_NODELAY.", e);
152 }
153 }
154
155
156
157
158 public void setTcpNoDelay(boolean on) {
159 Socket.optSet(getDescriptor(), Socket.APR_TCP_NODELAY, on ? 1 : 0);
160 }
161
162
163
164
165 public int getTrafficClass() {
166 return 0;
167 }
168
169
170
171
172 public void setTrafficClass(int tc) {
173 }
174
175
176
177
178 public int getSendBufferSize() {
179 try {
180 return Socket.optGet(getDescriptor(), Socket.APR_SO_SNDBUF);
181 } catch (Exception e) {
182 throw new RuntimeException("APR Exception", e);
183 }
184 }
185
186
187
188
189 public void setSendBufferSize(int size) {
190 Socket.optSet(getDescriptor(), Socket.APR_SO_SNDBUF, size);
191 }
192
193
194
195
196 public int getReceiveBufferSize() {
197 try {
198 return Socket.optGet(getDescriptor(), Socket.APR_SO_RCVBUF);
199 } catch (Exception e) {
200 throw new RuntimeException("APR Exception", e);
201 }
202 }
203
204
205
206
207 public void setReceiveBufferSize(int size) {
208 Socket.optSet(getDescriptor(), Socket.APR_SO_RCVBUF, size);
209 }
210 }
211 }