我正在编写一个解析操作码的服务器(只是一个简单的小爱好 MMO 服务器来自学绳索;目前有点混乱),我无法让服务器解析第二个操作码。我不确定问题是什么。每个操作码的格式是一个 8 字节的标头,例如 PRINTMSG,然后是正文,在这种情况下,它可能是一条消息,例如 Hello。每个操作码都以“|”结尾。我的代码似乎正确地获取和解析了每个操作码,但它没有执行第一个操作码之后的任何操作,这让我相信我对 BufferedReaders 有一些不了解的地方。我正在使用 read() 读取它,并将每个字符存储在一个 48 字节的数组中。自动刷新设置为 true。
这是相关的服务器代码:
public String readInputStream() {
String msg = "";
char[] charArray = new char[48];
short i = 0;
char current = '#';
int characterInt = -1;
while (current != '|') {
try {
if (in.ready()) {
characterInt = in.read();
if (characterInt == (-1)) continue;
current = (char) characterInt;
if (current == '|') break;
charArray[i] = current;
//if ((charArray[i] == 'n') && (charArray[i-1] == '\\')) {
// charArray = new char[48];
//}
i++;
}
}
catch (IOException e) {
System.out.println("Error reading input stream in server ConnectionThread");
if (DEBUGMODE) System.exit(1);
}
}
msg = new String(charArray);
msg = msg.substring(0,i);
return msg;
}
public static void printMsg(String msg) {
System.out.println("(Op-PRINTMSG): " +msg+"\n");
}
public static void executeOpCode(String op, String body) {
if (op.equals("ABSPLAYX")) receivedPlayerAbsoluteX(body);
else if (op.equals("ABSPLAYY")) receivedPlayerAbsoluteY(body);
else if (op.equals("FIRSTVOL")) requestVolley();
else if (op.equals("SECNDVOL")) returnVolley(body);
else if (op.equals("PRINTMSG")) printMsg(body);
}
和主循环:
public void run() {
while (shouldBeListening) {
//Main loop for the connection thread
String op = readInputStream();
//If stream has data, add the opcodes to opQueue.
if (op != ("")) {
separateOpsFromStream(op, incomingOpQueue);
}
//Extract incoming opcode head and body, then execute.
//This executes everything in the queue before moving on.
while (incomingOpQueue.size() > 0) {
op = incomingOpQueue.poll();
if (op != "") {
String opHeader = OpCodeOperations.readHeader(op);
String opBody = OpCodeOperations.readBody(op);
playerData.executeOpCode(opHeader, opBody);
}
}
}
}