Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TcpServerThread.java « usbserialtelnetserver « clusterrr « com « java « main « src « app - github.com/ClusterM/usb-serial-telnet-server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b18c516478561f712cbfff2a76faf7efc035041d (plain)
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
package com.clusterrr.usbserialtelnetserver;

import android.util.Log;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class TcpServerThread extends Thread {
    private UsbSerialTelnetService mUsbSerialTelnetService;
    private ServerSocket mTcpServer;
    private List<TcpClientThread> mClients;

    public TcpServerThread(UsbSerialTelnetService usbSerialTelnetService, ServerSocket tcpServer)
    {
        mUsbSerialTelnetService = usbSerialTelnetService;
        mTcpServer = tcpServer;
        mClients = new ArrayList<>();
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (mTcpServer == null) break;
                Socket socket = mTcpServer.accept();
                Log.i(UsbSerialTelnetService.TAG, "Connected: " + socket.getRemoteSocketAddress());
                TcpClientThread client = new TcpClientThread(mUsbSerialTelnetService, this, socket);
                client.start();
                mClients.add(client);
            }
        }
        catch (SocketException e) {
            Log.i(UsbSerialTelnetService.TAG, "Server: " + e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        close();
        Log.i(UsbSerialTelnetService.TAG, "Server: stopped");
    }

    public void removeClient(TcpClientThread tcpClientThread) {
        mClients.remove(tcpClientThread);
    }

    public void write(byte[] data) throws IOException {
        write(data, 0, data.length);
    }

    public void write(byte[] data, int offset, int len) throws IOException {
        List<TcpClientThread> toRemove = new ArrayList<>();
        for (TcpClientThread client : mClients) {
            try {
                client.write(data, offset, len);
            }
            catch (Exception ex) {
                ex.printStackTrace();
                toRemove.add(client);
            }
        }
        for (TcpClientThread client : toRemove) {
            client.close();
            mClients.remove(client);
        }
    }

    public void close()
    {
        try {
            if (mTcpServer != null) {
                mTcpServer.close();
                mTcpServer = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (TcpClientThread client : mClients) {
            try {
                client.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        mClients.clear();
    }
}