mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.dev.bybb;
 
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
 
import main.page_debug_inf;
 
import com.Com;
import com.ComFn;
 
public class UdpServBYBB implements Runnable {
    private boolean running_en = true;
    private page_debug_inf dt_debug_inf;
    
    public UdpServBYBB(page_debug_inf db_inf) {
        dt_debug_inf = db_inf;
    }
    
    public void exitThread() {
        running_en = false;
    }
    
    @Override
    public void run() {
        while(true == running_en) {
            runDdpServ(dt_debug_inf);
            
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        
        dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " UDP Serv Thread Exit!\n");
    }
    
    private void runDdpServ(page_debug_inf out_inf) {
        DatagramSocket socket = null;
        DatagramChannel channel = null;
        Selector selector = null;
        try {
            //´´½¨channel
            channel = DatagramChannel.open();
            //Ö¸¶¨Îª·Ç×èÈû·½Ê½
            channel.configureBlocking(false);
            socket = channel.socket();
            //°ó¶¨ipºÍ¶Ë¿Ú
            int udp_serv_port = 6600;
            InetSocketAddress address = new InetSocketAddress(udp_serv_port);
            socket.bind(address);
            dt_debug_inf.addDebugInf("port: " + udp_serv_port + "\n");
            //´´½¨¼àÌýÆ÷
            selector = Selector.open();
            //×¢²á¶Áʼþ
            channel.register(selector, SelectionKey.OP_READ);
 
            //¶Á»º³å
            ByteBuffer readBuffer = ByteBuffer.allocate(1024);
            while (true == running_en) {
                //µÈʼþ³öÏÖ
                if (selector.select(100) < 1) {
                    Thread.sleep(10);
                    continue;
                }
                //»ñÈ¡·¢ÉúµÄʼþ
                Set<SelectionKey> keys = selector.selectedKeys();
                Iterator<SelectionKey> it = keys.iterator();
                while (it.hasNext()) {
                    //»ñȡʼþ£¬ÒƳýÕýÔÚ´¦ÀíµÄʼþ
                    SelectionKey key = it.next();
                    it.remove();
 
                    //¶ÁÈ¡ÏûÏ¢
                    if(key.isReadable()){
                        DatagramChannel datagramChannel = (DatagramChannel) key.channel();
                        readBuffer.clear();
                        datagramChannel.receive(readBuffer);
                        readBuffer.flip();
                        
                        byte[] dat_rx = new byte[readBuffer.limit()];
                        readBuffer.get(dat_rx);
                        if(dat_rx.length > 0) {
                            out_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                + " UDP RX: " + ComFn.bytesToHexString(dat_rx, dat_rx.length) + "\n");
                        }
                    }
                }
            }
            selector.close();
            socket.close();
            channel.close();
        } catch (IOException | InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if(null != selector) {
                    selector.close();
                }
                if(null != channel) {
                    channel.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            if(null != socket) {
                socket.close();
            }
        }
    }
}