Exemplo n.º 1
0
 private void OnConnect()
 {
     try {
         if (this.Socket_Connect != null)
         {
             this.Socket_Connect(new EventData("connect"));
         }
     } catch (Exception ex) {
         ErrorRecorderHolder.recordError(ex);
     }
 }
Exemplo n.º 2
0
 private void OnError(Exception ex)
 {
     try {
         if (this.Socket_Error != null)
         {
             this.Socket_Error(new EventData("error", ex));
         }
     } catch (Exception e) {
         ErrorRecorderHolder.recordError(e);
     }
 }
Exemplo n.º 3
0
 private void OnError(EventData evd)
 {
     try {
         if (this.Client_Error != null)
         {
             this.Client_Error(evd);
         }
     } catch (Exception ex) {
         ErrorRecorderHolder.recordError(ex);
     }
 }
Exemplo n.º 4
0
        private string GetString(byte[] bytes)
        {
            string str = null;

            try {
                str = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            } catch (Exception ex) {
                ErrorRecorderHolder.recordError(ex);
            }

            return(str);
        }
Exemplo n.º 5
0
        public void RemoveListener(string type)
        {
            if (string.IsNullOrEmpty(type))
            {
                ErrorRecorderHolder.recordError(new Exception("event type is null or empty"));
                return;
            }

            lock (self_locker) {
                this._listeners.Remove(type);
            }
        }
Exemplo n.º 6
0
        private void OnClose()
        {
            try {
                if (this.Socket_Close != null)
                {
                    this.Socket_Close(new EventData("close"));
                }
            } catch (Exception ex) {
                ErrorRecorderHolder.recordError(ex);
            }

            this.Destroy();
        }
Exemplo n.º 7
0
 public void OnSecond(long timestamp)
 {
     try {
         lock (self_locker) {
             if (this._processor != null)
             {
                 this._processor.OnSecond(timestamp);
             }
         }
     } catch (Exception ex) {
         ErrorRecorderHolder.recordError(ex);
     }
 }
Exemplo n.º 8
0
 private void StopServiceThread()
 {
     lock (service_locker) {
         if (service_locker.Status == 1)
         {
             service_locker.Status = 0;
             try {
                 this._serviceEvent.Set();
             } catch (Exception ex) {
                 ErrorRecorderHolder.recordError(ex);
             }
         }
     }
 }
Exemplo n.º 9
0
 private void CallService(ICollection <ServiceDelegate> list)
 {
     foreach (ServiceDelegate service in list)
     {
         try {
             if (service != null)
             {
                 service();
             }
         } catch (Exception ex) {
             ErrorRecorderHolder.recordError(ex);
         }
     }
 }
Exemplo n.º 10
0
        public void StopTaskTimer()
        {
            lock (task_locker) {
                task_locker.Status = 0;

                if (this._taskTimer != null)
                {
                    try {
                        this._taskTimer.Dispose();
                        this._taskTimer = null;
                    } catch (Exception ex) {
                        ErrorRecorderHolder.recordError(ex);
                    }
                }
            }
        }
Exemplo n.º 11
0
        public void StopTimerThread()
        {
            lock (timer_locker) {
                timer_locker.Status = 0;

                if (this._threadTimer != null)
                {
                    try {
                        this._threadTimer.Dispose();
                        this._threadTimer = null;
                    } catch (Exception ex) {
                        ErrorRecorderHolder.recordError(ex);
                    }
                }

                this._secondCalls.Clear();
            }
        }
Exemplo n.º 12
0
        private void TryClose()
        {
            if (socket_locker.Status == SocketStatus.ScoketClosed)
            {
                return;
            }

            if (socket_locker.Count != 0)
            {
                return;
            }

            try {
                this.SocketClose();
            } catch (Exception ex) {
                ErrorRecorderHolder.recordError(ex);
            }
        }
Exemplo n.º 13
0
        public void AddSecond(EventDelegate callback)
        {
            if (callback == null)
            {
                return;
            }

            lock (timer_locker) {
                if (this._secondCalls.Count >= 500)
                {
                    ErrorRecorderHolder.recordError(new Exception("Second Calls Limit!"));
                    return;
                }

                this._secondCalls.Add(callback);
            }

            this.StartTimerThread();
        }
