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 ()
{
SparkleHelpers.DebugInfo ("Listener", "Disonnected");
SparkleHelpers.DebugInfo ("Listener", "Disonnected from " + Server);
if (Disconnected != null)
Disconnected ();

View file

@ -46,11 +46,12 @@ namespace SparkleLib {
public override bool IsConnected {
get {
//return this.client.IsConnected;
bool result = false;
lock (this.mutex) {
result = this.connected;
}
return result;
}
}
@ -70,40 +71,46 @@ namespace SparkleLib {
int port = Server.Port;
if (port < 0) port = 9999;
this.socket.Connect (Server.Host, port);
lock (this.mutex) {
base.is_connecting = false;
this.connected = true;
OnConnected ();
foreach (string channel in base.channels) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
this.socket.Send (Encoding.UTF8.GetBytes ("subscribe " + channel + "\n"));
}
}
byte [] bytes = new byte [4096];
// List to the channels, this blocks the thread
while (this.socket.Connected) {
int bytes_read = this.socket.Receive (bytes);
if (bytes_read > 0) {
string received = Encoding.UTF8.GetString (bytes);
string folder_identifier = received.Substring (0, received.IndexOf ("!"));
string message = received.Substring (received.IndexOf ("!") + 1);
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
} else {
SparkleHelpers.DebugInfo ("ListenerTcp", "Error on socket");
lock (this.mutex) {
this.socket.Close();
this.socket.Close ();
this.connected = false;
OnDisconnected ();
}
}
}
SparkleHelpers.DebugInfo ("ListenerTcp", "Disconnected from " + Server.Host);
// TODO: attempt to reconnect..?
} catch (SocketException e) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
}

View file

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