|
@@ -14,59 +14,69 @@ import java.net.SocketAddress;
|
|
|
* @author luoyaogui
|
|
|
*/
|
|
|
public class SocketChannel {
|
|
|
- private Channel channel = null;
|
|
|
- private Object lock = new Object();
|
|
|
- private ByteBuf cache = PooledByteBufAllocator.DEFAULT.directBuffer(1024*1024);//缓存大小
|
|
|
|
|
|
- public Channel getChannel() {
|
|
|
- return channel;
|
|
|
- }
|
|
|
- public void setChannel(Channel channel) {
|
|
|
- this.channel = channel;
|
|
|
- }
|
|
|
+ private Channel channel = null;
|
|
|
+ private Object lock = new Object();
|
|
|
+ private ByteBuf cache = PooledByteBufAllocator.DEFAULT.directBuffer(1024 * 1024); // 缓存大小
|
|
|
|
|
|
- public void writeCache(ByteBuf buf){
|
|
|
- synchronized (lock) {
|
|
|
- cache.discardReadBytes();//回收内存
|
|
|
- cache.writeBytes(buf);
|
|
|
- }
|
|
|
- }
|
|
|
- public void writeChannel(byte[]... buf) throws IOException {
|
|
|
- if(channel != null && channel.isWritable())
|
|
|
- channel.writeAndFlush(Unpooled.copiedBuffer(buf));
|
|
|
- else
|
|
|
- throw new IOException("write failed ! please checking !");
|
|
|
- }
|
|
|
- public byte[] read(int readSize) throws IOException {
|
|
|
- do{
|
|
|
- if(readSize > cache.readableBytes()){
|
|
|
- if(null == channel)
|
|
|
- throw new IOException("socket has Interrupted !");
|
|
|
- synchronized (this) {
|
|
|
- try { wait(100); } catch (InterruptedException e) {}
|
|
|
- }
|
|
|
- } else {
|
|
|
- byte[] back = new byte[readSize];
|
|
|
- synchronized (lock) {
|
|
|
- cache.readBytes(back);
|
|
|
- }
|
|
|
- return back;
|
|
|
- }
|
|
|
- }while(true);
|
|
|
- }
|
|
|
- public boolean isConnected() {
|
|
|
- return channel!=null?true:false;
|
|
|
- }
|
|
|
- public SocketAddress getRemoteSocketAddress(){
|
|
|
- return channel!=null?channel.remoteAddress():null;
|
|
|
- }
|
|
|
- public void close(){
|
|
|
- if(channel != null){
|
|
|
- channel.close();
|
|
|
- }
|
|
|
- channel = null;
|
|
|
- cache.discardReadBytes();//回收已占用的内存
|
|
|
- cache.release();//释放整个内存
|
|
|
- cache = null;
|
|
|
- }
|
|
|
+ public Channel getChannel() {
|
|
|
+ return channel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setChannel(Channel channel) {
|
|
|
+ this.channel = channel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void writeCache(ByteBuf buf) {
|
|
|
+ synchronized (lock) {
|
|
|
+ cache.discardReadBytes();// 回收内存
|
|
|
+ cache.writeBytes(buf);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void writeChannel(byte[]... buf) throws IOException {
|
|
|
+ if (channel != null && channel.isWritable()) channel.writeAndFlush(Unpooled.copiedBuffer(buf));
|
|
|
+ else throw new IOException("write failed ! please checking !");
|
|
|
+ }
|
|
|
+
|
|
|
+ public byte[] read(int readSize) throws IOException {
|
|
|
+ do {
|
|
|
+ if (readSize > cache.readableBytes()) {
|
|
|
+ if (null == channel) {
|
|
|
+ throw new IOException("socket has Interrupted !");
|
|
|
+ }
|
|
|
+ synchronized (this) {
|
|
|
+ try {
|
|
|
+ wait(100);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new IOException("socket has Interrupted !");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ byte[] back = new byte[readSize];
|
|
|
+ synchronized (lock) {
|
|
|
+ cache.readBytes(back);
|
|
|
+ }
|
|
|
+ return back;
|
|
|
+ }
|
|
|
+ } while (true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isConnected() {
|
|
|
+ return channel != null ? true : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public SocketAddress getRemoteSocketAddress() {
|
|
|
+ return channel != null ? channel.remoteAddress() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close() {
|
|
|
+ if (channel != null) {
|
|
|
+ channel.close();
|
|
|
+ }
|
|
|
+ channel = null;
|
|
|
+ cache.discardReadBytes();// 回收已占用的内存
|
|
|
+ cache.release();// 释放整个内存
|
|
|
+ cache = null;
|
|
|
+ }
|
|
|
}
|