Make tcp listener startup-able
This commit is contained in:
parent
c411c85589
commit
b8b6b86ce5
|
@ -65,7 +65,7 @@ namespace SparkleLib {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (SparkleListenerIrc) listeners [listeners.Count - 1];
|
return (SparkleListenerBase) listeners [listeners.Count - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,13 +26,18 @@ namespace SparkleLib {
|
||||||
public class SparkleListenerTcp : SparkleListenerBase {
|
public class SparkleListenerTcp : SparkleListenerBase {
|
||||||
|
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
|
// these are shared
|
||||||
|
private readonly Object mutex = new Object();
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
private bool connected;
|
||||||
|
|
||||||
public SparkleListenerTcp (Uri server, string folder_identifier) :
|
public SparkleListenerTcp (Uri server, string folder_identifier) :
|
||||||
base (server, folder_identifier)
|
base (server, folder_identifier)
|
||||||
{
|
{
|
||||||
base.channels.Add (folder_identifier);
|
base.channels.Add (folder_identifier);
|
||||||
this.socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
this.socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
this.connected = false;
|
||||||
/*
|
/*
|
||||||
this.client.OnConnected += delegate {
|
this.client.OnConnected += delegate {
|
||||||
base.is_connecting = false;
|
base.is_connecting = false;
|
||||||
|
@ -60,7 +65,11 @@ namespace SparkleLib {
|
||||||
public override bool IsConnected {
|
public override bool IsConnected {
|
||||||
get {
|
get {
|
||||||
//return this.client.IsConnected;
|
//return this.client.IsConnected;
|
||||||
return true;
|
bool result = false;
|
||||||
|
lock (this.mutex) {
|
||||||
|
result = this.connected;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,18 +84,20 @@ namespace SparkleLib {
|
||||||
this.thread = new Thread (
|
this.thread = new Thread (
|
||||||
new ThreadStart (delegate {
|
new ThreadStart (delegate {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Connect and subscribe to the channel
|
// Connect and subscribe to the channel
|
||||||
int port = Server.Port;
|
int port = Server.Port;
|
||||||
if (port < 0) port = 9999;
|
if (port < 0) port = 9999;
|
||||||
this.socket.Connect (Server.Host, port);
|
this.socket.Connect (Server.Host, port);
|
||||||
|
lock (this.mutex) {
|
||||||
base.is_connecting = false;
|
base.is_connecting = false;
|
||||||
|
this.connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (string channel in base.channels) {
|
foreach (string channel in base.channels) {
|
||||||
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
|
||||||
|
|
||||||
byte [] message = Encoding.UTF8.GetBytes (
|
byte [] message = Encoding.UTF8.GetBytes (
|
||||||
"{\"folder\": \"" + channel + "\", \"command\": \"subscribe\"}");
|
"{\"repo\": \"" + channel + "\", \"command\": \"subscribe\"}");
|
||||||
this.socket.Send (message);
|
this.socket.Send (message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,19 +105,21 @@ namespace SparkleLib {
|
||||||
|
|
||||||
// List to the channels, this blocks the thread
|
// List to the channels, this blocks the thread
|
||||||
while (this.socket.Connected) {
|
while (this.socket.Connected) {
|
||||||
this.socket.Receive (bytes);
|
int bytes_read = this.socket.Receive (bytes);
|
||||||
if (bytes != null && bytes.Length > 0) {
|
if (bytes_read > 0) {
|
||||||
Console.WriteLine (Encoding.UTF8.GetString (bytes));
|
Console.WriteLine (Encoding.UTF8.GetString (bytes));
|
||||||
|
|
||||||
string received_message = bytes.ToString ().Trim ();
|
string received_message = bytes.ToString ().Trim ();
|
||||||
string folder_id = ""; // TODO: parse message, use XML
|
string folder_id = ""; // TODO: parse message, use XML
|
||||||
OnAnnouncement (new SparkleAnnouncement (folder_id, received_message));
|
OnAnnouncement (new SparkleAnnouncement (folder_id, received_message));
|
||||||
}
|
} else {
|
||||||
}
|
lock (this.mutex) {
|
||||||
|
|
||||||
// Disconnect when we time out
|
|
||||||
this.socket.Close();
|
this.socket.Close();
|
||||||
|
this.connected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: attempt to reconnect..?
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue