0
|
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
|
|
2 // All rights reserved.
|
|
3 //
|
|
4 //-------------------------------------------------------------------------
|
|
5 // This file is part of the TightVNC software. Please visit our Web site:
|
|
6 //
|
|
7 // http://www.tightvnc.com/
|
|
8 //
|
|
9 // This program is free software; you can redistribute it and/or modify
|
|
10 // it under the terms of the GNU General Public License as published by
|
|
11 // the Free Software Foundation; either version 2 of the License, or
|
|
12 // (at your option) any later version.
|
|
13 //
|
|
14 // This program is distributed in the hope that it will be useful,
|
|
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 // GNU General Public License for more details.
|
|
18 //
|
|
19 // You should have received a copy of the GNU General Public License along
|
|
20 // with this program; if not, write to the Free Software Foundation, Inc.,
|
|
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
22 //-------------------------------------------------------------------------
|
|
23 //
|
|
24
|
|
25 package com.glavsoft.rfb.protocol;
|
|
26
|
|
27 import com.glavsoft.exceptions.TransportException;
|
|
28 import com.glavsoft.rfb.client.ClientToServerMessage;
|
|
29 import com.glavsoft.transport.Writer;
|
|
30
|
|
31 import java.io.PrintWriter;
|
|
32 import java.io.StringWriter;
|
|
33 import java.util.logging.Logger;
|
|
34
|
|
35 public class SenderTask implements Runnable {
|
|
36
|
|
37 private final MessageQueue queue;
|
|
38 private final Writer writer;
|
|
39 private final ProtocolContext protocolContext;
|
|
40 private volatile boolean isRunning = false;
|
|
41
|
|
42 /**
|
|
43 * Create sender task
|
|
44 * Task runs as thread, receive messages from queue and sends them to writer.
|
|
45 * When no messages appears in queue longer than timeout period, sends FramebufferUpdate
|
|
46 * request
|
|
47 * @param messageQueue queue to poll messages
|
|
48 * @param writer writer to send messages out
|
|
49 * @param protocolContext protocol
|
|
50 */
|
|
51 public SenderTask(MessageQueue messageQueue, Writer writer, ProtocolContext protocolContext) {
|
|
52 this.queue = messageQueue;
|
|
53 this.writer = writer;
|
|
54 this.protocolContext = protocolContext;
|
|
55 }
|
|
56
|
|
57 @Override
|
|
58 public void run() {
|
|
59 isRunning = true;
|
|
60 while (isRunning) {
|
|
61 ClientToServerMessage message;
|
|
62 try {
|
|
63 message = queue.get();
|
|
64 if (message != null) {
|
|
65 message.send(writer);
|
|
66 }
|
|
67 } catch (InterruptedException e) {
|
|
68 // nop
|
|
69 } catch (TransportException e) {
|
|
70 Logger.getLogger(getClass().getName()).severe("Close session: " + e.getMessage());
|
|
71 if (isRunning) {
|
|
72 protocolContext.cleanUpSession("Connection closed");
|
|
73 }
|
|
74 stopTask();
|
|
75 } catch (Throwable te) {
|
|
76 StringWriter sw = new StringWriter();
|
|
77 PrintWriter pw = new PrintWriter(sw);
|
|
78 te.printStackTrace(pw);
|
|
79 if (isRunning) {
|
|
80 protocolContext.cleanUpSession(te.getMessage() + "\n" + sw.toString());
|
|
81 }
|
|
82 stopTask();
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86
|
|
87 public void stopTask() {
|
|
88 isRunning = false;
|
|
89 }
|
|
90
|
|
91 }
|