143 PlainHttpConnection(InetSocketAddress addr, HttpClientImpl client) { |
143 PlainHttpConnection(InetSocketAddress addr, HttpClientImpl client) { |
144 super(addr, client); |
144 super(addr, client); |
145 try { |
145 try { |
146 this.chan = SocketChannel.open(); |
146 this.chan = SocketChannel.open(); |
147 chan.configureBlocking(false); |
147 chan.configureBlocking(false); |
148 int bufsize = client.getReceiveBufferSize(); |
148 trySetReceiveBufferSize(client.getReceiveBufferSize()); |
149 if (!trySetReceiveBufferSize(bufsize)) { |
149 if (debug.on()) { |
150 trySetReceiveBufferSize(256*1024); |
150 int bufsize = getInitialBufferSize(); |
|
151 debug.log("Initial receive buffer size is: %d", bufsize); |
151 } |
152 } |
152 chan.setOption(StandardSocketOptions.TCP_NODELAY, true); |
153 chan.setOption(StandardSocketOptions.TCP_NODELAY, true); |
153 // wrap the connected channel in a Tube for async reading and writing |
154 // wrap the channel in a Tube for async reading and writing |
154 tube = new SocketTube(client(), chan, Utils::getBuffer); |
155 tube = new SocketTube(client(), chan, Utils::getBuffer); |
155 } catch (IOException e) { |
156 } catch (IOException e) { |
156 throw new InternalError(e); |
157 throw new InternalError(e); |
157 } |
158 } |
158 } |
159 } |
159 |
160 |
160 private boolean trySetReceiveBufferSize(int bufsize) { |
161 private int getInitialBufferSize() { |
161 try { |
162 try { |
162 chan.setOption(StandardSocketOptions.SO_RCVBUF, bufsize); |
163 return chan.getOption(StandardSocketOptions.SO_RCVBUF); |
|
164 } catch(IOException x) { |
163 if (debug.on()) |
165 if (debug.on()) |
164 debug.log("Receive buffer size is %s", |
166 debug.log("Failed to get initial receive buffer size on %s", chan); |
165 chan.getOption(StandardSocketOptions.SO_RCVBUF)); |
167 } |
166 return true; |
168 return 0; |
|
169 } |
|
170 |
|
171 private void trySetReceiveBufferSize(int bufsize) { |
|
172 try { |
|
173 if (bufsize > 0) { |
|
174 chan.setOption(StandardSocketOptions.SO_RCVBUF, bufsize); |
|
175 } |
167 } catch(IOException x) { |
176 } catch(IOException x) { |
168 if (debug.on()) |
177 if (debug.on()) |
169 debug.log("Failed to set receive buffer size to %d on %s", |
178 debug.log("Failed to set receive buffer size to %d on %s", |
170 bufsize, chan); |
179 bufsize, chan); |
171 } |
180 } |
172 return false; |
|
173 } |
181 } |
174 |
182 |
175 @Override |
183 @Override |
176 HttpPublisher publisher() { return writePublisher; } |
184 HttpPublisher publisher() { return writePublisher; } |
177 |
185 |