| | |
| | | package com.hdl.sdk.socket.client; |
| | | |
| | | |
| | | import android.content.Context; |
| | | import android.net.wifi.WifiManager; |
| | | import android.text.TextUtils; |
| | | |
| | | import com.hdl.sdk.common.HDLSdk; |
| | | import com.hdl.sdk.common.utils.IpUtils; |
| | | import com.hdl.sdk.common.utils.LogUtils; |
| | | import com.hdl.sdk.socket.SocketBoot; |
| | | import com.hdl.sdk.socket.SocketOptions; |
| | | import com.hdl.sdk.socket.SocketPool; |
| | | import com.hdl.sdk.socket.codec.IHandleMessage; |
| | | import com.hdl.sdk.socket.udp.UdpSocketBoot; |
| | | import com.hdl.sdk.socket.udp.UdpSocketOptions; |
| | | import com.hdl.sdk.socket.codec.IHandleMessage; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.DatagramPacket; |
| | | import java.net.DatagramSocket; |
| | | import java.net.InetAddress; |
| | | import java.net.InetSocketAddress; |
| | | import java.net.SocketAddress; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 当前socket |
| | | */ |
| | | private DatagramSocket mSocket; |
| | | private DatagramSocket mSocket; |
| | | |
| | | /** |
| | | * 接收数据包 |
| | | */ |
| | | private DatagramPacket receivePacket; |
| | | |
| | | private WifiManager.MulticastLock lock; |
| | | /** |
| | | * 缓冲区大小 |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 初始化参数 |
| | | * @param ipAddress 本地监听端口 |
| | | * @param port 本地监听端口 |
| | | * |
| | | * @param ipAddress 本地监听端口 |
| | | * @param port 本地监听端口 |
| | | * @param socketOptions |
| | | */ |
| | | private UdpClient(String ipAddress,int port, UdpSocketOptions socketOptions) { |
| | | private UdpClient(String ipAddress, int port, UdpSocketOptions socketOptions) { |
| | | this.ipAddress = ipAddress; |
| | | this.port = port; |
| | | this.socketOptions = socketOptions; |
| | | byte[] receiveByte = new byte[BUFFER]; |
| | | receivePacket = new DatagramPacket(receiveByte, receiveByte.length); |
| | | |
| | | WifiManager manager = (WifiManager) HDLSdk.getInstance().getContext().getApplicationContext() |
| | | .getSystemService(Context.WIFI_SERVICE); |
| | | this.lock = manager.createMulticastLock("UDPwifi"); |
| | | } |
| | | |
| | | /** |
| | | * 初始化参数 |
| | | * |
| | | * @param ipAddress 本地监听IP地址 |
| | | * @param port 本地监听端口 |
| | | * @param port 本地监听端口 |
| | | * @param options |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 初始化参数 |
| | | * @param port 本地监听端口 |
| | | * |
| | | * @param port 本地监听端口 |
| | | * @param options |
| | | * @return |
| | | */ |
| | |
| | | |
| | | |
| | | @Override |
| | | public void bind() throws Exception { |
| | | public synchronized void bind() throws Exception { |
| | | try { |
| | | //已经绑定过就不用再绑定 |
| | | if (null != mSocket) { |
| | | return; |
| | | } |
| | | lock.acquire(); |
| | | if (TextUtils.isEmpty(ipAddress)) { |
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port)); |
| | | }else { |
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(ipAddress,port)); |
| | | } else { |
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(ipAddress, port)); |
| | | } |
| | | mSocket.setBroadcast(true); |
| | | // mSocket.setReuseAddress(true); |
| | |
| | | if (handleMessage != null) { |
| | | byte[] data = new byte[receivePacket.getLength()]; |
| | | System.arraycopy(receivePacket.getData(), 0, data, 0, data.length); |
| | | handleMessage.read(data,receivePacket.getAddress().getHostAddress()); |
| | | handleMessage.read(data, receivePacket.getAddress().getHostAddress()); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | |
| | | |
| | | |
| | | @Override |
| | | public void sendMsg(String ipAddress,int port, byte[] msg) throws Exception { |
| | | public void sendMsg(String ipAddress, int port, byte[] msg) throws Exception { |
| | | if (msg == null) { |
| | | return; |
| | | } |
| | | final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, InetAddress.getByName(ipAddress), port); |
| | | mSocket.send(sendPacket); |
| | | if (mSocket != null && sendPacket != null) { |
| | | mSocket.send(sendPacket); |
| | | } |
| | | } |
| | | } |