Skip to content

Commit

Permalink
兼容udt4版本,解决连接协议的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyuttt committed Aug 28, 2018
1 parent 86e6dc6 commit 8ec40f8
Show file tree
Hide file tree
Showing 172 changed files with 25,001 additions and 2,237 deletions.
362 changes: 362 additions & 0 deletions .metadata/.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*********************************************************************************
* Copyright (c) 2010 Forschungszentrum Juelich GmbH
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer at the end. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* (2) Neither the name of Forschungszentrum Juelich GmbH nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* DISCLAIMER
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************************/

package udt.packets;

import java.io.ByteArrayOutputStream;

import udt.UDTSession;

public class ConnectionHandshake extends ControlPacket {
private long udtVersion=4;

public static final long SOCKET_TYPE_STREAM=0;

public static final long SOCKET_TYPE_DGRAM=1;

private long socketType= SOCKET_TYPE_DGRAM; //stream or dgram

private long initialSeqNo = 0;
private long packetSize;
private long maxFlowWndSize;

public static final long CONNECTION_TYPE_REGULAR=1;

public static final long CONNECTION_TYPE_RENDEZVOUS=0;

private long connectionType = CONNECTION_TYPE_REGULAR;//regular or rendezvous mode

private long socketID;

private long cookie=0;

private long[] PeerIP=null;//cd 2018-08-28


public ConnectionHandshake(){
this.controlPacketType=ControlPacketType.CONNECTION_HANDSHAKE.ordinal();
}

public ConnectionHandshake(byte[]controlInformation){
this();
decode(controlInformation);
}

//faster than instanceof...
@Override
public boolean isConnectionHandshake(){
return true;
}

void decode(byte[]data){
udtVersion =PacketUtil.decode(data, 0);
socketType=PacketUtil.decode(data, 4);
initialSeqNo=PacketUtil.decode(data, 8);
packetSize=PacketUtil.decode(data, 12);
maxFlowWndSize=PacketUtil.decode(data, 16);
connectionType=PacketUtil.decode(data, 20);
socketID=PacketUtil.decode(data, 24);
if(data.length>28){
cookie=PacketUtil.decode(data, 28);
}
if(data.length>36)
{
//cd
cookie=PacketUtil.decode(data, 36);
}
}

public long getUdtVersion() {
return udtVersion;
}
public void setUdtVersion(long udtVersion) {
this.udtVersion = udtVersion;
}

public long getSocketType() {
return socketType;
}
public void setSocketType(long socketType) {
this.socketType = socketType;
}

public long getInitialSeqNo() {
return initialSeqNo;
}
public void setInitialSeqNo(long initialSeqNo) {
this.initialSeqNo = initialSeqNo;
}

public long getPacketSize() {
return packetSize;
}
public void setPacketSize(long packetSize) {
this.packetSize = packetSize;
}

public long getMaxFlowWndSize() {
return maxFlowWndSize;
}
public void setMaxFlowWndSize(long maxFlowWndSize) {
this.maxFlowWndSize = maxFlowWndSize;
}

public long getConnectionType() {
return connectionType;
}
public void setConnectionType(long connectionType) {
this.connectionType = connectionType;
}

public long getSocketID() {
return socketID;
}
public void setSocketID(long socketID) {
this.socketID = socketID;
}

@Override
public byte[] encodeControlInformation(){
try {
ByteArrayOutputStream bos=new ByteArrayOutputStream(24);
bos.write(PacketUtil.encode(udtVersion));
bos.write(PacketUtil.encode(socketType));
bos.write(PacketUtil.encode(initialSeqNo));
bos.write(PacketUtil.encode(packetSize));
bos.write(PacketUtil.encode(maxFlowWndSize));
bos.write(PacketUtil.encode(connectionType));
bos.write(PacketUtil.encode(socketID));
return bos.toByteArray();
} catch (Exception e) {
// can't happen
return null;
}

}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ConnectionHandshake other = (ConnectionHandshake) obj;
if (connectionType != other.connectionType)
return false;
if (initialSeqNo != other.initialSeqNo)
return false;
if (maxFlowWndSize != other.maxFlowWndSize)
return false;
if (packetSize != other.packetSize)
return false;
if (socketID != other.socketID)
return false;
if (socketType != other.socketType)
return false;
if (udtVersion != other.udtVersion)
return false;
return true;
}


