use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class TestDuplicatedHeapByteBuf method testHeap.
@Test
public void testHeap() {
ByteBuf buf = ByteBuf.direct(16);
buf.writeBytes(data.getBytes());
ByteBuf buf2 = buf.duplicate();
v(buf2);
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class TestDuplicatedHeapByteBuf method testDirectP.
@Test
public void testDirectP() throws Exception {
ByteBufAllocator a = TestAllocUtil.direct();
ByteBuf buf = a.allocate(16);
buf.writeBytes(data.getBytes());
ByteBuf buf2 = buf.duplicate();
v(buf2);
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class TestDuplicatedHeapByteBuf method testHeapP.
@Test
public void testHeapP() throws Exception {
ByteBufAllocator a = TestAllocUtil.heap();
ByteBuf buf = a.allocate(16);
buf.writeBytes(data.getBytes());
ByteBuf buf2 = buf.duplicate();
v(buf2);
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class ClientHttpCodec method encode.
@Override
public ByteBuf encode(Channel ch, Frame frame) {
ClientHttpFrame f = (ClientHttpFrame) frame;
Object content = f.getContent();
ByteBuf contentBuf = null;
byte[] contentArray = null;
boolean isArray = false;
int write_size = 0;
if (content instanceof ByteBuf) {
contentBuf = ((ByteBuf) content);
write_size = contentBuf.writeIndex();
} else if (content instanceof byte[]) {
isArray = true;
contentArray = (byte[]) content;
write_size = contentArray.length;
}
byte[] byte32 = FastThreadLocal.get().getBytes32();
byte[] url_bytes = getRequestURI(f).getBytes();
byte[] mtd_bytes = f.getMethod().getBytes();
int len_idx = Util.valueOf(write_size, byte32);
int len_len = 32 - len_idx;
int len = mtd_bytes.length + 1 + url_bytes.length + PROTOCOL.length + len_len + 2;
int header_size = 0;
List<byte[]> bytes_array = (List<byte[]>) FastThreadLocal.get().getList();
IntObjectMap<String> headers = f.getRequestHeaders();
if (headers != null) {
headers.remove(HttpHeader.Content_Length.getId());
for (headers.scan(); headers.hasNext(); ) {
byte[] k = HttpHeader.get(headers.getKey()).getBytes();
byte[] v = headers.getValue().getBytes();
if (v == null) {
continue;
}
header_size++;
bytes_array.add(k);
bytes_array.add(v);
len += 4;
len += k.length;
len += v.length;
}
}
len += 2;
if (isArray) {
len += write_size;
}
ByteBuf buf = ch.alloc().allocate(len);
buf.writeBytes(mtd_bytes);
buf.writeByte(SPACE);
buf.writeBytes(url_bytes);
if (f.isGet()) {
buf.writeBytes(PROTOCOL);
} else {
buf.writeBytes(PROTOCOL_CL);
buf.writeBytes(byte32, len_idx, len_len);
buf.writeByte(R);
buf.writeByte(N);
}
int j = 0;
for (int i = 0; i < header_size; i++) {
buf.writeBytes(bytes_array.get(j++));
buf.writeByte((byte) ':');
buf.writeByte(SPACE);
buf.writeBytes(bytes_array.get(j++));
buf.writeByte(R);
buf.writeByte(N);
}
buf.writeByte(R);
buf.writeByte(N);
if (write_size > 0) {
if (isArray) {
buf.writeBytes(contentArray);
} else {
ch.write(buf);
ch.write(contentBuf);
return null;
}
}
return buf;
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class WebSocketMsgAdapter method doLoop.
@Override
protected void doLoop() throws InterruptedException {
Msg msg = (Msg) msgs.poll(16, TimeUnit.MILLISECONDS);
if (msg == null) {
return;
}
if (msg.ch != null) {
WebSocketFrame f = new WebSocketFrame();
f.setString(msg.msg, msg.ch);
try {
msg.ch.writeAndFlush(f);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
} else {
if (!clientMap.isEmpty()) {
synchronized (this) {
Client client = clientMap.values().iterator().next();
Channel ch = client.channel;
WebSocketFrame f = new WebSocketFrame();
byte[] data = msg.msg.getBytes();
f.setContent(data);
try {
ByteBuf buf = ch.getCodec().encode(ch, f);
ChannelManagerListener.broadcast(buf, channelMap.values());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
}
}
Aggregations