⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protocolhandler.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on May 28, 2005
 * 
 * Handles all protocol communication for
 * server and client. All communication should come through here
 * rather than be throw sockets directly
 */
package org.GTADS.protocol;

import java.io.*;
import org.GTADS.client.*;


/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ProtocolHandler {
	
	public static String carriageReturn = "\r\n";
	public static String delimiter = ",";
	
	public static String LOGIN_SUCCESS = "LOGINSUCCESS";
	public static String LOGIN_FAIL = "LOGINFAIL";
	public static String MAXED_USERS = "MAXUSERS";
	
	public static int RECIPIENT = 0;
	public static int CONTEXT = 1;
	public static int SESSION = 2;
	public static int MESSAGE = 3;
	
	ProtocolHandler(){}
	
	public static boolean toClient(DataOutputStream ds, String metaData, String data) throws IOException {
		/* For now performs the simple act of 
		 * sending a message to a particular socket
		 * TODO -- Add a sort of encryption (encryption flag true or false)
		 * and tag ID to each sent packet
		 */
		
		if (ds != null) {
			ds.writeUTF(metaData + delimiter + data + carriageReturn);
			ds.flush();
			return true;
		}
		return false;	
	}
	
	public static String getDataFromMessage(String message, int placeValue){
		String dataCode[] = new String[4];
		if (message == null || message.split(",").length < 4)
			return null;
		
		//if (message.split(",").length != 4 && placeValue < 3)
			//return null;
		if (message.split(",").length > 4 && placeValue == 3) {
			// Commas in the message piece them together
			dataCode = message.split(",");
			String piecedTogetherMessage = dataCode[3];
			for (int i = 4; i <= dataCode.length - 1; i++) {
				piecedTogetherMessage += "," + dataCode[i];
			}
			return piecedTogetherMessage;
		}
		else {
			dataCode = message.split(",");
			return dataCode[placeValue];
		}
	}
	
	public static String getProtocolMsgFromCmd(String cmd){
		if (cmd != null){
			if (cmd.length() > 8){
				if (cmd.substring(0,6).equalsIgnoreCase("/join ")){
					String chatroom = new String(cmd.substring(7, cmd.length()));
					return DSChatClient.clientUser.getUsername() + "," + MetaData.GENERIC + "," 
					+ MetaData.JOIN_CHATROOM + "," + chatroom;
				}
			}
		}
		return null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -