connectpacket.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 111 行

JAVA
111
字号
/* Stream-2-Stream - Peer to peer television and radio
 * Project homepage: http://s2s.sourceforge.net/
 * Copyright (C) 2005-2006 Jason Hooks
 * ---------------------------------------------------------------------------
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------------
 */
package p2pradio.packets;

import p2pradio.Radio;
import p2pradio.gui.MainFrame;
import java.net.*;


/**
 *This packet is sent to the server to connect.
 *Contains bandwidth and version information.  
 *The server sends back either an accept or deny packet.
 *UDP
 *
 *@author Jason Hooks
 */
public class ConnectPacket extends Packet {
	private short networkVersion;
	private float unverifiedUpload;
	private float unverifiedDownload;
	private float verifiedUploadMax;
	private float verifiedDownloadMax;
	private float verifiedUploadLimit;
	private float verifiedDownloadLimit;
	private byte[] IP;
	
	public ConnectPacket(double LANUpload, double LANDownload, double InternetUpload, 
			double InternetDownload, double InternetUploadMax, double InternetDownloadMax, String IP)
	{
		this(Packet.CONNECT, new byte[getMaxLength()], LANUpload, LANDownload, InternetUpload, 
			InternetDownload, InternetUploadMax, InternetDownloadMax, IP);
	}
	public ConnectPacket(byte packetType, byte content[], double LANUpload, double LANDownload, double InternetUpload, 
			double InternetDownload, double InternetUploadMax, double InternetDownloadMax, String IP)
	{
		super(packetType, content);
		networkVersion = Radio.networkVersion;
		unverifiedUpload = (float) LANUpload;
		unverifiedDownload = (float) LANDownload;
		verifiedUploadMax = (float) InternetUploadMax;
		verifiedDownloadMax = (float) InternetDownloadMax;
		verifiedUploadLimit = (float) InternetUpload;
		verifiedDownloadLimit = (float) InternetDownload;
		try {
			InetAddress ipget = InetAddress.getByName(IP);
			this.IP = ipget.getAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		writeShort(networkVersion);
		writeFloat(unverifiedUpload);
		writeFloat(unverifiedDownload);
		writeFloat(verifiedUploadMax);
		writeFloat(verifiedDownloadMax);
		writeFloat(verifiedUploadLimit);
		writeFloat(verifiedDownloadLimit);
		writeBytes(this.IP);
	}
	public ConnectPacket(byte content[], int contentLength)
	{
		super(content, contentLength);
		networkVersion = readShort();
		unverifiedUpload = readFloat();
		unverifiedDownload = readFloat();
		verifiedUploadMax = readFloat();
		verifiedDownloadMax = readFloat();
		verifiedUploadLimit = readFloat();
		verifiedDownloadLimit = readFloat();
		IP = readBytes(4);
	}
	protected static int getMaxLength()
	{
		return 30 + Packet.getMaxLength();
	}
	public short getNetworkVersion()
	{return networkVersion;}
	public double getUnverifiedUpload()
	{return unverifiedUpload;}
	public double getUnverifiedDownload()
	{return unverifiedDownload;}
	public double getVerifiedUploadMax()
	{return verifiedUploadMax;}
	public double getVerifiedDownloadMax()
	{return verifiedDownloadMax;}
	public double getVerifiedUploadLimit()
	{return verifiedUploadLimit;}
	public double getVerifiedDownloadLimit()
	{return verifiedDownloadLimit;}
	public byte[] getIP()
	{return IP;}
}

⌨️ 快捷键说明

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