Exemplo n.º 14
0
        private void OnWrite(NetworkStream stream)
        {
            try {
                if (!this._sendEvent.SafeWaitHandle.IsClosed)
                {
                    this._sendEvent.WaitOne();
                }
            } catch (Exception ex) {
                ErrorRecorderHolder.recordError(ex);
            }

            byte[] buffer = new byte[0];

            lock (self_locker) {
                buffer = this._sendQueue.ToArray();
                this._sendQueue.Clear();
            }

            this.WriteSocket(stream, buffer, OnWrite);
        }
Exemplo n.º 15
0
        public void ExecCallback(string key, Exception ex)
        {
            CallbackDelegate cb = null;

            if (this._cbMap.Contains(key))
            {
                lock (this._cbMap) {
                    cb = (CallbackDelegate)this._cbMap[key];
                    this._cbMap.Remove(key);
                }

                ThreadPool.Instance.Execute((state) => {
                    try {
                        cb(new CallbackData(ex));
                    } catch (Exception e) {
                        ErrorRecorderHolder.recordError(e);
                    }
                });
            }
        }
Exemplo n.º 16
0
        public void StartTimerThread()
        {
            lock (timer_locker) {
                if (timer_locker.Status != 0)
                {
                    return;
                }

                timer_locker.Status = 1;

                if (this._threadTimer == null)
                {
                    try {
                        this._threadTimer = new Timer(new TimerCallback(OnSecond), null, 1000, 1000);
                    } catch (Exception ex) {
                        ErrorRecorderHolder.recordError(ex);
                    }
                }
            }
        }
Exemplo n.º 17
0
        private void StartTaskTimer()
        {
            lock (task_locker) {
                if (task_locker.Status != 0)
                {
                    return;
                }

                task_locker.Status = 1;

                if (this._taskTimer == null)
                {
                    try {
                        this._taskTimer = new Timer(new TimerCallback(OnTask), null, Timeout.Infinite, Timeout.Infinite);
                    } catch (Exception ex) {
                        ErrorRecorderHolder.recordError(ex);
                    }
                }
            }
        }
Exemplo n.º 18
0
        public void Close(Exception ex)
        {
            try {
                bool firstClose = false;

                lock (socket_locker) {
                    if (socket_locker.Status == SocketStatus.Normal)
                    {
                        firstClose           = true;
                        socket_locker.Status = SocketStatus.SocketWillClosing;

                        if (ex != null)
                        {
                            this.OnError(ex);
                        }

                        try {
                            this._sendEvent.Set();
                        } catch (Exception e) {
                            ErrorRecorderHolder.recordError(e);
                        }

                        if (this.IsConnecting())
                        {
                            return;
                        }
                    }
                    this.TryClose();
                }

                if (firstClose)
                {
                    FPManager.Instance.DelayTask(200, DelayClose, null);
                }
            } catch (Exception e) {
                ErrorRecorderHolder.recordError(e);
            }
        }
Exemplo n.º 19
0
        private void SocketClose()
        {
            if (this._stream != null)
            {
                this._stream.Close();
                this._stream = null;
            }

            if (this._socket != null)
            {
                this._socket.Close();
                this._socket = null;
            }

            try {
                this._sendEvent.Close();
            } catch (Exception ex) {
                ErrorRecorderHolder.recordError(ex);
            }

            socket_locker.Status = SocketStatus.ScoketClosed;
            this.OnClose();
        }
Exemplo n.º 20
0
        public void FireEvent(EventData evd)
        {
            if (evd == null)
            {
                ErrorRecorderHolder.recordError(new Exception("EventData is null"));
                return;
            }

            ArrayList queue = null;
            string    type  = evd.GetEventType();

            if (string.IsNullOrEmpty(type))
            {
                ErrorRecorderHolder.recordError(new Exception("event type is null or empty"));
                return;
            }

            lock (self_locker) {
                if (!this._listeners.Contains(type))
                {
                    return;
                }

                queue = ((ArrayList)this._listeners[type]);
                IEnumerator ie = queue.GetEnumerator();

                while (ie.MoveNext())
                {
                    EventDelegate callback = (EventDelegate)ie.Current;

                    if (callback != null)
                    {
                        FPManager.Instance.EventTask(callback, evd);
                    }
                }
            }
        }
