2011-06-18 21:42:34 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-02-06 11:54:04 +00:00
|
|
|
using System.Net.Sockets;
|
2011-06-18 21:42:34 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public class SparkleListenerTcp : SparkleListenerBase {
|
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
private Socket socket;
|
2011-06-18 21:42:34 +00:00
|
|
|
private Thread thread;
|
2012-02-08 00:44:56 +00:00
|
|
|
private Object socket_lock = new Object ();
|
|
|
|
private bool is_connected = false;
|
|
|
|
private bool is_connecting = false;
|
2012-02-09 22:42:51 +00:00
|
|
|
private DateTime last_ping = DateTime.Now;
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
|
2011-06-28 18:36:47 +00:00
|
|
|
public SparkleListenerTcp (Uri server, string folder_identifier) :
|
|
|
|
base (server, folder_identifier)
|
2011-06-18 21:42:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool IsConnected {
|
|
|
|
get {
|
2012-02-01 16:23:49 +00:00
|
|
|
lock (this.socket_lock)
|
|
|
|
return this.is_connected;
|
|
|
|
}
|
|
|
|
}
|
2011-07-24 18:22:17 +00:00
|
|
|
|
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
public override bool IsConnecting {
|
|
|
|
get {
|
|
|
|
lock (this.socket_lock)
|
|
|
|
return this.is_connecting;
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 22:38:32 +00:00
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
// Starts a new thread and listens to the channel
|
|
|
|
public override void Connect ()
|
|
|
|
{
|
2012-02-01 16:23:49 +00:00
|
|
|
this.is_connecting = true;
|
2011-06-18 21:42:34 +00:00
|
|
|
|
|
|
|
this.thread = new Thread (
|
|
|
|
new ThreadStart (delegate {
|
2012-02-01 16:23:49 +00:00
|
|
|
int port = Server.Port;
|
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
port = 1986;
|
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
try {
|
2012-02-01 16:23:49 +00:00
|
|
|
lock (this.socket_lock) {
|
|
|
|
this.socket = new Socket (AddressFamily.InterNetwork,
|
2012-02-01 22:04:58 +00:00
|
|
|
SocketType.Stream, ProtocolType.Tcp) {
|
2012-02-09 22:42:51 +00:00
|
|
|
ReceiveTimeout = 5 * 1000,
|
|
|
|
SendTimeout = 5 * 1000
|
2012-02-01 22:04:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Try to connect to the server
|
2011-10-25 16:16:29 +00:00
|
|
|
this.socket.Connect (Server.Host, port);
|
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
this.is_connecting = false;
|
|
|
|
this.is_connected = true;
|
2011-06-18 21:42:34 +00:00
|
|
|
|
2011-07-24 18:22:17 +00:00
|
|
|
OnConnected ();
|
|
|
|
|
2012-02-01 22:04:58 +00:00
|
|
|
// Subscribe to channels of interest to us
|
2012-02-11 19:23:38 +00:00
|
|
|
foreach (string channel in base.channels)
|
|
|
|
AlsoListenToInternal (channel);
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
} catch (SocketException e) {
|
|
|
|
this.is_connected = false;
|
|
|
|
this.is_connecting = false;
|
|
|
|
|
2012-02-19 20:33:25 +00:00
|
|
|
if (this.socket != null)
|
|
|
|
this.socket.Close ();
|
2012-02-09 22:42:51 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
OnDisconnected (e.Message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-10 13:26:33 +00:00
|
|
|
byte [] bytes = new byte [4096];
|
|
|
|
int bytes_read = 0;
|
|
|
|
this.last_ping = DateTime.Now;
|
2012-02-01 16:23:49 +00:00
|
|
|
|
2012-02-01 22:04:58 +00:00
|
|
|
// Wait for messages
|
2012-02-01 22:17:43 +00:00
|
|
|
while (this.is_connected) {
|
2012-03-13 17:57:05 +00:00
|
|
|
try {
|
|
|
|
// This blocks the thread
|
|
|
|
int i = 0;
|
|
|
|
while (this.socket.Available < 1) {
|
|
|
|
try {
|
|
|
|
// We've timed out, let's ping the server to
|
|
|
|
// see if the connection is still up
|
|
|
|
if (i == 180) {
|
2012-02-09 22:42:51 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp",
|
2012-03-13 17:57:05 +00:00
|
|
|
"Pinging " + Server);
|
2012-02-09 22:42:51 +00:00
|
|
|
|
2012-03-13 17:57:05 +00:00
|
|
|
byte [] ping_bytes = Encoding.UTF8.GetBytes ("ping\n");
|
|
|
|
byte [] pong_bytes = new byte [4096];
|
|
|
|
|
|
|
|
lock (this.socket_lock)
|
|
|
|
this.socket.Send (ping_bytes);
|
|
|
|
|
|
|
|
if (this.socket.Receive (pong_bytes) < 1)
|
|
|
|
// 10057 means "Socket is not connected"
|
|
|
|
throw new SocketException (10057);
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp",
|
|
|
|
"Received pong from " + Server);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
this.last_ping = DateTime.Now;
|
|
|
|
|
|
|
|
} else {
|
2012-03-17 19:16:53 +00:00
|
|
|
|
2012-03-13 17:57:05 +00:00
|
|
|
// Check when the last ping occured. If it's
|
|
|
|
// significantly longer than our regular interval the
|
|
|
|
// system likely woke up from sleep and we want to
|
|
|
|
// simulate a disconnect
|
|
|
|
int sleepiness = DateTime.Compare (
|
|
|
|
this.last_ping.AddMilliseconds (180 * 1000 * 1.2),
|
|
|
|
DateTime.Now
|
|
|
|
);
|
|
|
|
|
|
|
|
if (sleepiness <= 0) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp",
|
|
|
|
"System woke up from sleep");
|
|
|
|
|
|
|
|
// 10057 means "Socket is not connected"
|
|
|
|
throw new SocketException (10057);
|
|
|
|
}
|
2012-02-09 22:42:51 +00:00
|
|
|
}
|
2012-02-08 01:17:34 +00:00
|
|
|
|
2012-03-13 17:57:05 +00:00
|
|
|
// The ping failed: disconnect completely
|
|
|
|
} catch (SocketException) {
|
|
|
|
this.is_connected = false;
|
|
|
|
this.is_connecting = false;
|
|
|
|
|
2012-03-17 19:16:53 +00:00
|
|
|
if (this.socket != null) {
|
2012-03-13 17:57:05 +00:00
|
|
|
this.socket.Close ();
|
2012-03-17 19:16:53 +00:00
|
|
|
this.socket = null;
|
|
|
|
}
|
2012-02-09 22:42:51 +00:00
|
|
|
|
2012-03-13 17:57:05 +00:00
|
|
|
OnDisconnected ("Ping timeout");
|
|
|
|
return;
|
2012-02-05 21:59:06 +00:00
|
|
|
|
2012-03-13 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Sleep (1000);
|
|
|
|
i++;
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2012-03-13 17:57:05 +00:00
|
|
|
} catch (ObjectDisposedException) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-09 22:42:51 +00:00
|
|
|
|
|
|
|
if (this.socket.Available > 0)
|
|
|
|
lock (this.socket_lock)
|
|
|
|
bytes_read = this.socket.Receive (bytes);
|
|
|
|
|
2012-02-01 22:04:58 +00:00
|
|
|
// Parse the received message
|
2012-02-01 16:23:49 +00:00
|
|
|
if (bytes_read > 0) {
|
|
|
|
string received = Encoding.UTF8.GetString (bytes);
|
|
|
|
string line = received.Substring (0, received.IndexOf ("\n"));
|
2012-02-10 13:26:33 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
if (!line.Contains ("!"))
|
|
|
|
continue;
|
2012-02-10 13:26:33 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
string folder_identifier = line.Substring (0, line.IndexOf ("!"));
|
|
|
|
string message = CleanMessage (line.Substring (line.IndexOf ("!") + 1));
|
|
|
|
|
|
|
|
if (!folder_identifier.Equals ("debug") &&
|
|
|
|
!String.IsNullOrEmpty (message)) {
|
2012-02-09 22:42:51 +00:00
|
|
|
|
2012-02-01 22:04:58 +00:00
|
|
|
// We have a message!
|
2012-02-01 16:23:49 +00:00
|
|
|
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
|
|
|
|
}
|
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.thread.Start ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-11 19:23:38 +00:00
|
|
|
protected override void AlsoListenToInternal (string folder_identifier)
|
2011-06-18 21:42:34 +00:00
|
|
|
{
|
2012-02-11 19:23:38 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp",
|
|
|
|
"Subscribing to channel " + folder_identifier + " on " + Server);
|
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
string to_send = "subscribe " + folder_identifier + "\n";
|
2011-11-13 00:57:00 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
try {
|
2012-02-01 22:04:58 +00:00
|
|
|
lock (this.socket_lock)
|
2012-02-01 16:23:49 +00:00
|
|
|
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
|
2011-07-02 01:13:33 +00:00
|
|
|
|
2012-02-09 22:42:51 +00:00
|
|
|
this.last_ping = DateTime.Now;
|
|
|
|
|
2012-03-17 19:16:53 +00:00
|
|
|
} catch (Exception e) {
|
2012-02-01 16:23:49 +00:00
|
|
|
this.is_connected = false;
|
|
|
|
this.is_connecting = false;
|
2011-11-13 00:57:00 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
OnDisconnected (e.Message);
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-11 19:23:38 +00:00
|
|
|
protected override void AnnounceInternal (SparkleAnnouncement announcement)
|
2011-06-18 21:42:34 +00:00
|
|
|
{
|
2011-07-02 01:24:12 +00:00
|
|
|
string to_send = "announce " + announcement.FolderIdentifier
|
|
|
|
+ " " + announcement.Message + "\n";
|
2011-06-18 21:42:34 +00:00
|
|
|
|
2011-10-03 12:39:26 +00:00
|
|
|
try {
|
2012-02-01 16:23:49 +00:00
|
|
|
lock (this.socket_lock)
|
2012-03-17 19:16:53 +00:00
|
|
|
if (this.socket != null)
|
|
|
|
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
|
2012-02-01 16:23:49 +00:00
|
|
|
|
2012-02-09 22:42:51 +00:00
|
|
|
this.last_ping = DateTime.Now;
|
|
|
|
|
2012-03-17 19:16:53 +00:00
|
|
|
} catch (Exception e) {
|
2012-02-01 16:23:49 +00:00
|
|
|
this.is_connected = false;
|
|
|
|
this.is_connecting = false;
|
2011-10-03 12:39:26 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
OnDisconnected (e.Message);
|
2011-07-02 01:13:33 +00:00
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Dispose ()
|
|
|
|
{
|
|
|
|
this.thread.Abort ();
|
|
|
|
this.thread.Join ();
|
2012-02-01 16:23:49 +00:00
|
|
|
|
2012-02-19 20:33:25 +00:00
|
|
|
if (this.socket != null)
|
2012-03-17 19:16:53 +00:00
|
|
|
this.socket.Close ();
|
2012-02-19 20:33:25 +00:00
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
base.Dispose ();
|
|
|
|
}
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
|
|
|
|
private string CleanMessage (string message)
|
2011-11-05 21:09:09 +00:00
|
|
|
{
|
2012-02-01 16:23:49 +00:00
|
|
|
return message.Trim ()
|
|
|
|
.Replace ("\n", "")
|
|
|
|
.Replace ("\0", "");
|
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|