Skip to content

Commit

Permalink
Merge branch 'judt1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyuttt committed Oct 10, 2017
2 parents 98ee830 + 720774f commit acafd12
Show file tree
Hide file tree
Showing 954 changed files with 165,752 additions and 6,786 deletions.
12,431 changes: 12,431 additions & 0 deletions .metadata/.bak_0.log

Large diffs are not rendered by default.

10,661 changes: 7,012 additions & 3,649 deletions .metadata/.log

Large diffs are not rendered by default.

Binary file modified .metadata/.mylyn/.tasks.xml.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
/**
*
*/
package judp;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import udt.UDTSession;
import udt.UDTSocket;
import udt.packets.Destination;

/**
* @author jinyu
*
*����˷��ص�����ӿڶ���
*����socket����������ݵĶ���
*/
public class judpSocket {
private final int bufSize=65535;
private UDTSocket socket=null;
private boolean isClose=false;
private long sendLen=0;//��������
private long socketID=0;//ID
private Thread closeThread;
private final int waitClose=10*1000;
private PackagetCombin pack=new PackagetCombin();
private int readLen=0;
public boolean getCloseState()
{
return isClose;
}
public judpSocket(UDTSocket usocket)
{
this.socket=usocket;
socketID=socket.getSession().getSocketID();
}

/**
* ��ȡID
* @return
*/
public long getSocketID()
{
return socketID;
}

/**
* �ر�
* �ȴ�������ɹر�
*/
public void close()
{
isClose=true;
//������ʵ�ر�
if(sendLen==0)
{
stop();
System.out.println("�����ر�socket");
}
else
{
//�й����������򻺳�
//SocketManager.getInstance().add(socket);

if(closeThread==null)
{
closeThread=new Thread(new Runnable() {

@Override
public void run() {
int num=0;
while(true)
{
if(socket.getSender().isSenderEmpty())
{
stop();
}
else
{
try {
TimeUnit.MILLISECONDS.sleep(100);
num++;
if(waitClose<=num*100)
{
stop();
break;
}
} catch (InterruptedException e) {

e.printStackTrace();
}
}
}

}

});
closeThread.setDaemon(true);
closeThread.setName("closeThread");
}
if(closeThread.isAlive())
{
return;
}
else
{
closeThread.start();
}
}
}

/**
* �����ر�
*/
public void stop()
{
//û�з��������ֱ�ӹرգ�����Ҫ�ȴ����ݷ������
try {
socket.close();
UDTSession serversession=socket.getEndpoint().removeSession(socketID);
if(serversession!=null)
{
serversession.getSocket().close();
socket.getReceiver().stop();
socket.getSender().stop();
System.out.println("�����ر�socket:"+serversession.getSocketID());
}

serversession=null;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("�����ر�socket");
}

/**
* ��ȡ����
* ���ؽ��յ��ֽڴ�С
*/
public int readData(byte[]data)
{
if(isClose)
{
return -1;
}
try {
int r=socket.getInputStream().read(data);
readLen+=r;
return r;
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}

/**
* ��ȡȫ������
*/
public byte[] readData()
{
byte[] result=null;
if(socket!=null)
{
byte[] readBytes=new byte[bufSize];//������
int r=0;
try {
while(true)
{
if(isClose)
{
return null;
}
r=socket.getInputStream().read(readBytes);
if(r==-1)
{
result=pack.getData();
break;
}
else
{
readLen+=r;
if(r==0)
{
try {
TimeUnit.MILLISECONDS.sleep(100);

continue;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//
byte[] buf=new byte[r];
System.arraycopy(readBytes, 0, buf, 0, r);
if(pack.addData(buf))
{
result=pack.getData();
break;
}


}
}

} catch (IOException e) {

e.printStackTrace();
}

}

return result;
}

/*
* ��ȡ��ʼ������
*/
public long getInitSeqNo()
{
if(socket!=null)
{
return socket.getSession().getInitialSequenceNumber();
}
return 0;
}

/**
* ���Ͱ���
*/
public int getDataStreamLen()
{
return socket.getSession().getDatagramSize();
}

/**
* Ŀ��socket
* @return
*/
public Destination getDestination()
{

if(socket!=null)
{
return socket.getSession().getDestination();
}
Destination tmp = null;
try {
tmp = new Destination(InetAddress.getLocalHost(), 0);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tmp;
}
/**
* ��������
* �����ݲ��ܷ���
*/
public boolean sendData(byte[]data) {
if(isClose)
{
return false;
}
try {
socket.getOutputStream().write(data);
sendLen=+1;
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public String getRemoteHost() {
return socket.getSession().getDestination().getAddress().getHostName();

}
public int getRemotePort() {
return socket.getSession().getDestination().getPort();
}
public long getID() {

return socketID;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
*
*/
package judp;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author jinyu
*
*/
public class PackagetSub {
private static AtomicLong sessionid=new AtomicLong(0);
public static int dataSzie=1472;
private static int bufsize=0;
private static int headLen=20;

/**
* �ָ�����
* @param data
* @return
*/
public static byte[][] splitData(byte[]data)
{
if(bufsize==0)
{
bufsize=dataSzie-headLen;
}
long session=sessionid.incrementAndGet();
int dataLen=data.length;
int num=data.length/bufsize+data.length%bufsize>0?1:0;
byte[][]sendData=new byte[num][];
int index=0;
ByteBuffer buf=ByteBuffer.allocate(dataSzie);
for(int i=0;i<num;i++)
{
buf.putLong(session);
buf.putInt(num);
buf.putInt(i);
buf.putInt(dataLen);
if(index+bufsize<data.length)
{
buf.put(data, index, bufsize);
}
else
{
buf.put(data, index, data.length-index);
}
//
buf.flip();
byte[]tmp=new byte[buf.limit()];
buf.get(tmp);
sendData[i]=tmp;
buf.clear();
}
return sendData;
}

/**
* �����ָ�����
* @param data
* @param len
* @return
*/
public byte[][] split(byte[]data,int len)
{
int size=len-headLen;
long session=sessionid.incrementAndGet();
int dataLen=data.length;
int num=data.length/size+data.length%size;
byte[][]sendData=new byte[num][];
int index=0;
ByteBuffer buf=ByteBuffer.allocate(len);
for(int i=0;i<num;i++)
{

buf.putLong(session);
buf.putInt(num);
buf.putInt(i);
buf.putInt(dataLen);
if(index+size<data.length)
{
buf.put(data, index, size);
}
else
{
buf.put(data, index, data.length-index-1);
}
//
buf.flip();
byte[]tmp=new byte[buf.limit()];

buf.get(tmp);
sendData[i]=tmp;
buf.clear();
}
return sendData;
}

}
Loading

0 comments on commit acafd12

Please sign in to comment.