SparkleShare/SparkleLib/SparkleListenerTcp.cs

224 lines
7.1 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;
using System.IO;
2011-06-18 21:42:34 +00:00
using System.Text;
using System.Threading;
2012-02-01 16:23:49 +00:00
using System.Net.NetworkInformation;
2011-06-18 21:42:34 +00:00
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Xml.Serialization;
2011-06-18 21:42:34 +00:00
namespace SparkleLib {
public class SparkleListenerTcp : SparkleListenerBase {
2012-02-01 16:23:49 +00:00
private Socket socket;
private Object socket_lock = new Object ();
2011-06-18 21:42:34 +00:00
private Thread thread;
2012-02-01 16:23:49 +00:00
private bool is_connected = false;
private bool is_connecting = false;
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 ()
{
SparkleHelpers.DebugInfo ("ListenerTcp", "Connecting to " + Server.Host);
2011-06-18 21:42:34 +00:00
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
2012-02-01 16:23:49 +00:00
// TODO: our own time comparison to account for system sleep?
this.socket.ReceiveTimeout = 30 * 1000;
this.socket.Blocking = true;
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 ();
foreach (string channel in base.channels) {
2012-02-01 16:23:49 +00:00
SparkleHelpers.DebugInfo ("ListenerTcp",
"Subscribing to channel " + channel);
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;
// List to the channels, this blocks the thread
while (this.socket.Connected) {
try {
bytes_read = this.socket.Receive (bytes);
} catch (Exception e) {
if (!PingHost (Server.Host)) {
lock (this.socket_lock) {
2011-07-24 18:22:17 +00:00
this.socket.Close ();
2012-02-01 16:23:49 +00:00
this.is_connected = false;
2011-07-24 18:22:17 +00:00
2012-02-01 16:23:49 +00:00
OnDisconnected (e.Message);
2011-06-28 21:13:28 +00:00
}
2011-06-18 21:42:34 +00:00
}
}
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)) {
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
}
}
2011-06-18 21:42:34 +00:00
}
2012-02-01 16:23:49 +00:00
OnDisconnected ("");
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) {
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", "");
}
private bool PingHost (string host)
{
Ping ping = new Ping ();
PingOptions options = new PingOptions () {
DontFragment = true
};
string data = "00000000000000000000000000000000";
byte [] buffer = Encoding.ASCII.GetBytes (data);
PingReply reply = ping.Send (host, 15, buffer, options);
return reply.Status == IPStatus.Success;
}
2011-06-18 21:42:34 +00:00
}
}