1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.socket.oio;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.io.PushbackInputStream;
21 import java.net.InetSocketAddress;
22 import java.net.Socket;
23 import java.net.SocketException;
24
25 import org.jboss.netty.channel.Channel;
26 import org.jboss.netty.channel.ChannelSink;
27 import org.jboss.netty.channel.ChannelException;
28 import org.jboss.netty.channel.ChannelFactory;
29 import org.jboss.netty.channel.ChannelPipeline;
30 import org.jboss.netty.channel.socket.DefaultSocketChannelConfig;
31 import org.jboss.netty.channel.socket.SocketChannel;
32 import org.jboss.netty.channel.socket.SocketChannelConfig;
33
34 abstract class OioSocketChannel extends AbstractOioChannel
35 implements SocketChannel {
36
37 final Socket socket;
38 private final SocketChannelConfig config;
39
40 OioSocketChannel(
41 Channel parent,
42 ChannelFactory factory,
43 ChannelPipeline pipeline,
44 ChannelSink sink,
45 Socket socket) {
46
47 super(parent, factory, pipeline, sink);
48
49 this.socket = socket;
50 try {
51 socket.setSoTimeout(1000);
52 } catch (SocketException e) {
53 throw new ChannelException(
54 "Failed to configure the OioSocketChannel socket timeout.", e);
55 }
56 config = new DefaultSocketChannelConfig(socket);
57 }
58
59 public SocketChannelConfig getConfig() {
60 return config;
61 }
62
63 abstract PushbackInputStream getInputStream();
64 abstract OutputStream getOutputStream();
65
66 @Override
67 boolean isSocketBound() {
68 return socket.isBound();
69 }
70
71 @Override
72 boolean isSocketConnected() {
73 return socket.isConnected();
74 }
75
76 @Override
77 InetSocketAddress getLocalSocketAddress() throws Exception {
78 return (InetSocketAddress) socket.getLocalSocketAddress();
79 }
80
81 @Override
82 InetSocketAddress getRemoteSocketAddress() throws Exception {
83 return (InetSocketAddress) socket.getRemoteSocketAddress();
84 }
85
86 @Override
87 void closeSocket() throws IOException {
88 socket.close();
89 }
90
91 @Override
92 boolean isSocketClosed() {
93 return socket.isClosed();
94 }
95 }