mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-20 01:47:36 +02:00
don't spawn a thread for every message sent
This commit is contained in:
parent
124a69caba
commit
c60e013ab4
1 changed files with 123 additions and 83 deletions
|
@ -27,6 +27,9 @@ import java.io.OutputStreamWriter;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by johannes on 06.01.16.
|
* Created by johannes on 06.01.16.
|
||||||
|
@ -43,9 +46,10 @@ public class TcpClient {
|
||||||
private PrintWriter mBufferOut;
|
private PrintWriter mBufferOut;
|
||||||
// used to read messages from the server
|
// used to read messages from the server
|
||||||
private BufferedReader mBufferIn;
|
private BufferedReader mBufferIn;
|
||||||
private Thread worker = null;
|
private Thread readerThread = null;
|
||||||
private Socket socket = null;
|
private Socket socket = null;
|
||||||
private String uid;
|
private String uid;
|
||||||
|
private BlockingQueue<String> messages = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of the class. OnMessagedReceived listens for the messages
|
* Constructor of the class. OnMessagedReceived listens for the messages
|
||||||
|
@ -67,18 +71,7 @@ public class TcpClient {
|
||||||
* @param message text entered by client
|
* @param message text entered by client
|
||||||
*/
|
*/
|
||||||
public void sendMessage(final String message) {
|
public void sendMessage(final String message) {
|
||||||
Runnable runnable = new Runnable() {
|
messages.offer(message);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (mBufferOut != null) {
|
|
||||||
Log.d(TAG, "Sending: " + message);
|
|
||||||
mBufferOut.println(message + "\r\n");
|
|
||||||
mBufferOut.flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Thread thread = new Thread(runnable);
|
|
||||||
thread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,17 +86,71 @@ public class TcpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
// mMessageListener = null;
|
// mMessageListener = null;
|
||||||
|
readerThread.interrupt();
|
||||||
|
try {
|
||||||
|
readerThread.join(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
mBufferIn = null;
|
mBufferIn = null;
|
||||||
mBufferOut = null;
|
mBufferOut = null;
|
||||||
mServerMessage = null;
|
mServerMessage = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(final String host, final int port) {
|
public void start(final String host, final int port) {
|
||||||
Runnable runnable = new Runnable() {
|
ReaderRunnable readerRunnable = new ReaderRunnable(host, port);
|
||||||
|
readerThread = new Thread(readerRunnable);
|
||||||
|
readerThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare the interface. The method messageReceived(String message) will
|
||||||
|
// must be implemented in the MyActivity
|
||||||
|
// class at on asynckTask doInBackground
|
||||||
|
public interface TcpClientListener {
|
||||||
|
void onMessageReceived(TcpClient tcpClient, String message);
|
||||||
|
|
||||||
|
void onConnecting(TcpClient tcpClient);
|
||||||
|
|
||||||
|
void onConnected(TcpClient tcpClient);
|
||||||
|
|
||||||
|
void onDisconnected(TcpClient tcpClient, Exception e);
|
||||||
|
}
|
||||||
|
|
||||||
|
;
|
||||||
|
|
||||||
|
class WriterRunnable implements Runnable {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (mRun) {
|
||||||
|
try {
|
||||||
|
String message = messages.poll(50, TimeUnit.MILLISECONDS);
|
||||||
|
if ((message != null) && (mBufferOut != null)) {
|
||||||
|
Log.d(TAG, "Sending: " + message);
|
||||||
|
mBufferOut.println(message + "\r\n");
|
||||||
|
mBufferOut.flush();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReaderRunnable implements Runnable {
|
||||||
|
private String host;
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
public ReaderRunnable(final String host, final int port) {
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
mRun = true;
|
mRun = true;
|
||||||
Exception exception = null;
|
Exception exception = null;
|
||||||
|
Thread writerThread = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (mMessageListener != null)
|
if (mMessageListener != null)
|
||||||
|
@ -129,6 +176,9 @@ public class TcpClient {
|
||||||
if (mMessageListener != null)
|
if (mMessageListener != null)
|
||||||
mMessageListener.onConnected(TcpClient.this);
|
mMessageListener.onConnected(TcpClient.this);
|
||||||
|
|
||||||
|
writerThread = new Thread(new WriterRunnable());
|
||||||
|
writerThread.start();
|
||||||
|
|
||||||
// in this while the client listens for the messages sent by the
|
// in this while the client listens for the messages sent by the
|
||||||
// server
|
// server
|
||||||
while (mRun) {
|
while (mRun) {
|
||||||
|
@ -155,6 +205,12 @@ public class TcpClient {
|
||||||
// after it is closed, which means a new socket instance has to
|
// after it is closed, which means a new socket instance has to
|
||||||
// be created.
|
// be created.
|
||||||
mRun = false;
|
mRun = false;
|
||||||
|
writerThread.interrupt();
|
||||||
|
try {
|
||||||
|
writerThread.join(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
|
@ -166,22 +222,6 @@ public class TcpClient {
|
||||||
mMessageListener.onDisconnected(TcpClient.this, exception);
|
mMessageListener.onDisconnected(TcpClient.this, exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
worker = new Thread(runnable);
|
|
||||||
worker.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Declare the interface. The method messageReceived(String message) will
|
|
||||||
// must be implemented in the MyActivity
|
|
||||||
// class at on asynckTask doInBackground
|
|
||||||
public interface TcpClientListener {
|
|
||||||
void onMessageReceived(TcpClient tcpClient, String message);
|
|
||||||
|
|
||||||
void onConnecting(TcpClient tcpClient);
|
|
||||||
|
|
||||||
void onConnected(TcpClient tcpClient);
|
|
||||||
|
|
||||||
void onDisconnected(TcpClient tcpClient, Exception e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue