Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
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);
                }
            });
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
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);
            }
        }