@Override
public String toString(){
StringBuilder sb=new StringBuilder();
sb.append("ConnectionHandshake [");
sb.append("connectionType=").append(connectionType);
UDTSession session=getSession();
if(session!=null){
sb.append(", ");
sb.append(session.getDestination());
}
sb.append(", mySocketID=").append(socketID);
sb.append(", initialSeqNo=").append(initialSeqNo);
sb.append(", packetSize=").append(packetSize);
sb.append(", maxFlowWndSize=").append(maxFlowWndSize);
sb.append(", socketType=").append(socketType);
sb.append(", destSocketID=").append(destinationID);
if(cookie>0)sb.append(", cookie=").append(cookie);
sb.append("]");
return sb.toString();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* �ļ�����Tools.java
*
* �汾��Ϣ��
* ���ڣ�2018��8��28��
* Copyright ���� Corporation 2018
* ��Ȩ����
*
*/
package udt.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
*
* ��Ŀ���ƣ�judt
* �����ƣ�Tools
* �������� һ�㹤��
* �����ˣ�jinyu
* ����ʱ�䣺2018��8��28�� ����10:47:31
* �޸��ˣ�jinyu
* �޸�ʱ�䣺2018��8��28�� ����10:47:31
* �޸ı�ע��
* @version
*
*/
public class Tools {
public static InetAddress getLocalHostLANAddress() throws Exception {
try {
InetAddress candidateAddress = null;
// �������е�����ӿ�
for (Enumeration<?> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
// �����еĽӿ����ٱ���IP
for (Enumeration<?> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) {// �ų�loopback���͵�ַ
if (inetAddr.isSiteLocalAddress()) {
// �����site-local��ַ����������
return inetAddr;
} else if (candidateAddress == null) {
// site-local���͵ĵ�ַδ�����֣��ȼ�¼��ѡ��ַ
candidateAddress = inetAddr;
}
}
}
}
if (candidateAddress != null) {
return candidateAddress;
}
// ���û�з��� non-loopback��ַ.ֻ�������ѡ�ķ���
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
return jdkSuppliedAddress;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
*
* @Title: getPeerIP
* @Description: �����ַ���
* @param @return ����
* @return String ��������
*/
public static String getPeerIP()
{
String address="127.0.0.1";
InetAddress addr = null;
try {
addr = getLocalHostLANAddress();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(addr!=null)
{
address=addr.toString();
address=addr.getHostAddress();
}
return address;
}
public static long ipToLong(String strIp) {
String[]ip = strIp.split("\\.");
return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16) + (Long.parseLong(ip[2]) << 8) + Long.parseLong(ip[3]);
}
public static String longToIP(long longIp) {
StringBuffer sb = new StringBuffer("");
// ֱ������24λ
sb.append(String.valueOf((longIp >>> 24)));
sb.append(".");
// ����8λ��0��Ȼ������16λ
sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
sb.append(".");
// ����16λ��0��Ȼ������8λ
sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
sb.append(".");
// ����24�0
sb.append(String.valueOf((longIp & 0x000000FF)));
return sb.toString();
}
public static long[] ip6ToLong(String strIp) {
long[]ips=new long[4];
String[]ip = strIp.split(":");
//128�
ips[3] =(Long.parseLong(ip[15]) << 24) + (Long.parseLong(ip[14]) << 16) + (Long.parseLong(ip[13]) << 8) + Long.parseLong(ip[12]);
ips[2] =(Long.parseLong(ip[11]) << 24) + (Long.parseLong(ip[10]) << 16) + (Long.parseLong(ip[9]) << 8) + Long.parseLong(ip[8]);
ips[1] =(Long.parseLong(ip[7]) << 24) + (Long.parseLong(ip[6]) << 16) + (Long.parseLong(ip[5]) << 8) + Long.parseLong(ip[4]);
ips[0] =(Long.parseLong(ip[3]) << 24) + (Long.parseLong(ip[2]) << 16) + (Long.parseLong(ip[1]) << 8) + Long.parseLong(ip[0]);
return ips;
}

public static String longToIP6(long[] longIps) {
StringBuffer sb = new StringBuffer("");
for(int i=0;i<4;i++)
{
longIp=longIps[i];
// ֱ������24λ
sb.append(String.valueOf((longIp >>> 24)));
sb.append(".");
// ����8λ��0��Ȼ������16λ
sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
sb.append(".");
// ����16λ��0��Ȼ������8λ
sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
sb.append(".");
// ����24�0
sb.append(String.valueOf((longIp & 0x000000FF)));
}
return sb.toString();
}
}
Loading

0 comments on commit 8ec40f8

Please sign in to comment.