mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
Update SocketChannel.java
This commit is contained in:
@@ -7,7 +7,6 @@ import io.netty.channel.Channel;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author luoyaogui
|
* @author luoyaogui
|
||||||
@@ -17,18 +16,13 @@ import java.nio.ByteBuffer;
|
|||||||
public class SocketChannel {
|
public class SocketChannel {
|
||||||
private Channel channel = null;
|
private Channel channel = null;
|
||||||
private Object lock = new Object();
|
private Object lock = new Object();
|
||||||
private ByteBuf cache = PooledByteBufAllocator.DEFAULT.directBuffer(1024*1024*5);//缓存大小
|
private ByteBuf cache = PooledByteBufAllocator.DEFAULT.directBuffer(1024*1024);//缓存大小
|
||||||
|
|
||||||
public Channel getChannel() {
|
public Channel getChannel() {
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
public void setChannel(Channel channel,boolean notify) {
|
public void setChannel(Channel channel) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
if(notify){//是否需要通知,主要时channel不可用时
|
|
||||||
synchronized(this){
|
|
||||||
notifyAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeCache(ByteBuf buf){
|
public void writeCache(ByteBuf buf){
|
||||||
@@ -36,9 +30,6 @@ public class SocketChannel {
|
|||||||
cache.discardReadBytes();//回收内存
|
cache.discardReadBytes();//回收内存
|
||||||
cache.writeBytes(buf);
|
cache.writeBytes(buf);
|
||||||
}
|
}
|
||||||
synchronized(this){
|
|
||||||
notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public void writeChannel(byte[]... buf) throws IOException {
|
public void writeChannel(byte[]... buf) throws IOException {
|
||||||
if(channel != null && channel.isWritable())
|
if(channel != null && channel.isWritable())
|
||||||
@@ -46,19 +37,22 @@ public class SocketChannel {
|
|||||||
else
|
else
|
||||||
throw new IOException("write failed ! please checking !");
|
throw new IOException("write failed ! please checking !");
|
||||||
}
|
}
|
||||||
public int read(ByteBuffer buffer) throws IOException {
|
public byte[] read(int readSize) throws IOException {
|
||||||
if(null == channel)
|
do{
|
||||||
throw new IOException("socket has Interrupted !");
|
if(readSize > cache.readableBytes()){
|
||||||
if(cache.readableBytes() < buffer.remaining()){
|
if(null == channel)
|
||||||
synchronized(this){
|
throw new IOException("socket has Interrupted !");
|
||||||
try {wait();} catch (InterruptedException e) {}
|
synchronized (this) {
|
||||||
|
try { wait(100); } catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
byte[] back = new byte[readSize];
|
||||||
|
synchronized (lock) {
|
||||||
|
cache.readBytes(back);
|
||||||
|
}
|
||||||
|
return back;
|
||||||
}
|
}
|
||||||
}else{
|
}while(true);
|
||||||
synchronized (lock){
|
|
||||||
cache.readBytes(buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return channel!=null?true:false;
|
return channel!=null?true:false;
|
||||||
|
|||||||
Reference in New Issue
Block a user