Example #1
0
        private void OnWrite()
        {
            this._sendEvent.WaitOne();

            if (this._isClosed)
            {
                return;
            }

            ArrayList tmpQueue = new ArrayList();

            lock (this._sendQueue) {
                for (int i = 0; i < this._sendQueue.Count; i++)
                {
                    for (int j = 0; j < ((byte[])this._sendQueue[i]).Length; j++)
                    {
                        tmpQueue.Add(((byte[])this._sendQueue[i])[j]);
                    }
                }

                this._sendQueue.Clear();
                this._sendEvent.Reset();
            }

            byte[] buffer = this.GetBytes(tmpQueue);

            try {
                if (buffer != null && buffer.Length > 0)
                {
                    FPSocket self = this;

                    this._stream.BeginWrite(buffer, 0, buffer.Length, (ar) => {
                        try {
                            self._stream.EndWrite(ar);
                            self.OnWrite();
                        } catch (Exception ex) {
                            self.Close(ex);
                        }
                    }, null);
                }
                else
                {
                    this.OnWrite();
                }
            } catch (Exception ex) {
                this.Close(ex);
            }
        }
Example #2
0
        protected void Init(string host, int port, int connectionTimeout)
        {
            this._pkg      = new FPPackage();
            this._cry      = new FPEncryptor(_pkg);
            this._psr      = new FPProcessor();
            this._callback = new FPCallback();

            if (connectionTimeout <= 0)
            {
                connectionTimeout = 30 * 1000;
            }

            FPManager.Instance.AddSecond(OnSecondDelegate);
            this._sock = new FPSocket(OnData, host, port, connectionTimeout);
            this._sock.Socket_Connect = this.OnConnect;
            this._sock.Socket_Close   = this.OnClose;
            this._sock.Socket_Error   = this.OnError;
        }
        protected void Init(string host, int port, bool autoReconnect, int connectionTimeout)
        {
            this._pkg      = new FPPackage();
            this._cyr      = new FPEncryptor(_pkg);
            this._event    = new FPEvent();
            this._psr      = new FPProcessor();
            this._callback = new FPCallback();

            if (connectionTimeout > 0)
            {
                this._timeout = connectionTimeout;
            }

            this._autoReconnect = autoReconnect;

            FPClient self = this;

            this._eventDelegate = (evd) => {
                self.OnSecond(evd.GetTimestamp());
            };

            ThreadPool.Instance.Event.AddListener("second", this._eventDelegate);

            this._sock = new FPSocket((stream) => {
                self.OnData(stream);
            }, host, port, this._timeout);

            this._sock.GetEvent().AddListener("connect", (evd) => {
                self.OnConnect();
            });

            this._sock.GetEvent().AddListener("close", (evd) => {
                self.OnClose();
            });

            this._sock.GetEvent().AddListener("error", (evd) => {
                self.OnError(evd.GetException());
            });
        }
Example #4
0
        public void Open()
        {
            if (String.IsNullOrEmpty(this._host))
            {
                this.OnError(new Exception("Cannot open null host"));
                return;
            }

            if (this._port <= 0)
            {
                this.OnError(new Exception("Cannot open without port"));
                return;
            }

            if (this._socket != null && (this.IsOpen() || this.IsConnecting()))
            {
                this.OnError(new Exception("has been connect!"));
                return;
            }

            this._isClosed = false;
            this._socket   = new TcpClient();

            FPSocket self = this;

            ThreadPool.Instance.Execute((state) => {
                IAsyncResult result;

                try {
                    result = self._socket.BeginConnect(self._host, self._port, null, null);

                    lock (self._lock_obj) {
                        self._isConnecting = true;
                    }

                    var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds((double)self._timeout));

                    if (!success)
                    {
                        lock (self._lock_obj) {
                            self._isConnecting = false;
                        }

                        self.Close(new Exception("Connect Timeout"));
                        return;
                    }

                    self._socket.EndConnect(result);
                    self._stream = self._socket.GetStream();

                    lock (self._lock_obj) {
                        self._isConnecting = false;
                    }

                    self.StartWriteThread();
                    self.OnRead(self._stream);

                    self.OnConnect();
                } catch (Exception ex) {
                    lock (self._lock_obj) {
                        self._isConnecting = false;
                    }

                    self.Close(ex);
                }
            });
        }
Example #5
0
 public static void AppleMobileDeviceSwitchToBackground(bool background)
 {
     FPSocket.AppleMobileDeviceSwitchToBackground(background);
 }
        public void ReadSocket(NetworkStream stream, byte[] buffer, int rlen, Action <byte[], int> 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;
                    }
                }

                socket_locker.Count++;
            }

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

            try {
                FPSocket self = this;
                stream.BeginRead(buffer, rlen, buffer.Length - rlen, (ar) => {
                    try {
                        int len = 0;

                        try {
                            len = stream.EndRead(ar);
                        } catch (Exception ex) {
                            self.Close(ex);
                        }

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

                        if (len == 0)
                        {
                            self.Close(null);
                        }
                        else
                        {
                            rlen += len;

                            if (calllback != null)
                            {
                                calllback(buffer, rlen);
                            }
                        }
                    } catch (Exception ex) {
                        self.Close(ex);
                    }
                }, null);
            } catch (Exception ex) {
                this.Close(ex);
            }
        }
        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);
            }
        }