use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method read_plain_remain.
protected void read_plain_remain(ByteBuf dst) {
ByteBuf remain = this.plain_remain_buf;
if (remain != null) {
dst.writeBytes(remain);
remain.release();
this.plain_remain_buf = null;
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class Channel method release_wb_array.
private void release_wb_array() {
final ByteBuf[] c_wbs = this.current_wb_array;
final int max_len = c_wbs.length;
// 对所有不为空的ByteBuf release
for (int i = 0; i < max_len; i++) {
ByteBuf buf = c_wbs[i];
if (buf == null) {
break;
}
buf.release();
c_wbs[i] = null;
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class ChannelManagerListener method broadcast.
public static void broadcast(Frame frame, Collection<Channel> chs) throws Exception {
if (chs.size() == 0) {
frame.release();
return;
}
Channel ch = chs.iterator().next();
if (ch == null) {
frame.release();
return;
}
try {
ByteBuf buf = ch.getCodec().encode(ch, frame);
broadcast(buf, chs);
} finally {
frame.release();
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class HttpCodec method encode.
@Override
public ByteBuf encode(final Channel ch, Frame frame) {
HttpFrame f = (HttpFrame) frame;
FastThreadLocal l = FastThreadLocal.get();
List<byte[]> bytes_array = (List<byte[]>) l.getList();
Object content = f.getContent();
ByteBuf content_buf = null;
byte[] content_array = null;
byte[] head_bytes = f.getStatus().getLine();
byte[] conn_bytes = f.getConnection().getLine();
byte[] type_bytes = f.getContentType().getLine();
byte[] date_bytes = f.getDate();
boolean is_array = false;
int write_size = 0;
if (content instanceof ByteBuf) {
content_buf = (ByteBuf) content;
write_size = content_buf.readableBytes();
} else if (content instanceof byte[]) {
is_array = true;
content_array = (byte[]) content;
write_size = content_array.length;
}
byte[] cl_len_bytes;
int cl_len;
if (write_size < 1024) {
cl_len_bytes = cl_bytes[write_size];
cl_len = cl_len_bytes.length;
} else {
cl_len_bytes = cl_buf.array();
int tmp_len = cl_buf.limit();
int len_idx = Util.valueOf(write_size, cl_len_bytes);
int num_len = cl_len_bytes.length - len_idx;
System.arraycopy(cl_len_bytes, len_idx, cl_len_bytes, tmp_len, num_len);
cl_len = tmp_len + num_len;
}
int hlen = head_bytes.length;
int tlen = type_bytes == null ? 0 : type_bytes.length;
int clen = conn_bytes == null ? 0 : conn_bytes.length;
int dlen = date_bytes == null ? 0 : date_bytes.length;
int len = hlen + cl_len + dlen + 2 + clen + tlen;
int h_size = 0;
IntObjectMap<byte[]> headers = f.getResponseHeaders();
if (headers != null) {
for (headers.scan(); headers.hasNext(); ) {
byte[] k = HttpHeader.get(headers.getKey()).getBytes();
byte[] v = headers.getValue();
h_size++;
bytes_array.add(k);
bytes_array.add(v);
len += 4;
len += k.length;
len += v.length;
}
}
len += 2;
if (write_size <= cthreshold) {
len += write_size;
}
ByteBuf buf;
if (Develop.BUF_DEBUG) {
buf = ch.alloc().allocate(1);
} else {
buf = ch.alloc().allocate(len);
}
buf.writeBytes(head_bytes);
buf.writeBytes(cl_len_bytes, 0, cl_len);
if (conn_bytes != null) {
buf.writeBytes(conn_bytes);
}
if (type_bytes != null) {
buf.writeBytes(type_bytes);
}
if (date_bytes != null) {
buf.writeBytes(date_bytes);
}
buf.writeByte(R);
buf.writeByte(N);
if (h_size > 0) {
put_headers(buf, bytes_array, h_size);
}
buf.writeByte(R);
buf.writeByte(N);
if (write_size > cthreshold) {
ch.write(buf);
if (is_array) {
ch.write(ByteBuf.wrap(content_array));
} else {
ch.write(content_buf);
}
return null;
} else {
if (write_size > 0) {
if (is_array) {
buf.writeBytes(content_array);
} else {
buf.writeBytes(content_buf);
}
}
return buf;
}
}
use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.
the class HttpFrame method updateWebSocketProtocol.
public boolean updateWebSocketProtocol(final Channel ch) throws Exception {
String Sec_WebSocket_Key_Value = getRequestHeader(Sec_WebSocket_Key);
if (!Util.isNullOrBlank(Sec_WebSocket_Key_Value)) {
// FIXME 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 必须这个值?
String Sec_WebSocket_Key_Magic = Sec_WebSocket_Key_Value + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] key_array = Cryptos.SHA1(Sec_WebSocket_Key_Magic);
String acceptKey = Cryptos.base64_en(key_array);
setStatus(HttpStatus.C101);
setConnection(HttpConnection.UPGRADE);
setResponseHeader(Upgrade, HttpStatic.websocket_bytes);
setResponseHeader(Sec_WebSocket_Accept, acceptKey.getBytes());
((HttpAttachment) ch.getAttachment()).setWebSocketFrameName(getRequestURL());
ByteBuf buf = ch.encode(this);
ch.setCodec(WebSocketCodec.PROTOCOL_ID);
ch.writeAndFlush(buf);
return true;
}
return false;
}
Aggregations