gtavirtualhostudp.java

来自「是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术」· Java 代码 · 共 87 行

JAVA
87
字号
/*
 * Created on Feb 15, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.GTADS.proxy;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author sday
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GTAVirtualHostUDP extends Thread{
	public static GTAVirtualHostUDP instance;
	private DatagramSocket virtualUDPHostSocket;
	private int listenPort = 2351;
	private int sendPort = 2350;
	private byte[] udpData = new byte[256];
	private boolean initial = true;

	public static GTAVirtualHostUDP getInstance(){
		if (instance == null){
			instance = new GTAVirtualHostUDP();
		}
		return instance;
	}
	
	public static void clearInstance(){
		if (instance != null){
			if (instance.virtualUDPHostSocket != null){
				instance.virtualUDPHostSocket.close();
			}
			instance.stop();
			instance = null;
		}
	}
	
	public GTAVirtualHostUDP (){
//		if (Proxy.isSpecial){
//			listenPort = 2350;
//			sendPort = 2351;
//		}
		start();
	}
	
	public void run(){
		byte[] buffer = new byte[256];
		DatagramPacket p = new DatagramPacket(buffer, buffer.length);	
		try {
			virtualUDPHostSocket = new DatagramSocket(listenPort);
			while (true){
				// During gameplay take all UDP Packets recieved from game
				// and send them to remote player
				virtualUDPHostSocket.receive(p);
				Proxy.getInstance().sendUDPClientData(Proxy.getByteOffset(p.getData(),0,p.getLength()));
			}
		} catch (IOException ioe){
			return;
		}
	}
	
	public void sendUDPDataToGame(byte[] data) throws IOException {
		if (initial){
			initial = false;
			virtualUDPHostSocket.connect(InetAddress.getByName("localhost"), sendPort);
		}
		try {
			Thread.sleep(1);
			DatagramPacket p = new DatagramPacket(data,data.length);
			virtualUDPHostSocket.send(p);
		} catch (InterruptedException ie) {
			return;
		}
	}
	
	public void processVHostUDP(byte[] data) throws IOException {
		sendUDPDataToGame(data);
	}
}

⌨️ 快捷键说明

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