SparkleShare/Sparkles/TcpListener.cs

260 lines
8.8 KiB
C#
Raw Normal View History

2011-06-18 21:42:34 +00:00
// SparkleShare, a collaboration and sharing tool.
2017-07-23 12:47:54 +00:00
// Copyright (C) 2010 Hylke Bons <hi@planetpeanut.uk>
2011-06-18 21:42:34 +00:00
//
// This program is free software: you can redistribute it and/or modify
2013-10-11 15:13:46 +00:00
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
2011-06-18 21:42:34 +00:00
//
// 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;
2016-03-31 08:35:26 +00:00
namespace Sparkles {
2011-06-18 21:42:34 +00:00
2016-03-30 23:36:31 +00:00
public class TcpListener : BaseListener {
2011-06-18 21:42:34 +00:00
2012-02-01 16:23:49 +00:00
private Socket socket;
2011-06-18 21:42:34 +00:00
private Thread thread;
2012-07-03 18:27:42 +00:00
private bool is_connected = false;
private bool is_connecting = false;
private DateTime last_ping = DateTime.Now;
2011-06-18 21:42:34 +00:00
2016-03-30 23:36:31 +00:00
public TcpListener (Uri server, string folder_identifier) : base (server, folder_identifier)
2011-06-18 21:42:34 +00:00
{
}
public override bool IsConnected {
get {
2012-07-03 18:27:42 +00:00
return this.is_connected;
2012-02-01 16:23:49 +00:00
}
}
2011-07-24 18:22:17 +00:00
2012-02-01 16:23:49 +00:00
public override bool IsConnecting {
get {
2012-07-03 18:27:42 +00:00
return this.is_connecting;
2011-06-18 21:42:34 +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
2012-07-15 13:59:04 +00:00
this.thread = new Thread (() => {
int port = Server.Port;
2012-02-01 16:23:49 +00:00
2012-07-15 13:59:04 +00:00
if (port < 0)
2012-09-03 18:44:36 +00:00
port = 443;
2012-02-01 16:23:49 +00:00
2012-07-15 13:59:04 +00:00
try {
this.socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) {
ReceiveTimeout = 5 * 1000,
SendTimeout = 5 * 1000
};
2012-02-01 16:23:49 +00:00
2012-07-15 13:59:04 +00:00
// Try to connect to the server
this.socket.Connect (Server.Host, port);
2012-02-01 16:23:49 +00:00
2012-07-15 13:59:04 +00:00
this.is_connecting = false;
this.is_connected = true;
2012-07-15 13:59:04 +00:00
OnConnected ();
} catch (Exception e) {
2012-07-15 13:59:04 +00:00
this.is_connected = false;
this.is_connecting = false;
2012-07-15 13:59:04 +00:00
if (this.socket != null)
this.socket.Close ();
2016-03-31 08:35:26 +00:00
OnDisconnected (Sparkles.DisconnectReason.TimeOut, e.Message);
2012-07-15 13:59:04 +00:00
return;
}
2012-07-15 13:59:04 +00:00
byte [] bytes = new byte [4096];
int bytes_read = 0;
this.last_ping = DateTime.Now;
2012-07-15 13:59:04 +00:00
// Wait for messages
while (this.is_connected) {
try {
int i = 0;
int timeout = 300;
DisconnectReason reason = DisconnectReason.TimeOut;
// This blocks the thread
2012-07-15 13:59:04 +00:00
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 == timeout) {
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("ListenerTcp", "Pinging " + Server);
2012-07-15 13:59:04 +00:00
byte [] ping_bytes = Encoding.UTF8.GetBytes ("ping\n");
byte [] pong_bytes = new byte [4096];
this.socket.Send (ping_bytes);
if (this.socket.Receive (pong_bytes) < 1)
// 10057 means "Socket is not connected"
throw new SocketException (10057);
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("ListenerTcp", "Received pong from " + Server);
2012-07-15 13:59:04 +00:00
i = 0;
this.last_ping = DateTime.Now;
} else {
// 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 (timeout * 1000 * 1.2),
DateTime.Now
);
if (sleepiness <= 0) {
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("ListenerTcp", "System woke up from sleep");
reason = DisconnectReason.SystemSleep;
2012-07-15 13:59:04 +00:00
// 10057 means "Socket is not connected"
throw new SocketException (10057);
}
2012-07-15 13:59:04 +00:00
}
2012-07-15 13:59:04 +00:00
// The ping failed: disconnect completely
} catch (SocketException e) {
Disconnect(reason, "Ping timeout: " + e.Message);
2012-07-15 13:59:04 +00:00
return;
2011-06-18 21:42:34 +00:00
}
2012-07-15 13:59:04 +00:00
Thread.Sleep (1000);
i++;
}
2012-07-15 13:59:04 +00:00
} catch (Exception) {
return;
}
try {
if (this.socket.Available > 0)
bytes_read = this.socket.Receive (bytes);
2012-02-10 13:26:33 +00:00
// Parse the received message
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
if (!line.Contains ("!"))
continue;
2012-02-01 16:23:49 +00:00
string folder_identifier = line.Substring (0, line.IndexOf ("!"));
string message = CleanMessage (line.Substring (line.IndexOf ("!") + 1));
2012-07-15 13:59:04 +00:00
// We have a message!
if (!folder_identifier.Equals ("debug") && !string.IsNullOrEmpty (message))
2016-03-30 23:56:48 +00:00
OnAnnouncement (new Announcement (folder_identifier, message));
}
} catch (SocketException e) {
Disconnect (DisconnectReason.TimeOut, "Timeout during receiving: " + e.Message);
return;
2011-06-18 21:42:34 +00:00
}
2012-07-15 13:59:04 +00:00
}
});
2011-06-18 21:42:34 +00:00
this.thread.Start ();
}
private void Disconnect (DisconnectReason reason, string message)
{
this.is_connected = false;
this.is_connecting = false;
if (this.socket != null) {
this.socket.Close ();
this.socket = null;
}
OnDisconnected (reason, message);
}
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-01 16:23:49 +00:00
string to_send = "subscribe " + folder_identifier + "\n";
2012-02-01 16:23:49 +00:00
try {
2012-07-03 18:27:42 +00:00
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
this.last_ping = DateTime.Now;
} catch (Exception e) {
2012-02-01 16:23:49 +00:00
this.is_connected = false;
this.is_connecting = false;
OnDisconnected (DisconnectReason.TimeOut, e.Message);
2011-06-18 21:42:34 +00:00
}
}
2016-03-30 23:56:48 +00:00
protected override void AnnounceInternal (Announcement announcement)
2011-06-18 21:42:34 +00:00
{
2012-07-17 13:09:57 +00:00
string to_send = "announce " + announcement.FolderIdentifier + " " + announcement.Message + "\n";
2011-06-18 21:42:34 +00:00
try {
2012-07-03 18:27:42 +00:00
if (this.socket != null)
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
2012-02-01 16:23:49 +00:00
this.last_ping = DateTime.Now;
} catch (Exception e) {
2012-02-01 16:23:49 +00:00
this.is_connected = false;
this.is_connecting = false;
OnDisconnected (DisconnectReason.TimeOut, e.Message);
}
2011-06-18 21:42:34 +00:00
}
public override void Dispose ()
{
if (this.socket != null) {
this.socket.Close ();
this.socket = null;
}
2011-06-18 21:42:34 +00:00
this.thread.Abort ();
this.thread.Join ();
2012-02-01 16:23:49 +00:00
2011-06-18 21:42:34 +00:00
base.Dispose ();
}
2012-02-01 16:23:49 +00:00
private string CleanMessage (string message)
{
2012-07-17 13:09:57 +00:00
message = message.Replace ("\n", "");
message = message.Replace ("\0", "");
return message.Trim ();
2012-02-01 16:23:49 +00:00
}
2011-06-18 21:42:34 +00:00
}
}