-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
954 changed files
with
165,752 additions
and
6,786 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
290 changes: 290 additions & 0 deletions
290
.metadata/.plugins/org.eclipse.core.resources/.history/0/209a8eaa21ac001710ff8a7c6bda0fb8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
99 changes: 99 additions & 0 deletions
99
.metadata/.plugins/org.eclipse.core.resources/.history/0/c0b151e22dac001710ff8a7c6bda0fb8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.