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

📄 dschatserver.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on May 27, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.GTADS.server;
import java.io.*;
import java.net.*;
import java.util.Vector;

import org.GTADS.debug.*;
import org.GTADS.messenger.MessageAdapter;
import org.GTADS.protocol.MetaData;
import org.GTADS.usermanager.*;
import org.GTADS.helper.*;
import java.util.*;

/**
 * @author sday
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DSChatServer {
	private int serverPort;
	private ServerSocket serverListener;
	private int clientsConnected;
	
	protected static boolean verbose;
	protected static int debugLevel;
	protected static int statusReport;
	protected static String accountsFileName;
	public static String ServerSpecialAccount = "$SYSTEM$";
	
	public static DSChatServer instance;
	
	public static DSChatServer getInstance(){
		if (instance == null)
			return null;
		synchronized(DSChatServer.class){
			return instance;
		}
	}
	
	DSChatServer (int listeningPort){
		instance = this;
		Helper.setAsServer(true);
		serverPort = listeningPort;
		// Server Initialization
		if (ServerConfig.getInstance().getIsLogging())
			ServerLogger.getInstance();
		ServerLogger.sendConsoleOutput("Starting up server on port " + listeningPort + "...", this.getClass());
		
		ServerLogger.sendConsoleOutput("Reporting Debug Messages with a level of " + DSChatServer.debugLevel
				, this.getClass());
		// Activate Server Uptime In Cache
		Date d = new Date();
		ServerCacheHandler.userUptimeCache.put(ServerSpecialAccount,new Long(d.getTime()));
		
		// Activate Services
		AuthenticationManager.readInPasswords();
		if (ServerConfig.getInstance().isFriendsListEnabled())
			FriendsListManager.getInstance().readInFriendsList();
		new PeriodicStatusChecker();
		if (ServerConfig.getInstance().isAccountRefreshCacheOn())
			new PeriodicAccountRefresh();
		if (ServerConfig.getInstance().getGSIPSOn())
			GSIPServer.getInstance();
		PeriodicClientPing.getInstance();
		BanManagement.getInstance();
		if (ServerConfig.getInstance().getIsPublish())
			ServerPublishManager.getInstance();
		
		try {
			listenForClients();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) throws Error{
		ServerConfig.getInstance();
		ServerLogger.sendConsoleOutput("Configuration values read in from " + ServerConfig.configFileName, ServerConfig.class);
		
		new DSChatServer(ServerConfig.getInstance().getServerPort());
	}
	
	public void listenForClients() throws IOException {
		serverListener = new ServerSocket(serverPort);
		
		while(true) {
			Socket clientConnection = serverListener.accept();
			ServerLogger.sendConsoleDebugMessage("Incoming Connection on " + clientConnection, this.getClass());
			
			DataOutputStream writeToClient = new DataOutputStream(clientConnection.getOutputStream());
			
			ServerCacheHandler.socketStreamCache.put(clientConnection, writeToClient);
			// Handle this on Thread
			ServerLogger.sendConsoleDebugMessage("Creating new server thread...", ServerThreadHandler.class, 3);
			new ServerThreadHandler(this, clientConnection);
		}
		
	}
	
	public void serverKick(Socket serveredSocket) throws IOException{
		String userName = (String)ServerCacheHandler.socketUserCache.get(serveredSocket);
		Vector v = new Vector();
		v = (Vector)ServerCacheHandler.userChatroomCache.get(userName);
		for (int i = 0; i < v.size(); i++) {
			String userChatroom = (String)v.get(i);
			if (userChatroom != null) {
				killConnection(serveredSocket, null);
				MetaData kickedHeader = new MetaData(MetaData.FROM_SERVER, MetaData.CHATROOM, MetaData.NO_SESSION_NAME);
				MessageAdapter.sendToChatroom(kickedHeader, userChatroom, userName + " has been kicked from server");
			}
		}
	}
	
	public void killConnection (Socket severedSocket, DSChatServer serverInstance) {
		try {
			String userChatroom;
			String userName = (String)ServerCacheHandler.socketUserCache.get(severedSocket);
			severedSocket.close();
			removeConnected();
			
			if (userName != null){
				// Remove any gameroom the user was joined to
				if (ServerCacheHandler.userGameroomCache.containsKey(userName)){
					String gameRoom = (String)ServerCacheHandler.userGameroomCache.get(userName);
					GameroomManager.getInstance().leaveGameroom(userName,gameRoom);
				}
				if (ServerCacheHandler.userPingTimeCache.containsKey(userName)){
					ServerCacheHandler.userPingTimeCache.remove(userName);
				}
			}
			
			// Notify Chatroom that user is disconnected
			if (ServerCacheHandler.socketUserCache.get(severedSocket) != null) {
				Vector v = new Vector();
				if (ServerCacheHandler.userChatroomCache.get(ServerCacheHandler.socketUserCache.get(severedSocket)) != null){
					v = (Vector)ServerCacheHandler.userChatroomCache.get(ServerCacheHandler.socketUserCache.get(severedSocket)); 
				}
				for (int i = 0; i < v.size(); i++){
					userChatroom = (String)v.get(i);
					if (userChatroom != null && ServerCacheHandler.chatroomNameCache.get(userChatroom) != null) {
						partServeredClientFromChatroom(severedSocket, userChatroom);
						//((Chatroom)ServerCacheHandler.chatroomNameCache.get(userChatroom)).
						//removeUserFromChatroom((String)ServerCacheHandler.socketUserCache.get(severedSocket));
					}
				}
				v.removeAllElements();
			}
			
			// Notify Buddylists with user listed that user has signed off
			if (ServerCacheHandler.socketUserCache.get(severedSocket) != null)
				FriendsListManager.getInstance().announceFriendsSignOff((String)ServerCacheHandler.socketUserCache.get(severedSocket));
			
			if (userName != null){
				ServerCacheHandler.userUptimeCache.remove(userName); // Remove Uptime
				ServerCacheHandler.userProxyPortState.remove(userName);
			}
			ServerCacheHandler.socketStreamCache.remove(severedSocket);
			ServerCacheHandler.removeUserSocketCache((String)ServerCacheHandler.socketUserCache.get(severedSocket)
					, severedSocket);
			ServerLogger.sendConsoleDebugMessage(this.getTotalClientsConnected() + " users connected", this.getClass());
			// TODO Add user/chatroom cache to cleanup user from chatroom
		} 
		catch (IOException e) {
			ServerLogger.sendConsoleDebugMessage("Error killing connection...", IOException.class, 5);
			e.printStackTrace();
		}
	}
	
	public void addConnected() {
		clientsConnected++;
	}
	
	public void removeConnected() {
		if (clientsConnected > 0){
			clientsConnected--;
		}
	}
	
	public int getServerPort() {
		return serverPort;
	}
	
	public int getTotalClientsConnected (){
		return ServerCacheHandler.userSocketCache.size();
	}
	
	public static boolean isVerbose() {
		return verbose;
	}
	
	public static int debugLevel() {
		return debugLevel;
	}
	
	public static int getStatusReportInteveral(){
		return statusReport;
	}
	
	private void partServeredClientFromChatroom(Socket severedSocket, String chatroomName){
		MetaData announceListHeader = new MetaData(chatroomName, MetaData.CHATROOM, MetaData.PART_CHATROOM);
		
		Chatroom usersChatroom = (Chatroom)ServerCacheHandler.chatroomNameCache.get(chatroomName);
		if (usersChatroom == null)
			return;
		
		String userName = (String)ServerCacheHandler.socketUserCache.get(severedSocket);
		usersChatroom.removeUserFromChatroom(userName);
		Vector usersList = new Vector(usersChatroom.getListOfUsers());
		
		
		if (usersChatroom.getListOfUsers().isEmpty()){
			ServerCacheHandler.chatroomNameCache.remove(chatroomName);
			ServerLogger.sendConsoleDebugMessage("Chatroom " + chatroomName + " being destroyed (Chatroom Empty)"
					,this.getClass(), 5);
		}
		
		try {
			for (int i = 0; i < usersList.size(); i++) {
				MessageAdapter.sendData(null,(Socket)ServerCacheHandler.userSocketCache.get(usersList.get(i)), announceListHeader, userName);
			}
		} catch (IOException ioe){
			return; // error not important
		}
		
	}
	
	public static boolean isUserOnline(String user){
		return ServerCacheHandler.userSocketCache.containsKey(user);
	}
}

⌨️ 快捷键说明

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