Exemplo n.º 21
0
        private void AddTimerTask(TimerTask task)
        {
            if (task == null)
            {
                return;
            }

            this.StartTaskTimer();

            lock (task_locker) {
                if (this._timerTaskQueue.Count < 10000)
                {
                    int index = this._timerTaskQueue.Count;

                    for (int i = 0; i < this._timerTaskQueue.Count; i++)
                    {
                        if (task.timestamp < this._timerTaskQueue[i].timestamp)
                        {
                            index = i;
                            break;
                        }
                    }

                    this._timerTaskQueue.Insert(index, task);

                    if (this._timerTaskQueue.Count == 9998)
                    {
                        ErrorRecorderHolder.recordError(new Exception("TimerTask Calls Limit!"));
                    }

                    if (index == 0)
                    {
                        this.TimerOneTask();
                    }
                }
            }
        }
Exemplo n.º 22
0
        private void OnClose(EventData evd)
        {
            this.Close();

            lock (self_locker) {
                this._callback.RemoveCallback();
                this._cry.Clear();
            }

            lock (seq_locker) {
                this._seq = 0;
            }

            try {
                if (this.Client_Close != null)
                {
                    this.Client_Close(new EventData("close"));
                }
            } catch (Exception ex) {
                ErrorRecorderHolder.recordError(ex);
            }

            this.Destroy();
        }
Exemplo n.º 23
0
        private void StartServiceThread()
        {
            lock (service_locker) {
                if (service_locker.Status != 0)
                {
                    return;
                }

                service_locker.Status = 1;

                try {
                    this._serviceThread = new Thread(new ThreadStart(ServiceThread));

                    if (this._serviceThread.Name == null)
                    {
                        this._serviceThread.Name = "FPNN-SERVICE";
                    }

                    this._serviceThread.Start();
                } catch (Exception ex) {
                    ErrorRecorderHolder.recordError(ex);
                }
            }
        }
Exemplo n.º 24
0
        private void WriteSocket(NetworkStream stream, byte[] buffer, Action <NetworkStream> calllback)
        {
            bool needTriggerDelayTask = false;

            lock (socket_locker) {
                if (this._socket == null)
                {
                    return;
                }

                if (!this._socket.Connected)
                {
                    this.Close(null);
                    return;
                }

                if (_appleMobileDeviceInBackground)
                {
                    if (_closeDelayForAppleMobileDeviceInBsckground == 0)
                    {
                        this.Close(null);
                        return;
                    }

                    if (!_delayCloseTriggered)
                    {
                        needTriggerDelayTask = true;
                        _delayCloseTriggered = true;
                    }
                }

                if (socket_locker.Status == SocketStatus.Normal)
                {
                    try {
                        this._sendEvent.Reset();
                    } catch (Exception ex) {
                        ErrorRecorderHolder.recordError(ex);
                    }
                }
                socket_locker.Count++;
            }

            if (needTriggerDelayTask)
            {
                FPManager.Instance.DelayTask(_closeDelayForAppleMobileDeviceInBsckground, BackgroundDelayClose, null);
            }

            try {
                FPSocket self = this;
                stream.BeginWrite(buffer, 0, buffer.Length, (ar) => {
                    try {
                        try {
                            stream.EndWrite(ar);
                        } catch (Exception ex) {
                            self.Close(ex);
                        }

                        lock (socket_locker) {
                            socket_locker.Count--;
                        }

                        if (calllback != null)
                        {
                            calllback(stream);
                        }
                    } catch (Exception ex) {
                        self.Close(ex);
                    }
                }, null);
            } catch (Exception ex) {
                this.Close(ex);
            }
        }
Exemplo n.º 25
0
 public static void recordError(Exception ex)
 {
     ErrorRecorderHolder.getInstance().recordError(ex);
 }