public SocketClient(SocketServer parent, TcpClient socket, long clientID, ErrorLogger errorLog, FileLogger debugLog) : base(errorLog, debugLog) { try { this.encoding = Encoding.ASCII; this.parent = parent; this.ID = clientID; this.socket = socket; this.stream = this.socket.GetStream(); this.connected = true; this.connectionLoopThread = new Thread(this.ConnectionLoop); this.outgoingLoopThread = new Thread(this.OutgoingLoop); this.incomingLoopThread = new Thread(this.IncomingLoop); this.pingLoopThread = new Thread(this.PingLoop); this.connectionLoopThread.Name = "SocketClient - ConnectionLoop"; this.outgoingLoopThread.Name = "SocketClient - OutgoingLoop"; this.incomingLoopThread.Name = "SocketClient - IncomingLoop"; this.pingLoopThread.Name = "SocketClient - PingLoop"; this.connectionLoopThread.IsBackground = true; this.outgoingLoopThread.IsBackground = true; this.incomingLoopThread.IsBackground = true; this.pingLoopThread.IsBackground = true; this.Log("Host Constructor"); this.messagesOut = new List <SocketMessage>(); this.messagesIn = new List <SocketMessage>(); // connected this.initilized = true; this.OnConnectionChange(true); this.outgoingLoopThread.Start(); this.incomingLoopThread.Start(); this.connectionLoopThread.Start(); // send clientID this.SendData("set", "Me.ID", clientID.ToString(), this.ID); // send all values lock (parent.MemCache) { foreach (KeyValuePair <string, SocketValue> entry in parent.MemCache) { SocketValue val = entry.Value; this.SendValue(entry.Value, this.ID); } } // send initilization code to client this.SendData("startup", this.ID); } catch (Exception ex) { this.Log(ex); } }
public SocketMessage(long messageID, SocketValue data, long destinationID) { this.command = "set"; this.id = messageID; this.member = data.Name; this.data = data.Data; this.fromID = data.SourceID; this.toID = destinationID; this.timestamp = data.Timestamp; }
protected override void OnMemCacheChanged(SocketValue data) { try { SocketValue.DataChangedHandler handler; if (null != (handler = (SocketValue.DataChangedHandler) this.dataChanged)) { handler(data); } } catch { } }
private void OnDataChange(SocketValue data) { try { DataChangedHandler handler; if (null != (handler = (DataChangedHandler)this.dataChanged)) { handler(data); } } catch (Exception ex) { Logger.Log(ex); } }
private void OnDataChange(SocketValue data) { try { DataChangedHandler handler; if (null != (handler = (DataChangedHandler)this.dataChanged)) { handler(data); } } catch (Exception ex) { if (this.errorLog != null) { this.errorLog.Write(ex); } } }
internal void ProcessMessage(SocketClient client, SocketMessage message) { if (message != null) { switch (message.Command.ToLower()) { case "set": this.Store(message); if (message.ToID == -1) { this.Broadcast(message); } break; case "get": lock (this.memCache) { client.SendValue(this.Get(message.Member), -1); } break; case "get-all": lock (this.memCache) { foreach (KeyValuePair <string, SocketValue> entry in this.memCache) { SocketValue val = entry.Value; client.SendValue(entry.Value, message.FromID); } } break; default: break; } message = null; } }
protected override void OnMemCacheChanged(SocketValue data) { try { SocketValue.DataChangedHandler handler; if (null != (handler = (SocketValue.DataChangedHandler)this.dataChanged)) { handler(data); } } catch { } }
public void RegisterCallback(string member, SocketValue.DataChangedHandler handler) { this.Log("RegisterCallback -- " + member); lock(this.memCache) { if (MemberExists(member)) { this.memCache[member].DataChanged += handler; } } }
protected abstract void OnMemCacheChanged(SocketValue data);
public void SendValue(SocketValue data, long to) { this.messageID++; SendData(new SocketMessage(this.messageID, data, to)); }
private void DataChanged(SocketValue data) { lock (this.controlLock) { try { if (this.memCache == null) return; this.updatesAvailable = true; } catch (System.ObjectDisposedException) { } catch (Exception ex) { if (this.errorLog != null) this.errorLog.Write(ex); } } }
private void OnDataChange(SocketValue data) { try { DataChangedHandler handler; if (null != (handler = (DataChangedHandler)this.dataChanged)) { handler(data); } } catch (Exception ex) { if (this.errorLog != null) this.errorLog.Write(ex); } }