-
Notifications
You must be signed in to change notification settings - Fork 0
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
14 changed files
with
239 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
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,96 @@ | ||
package com.dream.socket; | ||
|
||
import com.dream.socket.codec.DataProtocol; | ||
import com.dream.socket.codec.MessageDecode; | ||
import com.dream.socket.codec.MessageEncode; | ||
import com.dream.socket.codec.MessageHandle; | ||
import com.dream.socket.runnable.HandleRunnable; | ||
|
||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
|
||
public class DreamUDPSocket extends DreamSocket { | ||
|
||
private int mPort; | ||
private DatagramSocket mSocket; | ||
private UDPSocketSendRunnable mSocketSendRunnable; | ||
private DatagramPacket packet; | ||
private MessageDecode mMessageDecode; | ||
private HandleRunnable mHandleRunnable; | ||
|
||
public DreamUDPSocket(int port) { | ||
this.mPort = port; | ||
packet = new DatagramPacket(new byte[500], 500); | ||
} | ||
|
||
public <T extends DataProtocol> void codec(MessageDecode<T> messageDecode, MessageHandle<T> messageHandle, MessageEncode<T> messageEncode) { | ||
this.mHandleRunnable = new HandleRunnable<>(messageHandle); | ||
messageDecode.setHandleRunnable(mHandleRunnable); | ||
this.mMessageDecode = messageDecode; | ||
this.mSocketSendRunnable = new UDPSocketSendRunnable<>(messageEncode); | ||
} | ||
|
||
@Override | ||
public boolean onStart() { | ||
synchronized (this) { | ||
try { | ||
mSocket = new DatagramSocket(mPort); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
mSocketSendRunnable.setDatagramSocket(mSocket); | ||
executeRunnable(mSocketSendRunnable); | ||
executeRunnable(mHandleRunnable); | ||
while (isConnected()) { | ||
try { | ||
mSocket.receive(packet); | ||
mMessageDecode.put(packet.getSocketAddress(), packet.getData(), packet.getOffset(), packet.getLength()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
mSocket = null; | ||
if (isRunning()) { | ||
this.mHandleRunnable.status(Status.STATUS_FAIL); | ||
System.out.println("6秒后尝试重连"); | ||
this.wait(6000); | ||
System.out.println("开始重连"); | ||
} | ||
} catch (Exception ie) { | ||
System.out.println("重连发生异常"); | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean onStop() { | ||
if (mSocketSendRunnable != null) { | ||
mSocketSendRunnable.stop(); | ||
} | ||
if (this.mHandleRunnable != null) { | ||
this.mHandleRunnable.stop(); | ||
} | ||
if (mSocket != null) { | ||
mSocket.close(); | ||
mSocket = null; | ||
mHandleRunnable.status(Status.STATUS_DISCONNECT); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
if (mSocket != null && !mSocket.isClosed() && mSocket.isConnected() && isRunning()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean send(String host, int port, Object data) { | ||
return false; | ||
} | ||
} |
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,36 @@ | ||
package com.dream.socket; | ||
|
||
import com.dream.socket.codec.DataProtocol; | ||
import com.dream.socket.codec.MessageEncode; | ||
import com.dream.socket.runnable.SendRunnable; | ||
|
||
import java.io.IOException; | ||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
import java.net.SocketAddress; | ||
|
||
public class UDPSocketSendRunnable<T extends DataProtocol> extends SendRunnable<T> { | ||
|
||
private DatagramSocket mSocket; | ||
|
||
public UDPSocketSendRunnable(MessageEncode<T> encode) { | ||
super(encode); | ||
} | ||
|
||
public void setDatagramSocket(DatagramSocket socket) { | ||
this.mSocket = socket; | ||
} | ||
|
||
@Override | ||
protected boolean doSend(SocketAddress address, byte[] buffer, int offset, int length) { | ||
if (mSocket != null) { | ||
try { | ||
mSocket.send(new DatagramPacket(buffer, offset, length, address)); | ||
return true; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,7 @@ | ||
package com.dream.socket.codec; | ||
|
||
import java.net.SocketAddress; | ||
|
||
public abstract class DataProtocol { | ||
public SocketAddress mAddress; | ||
} |
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
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
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
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
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
Oops, something went wrong.