SparkleShare/SparkleLib/SparkleListenerTcp.cs

227 lines
7.7 KiB
C#
Raw Normal View History

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;
private Object socket_lock = new Object ();
private bool is_connected = false;
private bool is_connecting = false;
private int receive_timeout = 180 * 1000;
2011-06-18 21:42:34 +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-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,
SocketType.Stream, ProtocolType.Tcp) {
2011-07-24 18:22:17 +00:00
ReceiveTimeout = this.receive_timeout,
SendTimeout = 10 * 1000
};
// Try to connect to the server
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 ();
// Subscribe to channels of interest to us
foreach (string channel in base.channels) {
2012-02-01 16:23:49 +00:00
SparkleHelpers.DebugInfo ("ListenerTcp",
"Subscribing to channel " + channel + " on " + Server);
2012-02-01 16:23:49 +00:00
byte [] subscribe_bytes =
Encoding.UTF8.GetBytes ("subscribe " + channel + "\n");
this.socket.Send (subscribe_bytes);
}
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;
OnDisconnected (e.Message);
return;
}
byte [] bytes = new byte [4096];
int bytes_read = 0;
// Wait for messages
2012-02-01 22:17:43 +00:00
while (this.is_connected) {
2012-02-01 16:23:49 +00:00
try {
// This blocks the thread
2012-02-01 16:23:49 +00:00
bytes_read = this.socket.Receive (bytes);
// We've timed out, let's ping the server to
// see if the connection is still up
} catch (SocketException) {
try {
byte [] ping_bytes = Encoding.UTF8.GetBytes ("ping\n");
byte [] ping_back_bytes = new byte [4096];
SparkleHelpers.DebugInfo ("ListenerTcp", "Pinging " + Server);
2012-02-05 21:28:06 +00:00
lock (this.socket_lock)
this.socket.Send (ping_bytes);
this.socket.ReceiveTimeout = 10 * 1000;
2012-02-05 21:28:06 +00:00
// 10057 means "Socket is not connected"
if (this.socket.Receive (ping_back_bytes) < 1)
throw new SocketException (10057);
2012-02-05 21:28:06 +00:00
SparkleHelpers.DebugInfo ("ListenerTcp", "Received pong from " + Server);
this.socket.ReceiveTimeout = this.receive_timeout;
// The ping failed: disconnect completely
} catch (SocketException) {
this.is_connected = false;
this.is_connecting = false;
2011-07-24 18:22:17 +00:00
this.socket.ReceiveTimeout = this.receive_timeout;
OnDisconnected ("Ping timeout");
return;
2011-06-18 21:42:34 +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-01 16:23:49 +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));
if (!folder_identifier.Equals ("debug") &&
!String.IsNullOrEmpty (message)) {
// 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-01 16:23:49 +00:00
protected override void AlsoListenTo (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 {
lock (this.socket_lock)
2012-02-01 16:23:49 +00:00
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
2012-02-01 16:23:49 +00:00
} catch (SocketException e) {
this.is_connected = false;
this.is_connecting = false;
2012-02-01 16:23:49 +00:00
OnDisconnected (e.Message);
2011-06-18 21:42:34 +00:00
}
}
2012-02-01 16:23:49 +00:00
protected override void Announce (SparkleAnnouncement announcement)
2011-06-18 21:42:34 +00:00
{
string to_send = "announce " + announcement.FolderIdentifier
+ " " + announcement.Message + "\n";
2011-06-18 21:42:34 +00:00
try {
2012-02-01 16:23:49 +00:00
lock (this.socket_lock)
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
2012-02-01 16:23:49 +00:00
} catch (SocketException e) {
2012-02-01 16:23:49 +00:00
this.is_connected = false;
this.is_connecting = false;
2012-02-01 16:23:49 +00:00
OnDisconnected (e.Message);
}
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
2011-06-18 21:42:34 +00:00
base.Dispose ();
}
2012-02-01 16:23:49 +00:00
private string CleanMessage (string message)
{
2012-02-01 16:23:49 +00:00
return message.Trim ()
.Replace ("\n", "")
.Replace ("\0", "");
}
2011-06-18 21:42:34 +00:00
}
}