示例#1
0
        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);
            }
        }
示例#2
0
 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;
 }
示例#3
0
 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;
 }
示例#4
0
 protected override void OnMemCacheChanged(SocketValue data)
 {
     try
     {
         SocketValue.DataChangedHandler handler;
         if (null != (handler = (SocketValue.DataChangedHandler) this.dataChanged))
         {
             handler(data);
         }
     }
     catch
     {
     }
 }
示例#5
0
 private void OnDataChange(SocketValue data)
 {
     try
     {
         DataChangedHandler handler;
         if (null != (handler = (DataChangedHandler)this.dataChanged))
         {
             handler(data);
         }
     }
     catch (Exception ex)
     {
         Logger.Log(ex);
     }
 }
示例#6
0
 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);
         }
     }
 }
示例#7
0
        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;
            }
        }
示例#8
0
        protected override void OnMemCacheChanged(SocketValue data)
        {
            try
            {
                SocketValue.DataChangedHandler handler;
                if (null != (handler = (SocketValue.DataChangedHandler)this.dataChanged))
                {
                    handler(data);
                }
            }
            catch
            {

            }
        }
示例#9
0
        public void RegisterCallback(string member, SocketValue.DataChangedHandler handler)
        {
            this.Log("RegisterCallback -- " + member);

            lock(this.memCache)
            {
                if (MemberExists(member))
                {
                    this.memCache[member].DataChanged += handler;
                }
            }
        }
示例#10
0
 protected abstract void OnMemCacheChanged(SocketValue data);
示例#11
0
 public void SendValue(SocketValue data, long to)
 {
     this.messageID++;
     SendData(new SocketMessage(this.messageID, data, to));
 }
示例#12
0
        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);
                }
            }
        }
示例#13
0
 public void SendValue(SocketValue data, long to)
 {
     this.messageID++;
     SendData(new SocketMessage(this.messageID, data, to));
 }
示例#14
0
 protected abstract void OnMemCacheChanged(SocketValue data);
示例#15
0
 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);
     }
 }