📄 attachment.java
字号:
/*
* Created on 2005-4-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.swing.server.common;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* @author vampire_a
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Attachment {
/** number of bytes in the network event header */
public static final int HEADER_SIZE = 9;
public static final String EVENT_END = "end";
public static final int SOCKET = 0;
public static final int POST = 1;
public static final int GET = 2;
public static final int OTHER = 3;
/** log4j logger */
private static Logger log = Logger.getLogger("Attachment");
/** unique id for this client */
String clientId;
/** 请求类型
* SOCKET : 0
* POST : 1
* GET : 2
* OTHER : 3
*/
public int type;
/** hash code of the GameName this event should be routed to */
public String gameCode;
/** do we have a full header yet */
private boolean gotHeader;
/** buffer used to hold data as it's read from the channel */
public ByteBuffer readBuff;
public int payloadSize;
/** temporary storage of the payload before it is read into an event */
public byte payload[];
public Map httpHeader = new HashMap();
public StringBuffer sb = new StringBuffer();
/**
* constructor. initiales the payload array and the read buffer
*/
public Attachment() {
payload = new byte[Globals.MAX_IN_EVENT_SIZE];
readBuff = ByteBuffer.allocateDirect(Globals.NET_BUFFER_SIZE);
// readBuff.order(ByteOrder.LITTLE_ENDIAN);
}
/**
* checks if a full event has been read
* 这个函数里面的两个函数要仔细考虑怎么写,很关键,要区分是Socket连接还是HTTP连接,还要处理是不是结束一个请求,并把数据读出来
*
* @return true if the event is ready, otherwise false
*/
public boolean eventReady() throws IllegalArgumentException {
if(checkHeader() && checkPayload())
return true;
else
return false;
}
/**
* reset the attachment to prepare for reading the next event
*/
public void reset() {
gotHeader = false;
}
/**
* 区分属于哪种类型的上行数据,然后根据读取游戏代号和数据包长度(不同类型数据包读取的方法不同)
*
* @return true if the header is fully available, otherwise false
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 数据包格式(SOCKET) *
* 包类型(SOCK) * 游戏代码 * 数据包长度 * 数据 *
* 0 - 3 4 5 - 8 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 数据包格式(POST) *
* 包类型(POST) * HTTP包头数据(包括游戏代码,数据包长度) * *
* 0 - 3 以/r/n/r/n结尾 *
* 数据 * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 其他请求(OTHER) *
* 包类型 * HTTP包头数据 * *
* 0 - 3 以/r/n/r/n结尾 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private boolean checkHeader() throws IllegalArgumentException {
if (gotHeader)
return true;
if (readBuff.remaining() >= HEADER_SIZE) {
byte[] head = new byte[4];
readBuff.get(head, 0, 4);
String header = new String(head);
// 用HTTP协议访问服务器端的手机请求
if (header.equals ("POST")) {
this.type = POST;
HTTPUtils.filter(readBuff, httpHeader, sb);
String length = (String) httpHeader.get("Content-Length");
if (length != null)
payloadSize = Integer.parseInt (length);
gameCode = (String) httpHeader.get("Code");
}
// 用Socket协议访问服务器的手机请求
else if (header.equals ("SOCK")) {
this.type = SOCKET;
gameCode = String.valueOf((char)readBuff.get());
payloadSize = readBuff.getShort();
log.debug("Payload Size = " + payloadSize);
}
// 用浏览器访问的
else if (header.equals("GET ")) {
this.type = GET;
HTTPUtils.filter(readBuff, httpHeader, sb);
payloadSize = 0;
gameCode = "";
}
// 其他的访问请求,需要返回一段内容
else {
this.type = OTHER;
payloadSize = 0;
gameCode = "";
}
// readBuff.compact();
if (payloadSize > Globals.MAX_IN_EVENT_SIZE)
throw new IllegalArgumentException("Header specifies payload size (" +
payloadSize + ") greater than MAX_EVENT_SIZE(" +
Globals.MAX_IN_EVENT_SIZE + ")");
// check bounds on the payload
gotHeader = true;
return true;
} else {
return false;
}
}
/**
* check for a complete payload
*/
private boolean checkPayload() {
if (readBuff.remaining() >= payloadSize) {
try {
readBuff.get(payload, 0, payloadSize);
} catch (BufferUnderflowException bue) {
log.error("buffer underflow", bue);
}
return true;
}
else
return false;
}
}// Attachment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -