Add events to tcp listener

This commit is contained in:
Hylke Bons 2011-07-24 19:22:17 +01:00
parent 7fcd9c96f7
commit dbce0e14e9
3 changed files with 15 additions and 8 deletions

View file

@ -186,7 +186,7 @@ namespace SparkleLib {
public void OnDisconnected () public void OnDisconnected ()
{ {
SparkleHelpers.DebugInfo ("Listener", "Disonnected"); SparkleHelpers.DebugInfo ("Listener", "Disonnected from " + Server);
if (Disconnected != null) if (Disconnected != null)
Disconnected (); Disconnected ();

View file

@ -46,11 +46,12 @@ namespace SparkleLib {
public override bool IsConnected { public override bool IsConnected {
get { get {
//return this.client.IsConnected;
bool result = false; bool result = false;
lock (this.mutex) { lock (this.mutex) {
result = this.connected; result = this.connected;
} }
return result; return result;
} }
} }
@ -70,40 +71,46 @@ namespace SparkleLib {
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) { lock (this.mutex) {
base.is_connecting = false; base.is_connecting = false;
this.connected = true; this.connected = true;
OnConnected ();
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);
this.socket.Send (Encoding.UTF8.GetBytes ("subscribe " + channel + "\n")); this.socket.Send (Encoding.UTF8.GetBytes ("subscribe " + channel + "\n"));
} }
} }
byte [] bytes = new byte [4096]; byte [] bytes = new byte [4096];
// List to the channels, this blocks the thread // List to the channels, this blocks the thread
while (this.socket.Connected) { while (this.socket.Connected) {
int bytes_read = this.socket.Receive (bytes); int bytes_read = this.socket.Receive (bytes);
if (bytes_read > 0) { if (bytes_read > 0) {
string received = Encoding.UTF8.GetString (bytes); string received = Encoding.UTF8.GetString (bytes);
string folder_identifier = received.Substring (0, received.IndexOf ("!")); string folder_identifier = received.Substring (0, received.IndexOf ("!"));
string message = received.Substring (received.IndexOf ("!") + 1); string message = received.Substring (received.IndexOf ("!") + 1);
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message)); OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
} else { } else {
SparkleHelpers.DebugInfo ("ListenerTcp", "Error on socket"); SparkleHelpers.DebugInfo ("ListenerTcp", "Error on socket");
lock (this.mutex) { lock (this.mutex) {
this.socket.Close(); this.socket.Close ();
this.connected = false; this.connected = false;
OnDisconnected ();
} }
} }
} }
SparkleHelpers.DebugInfo ("ListenerTcp", "Disconnected from " + Server.Host); SparkleHelpers.DebugInfo ("ListenerTcp", "Disconnected from " + Server.Host);
// 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);
} }

View file

@ -462,9 +462,9 @@ namespace SparkleLib {
NewChangeSet (change_set); NewChangeSet (change_set);
} }
// There could be changes from a // There could be changes from a resolved
// resolved conflict. Tries only once, // conflict. Tries only once, then lets
//then let the timer try again periodicallly // the timer try again periodically
if (HasUnsyncedChanges) if (HasUnsyncedChanges)
SyncUp (); SyncUp ();