Exemplo n.º 1
0
        private void TcpServer_eventactionReceive(int connectId, byte[] data, int offset, int length)
        {
            if (!_queue.ContainsKey(connectId))
            {
                FileDataContainer container = new FileDataContainer {
                    Length = 0, Data = new List <byte>(), FilePathLenght = 0, FilePath = string.Empty
                };
                _queue.Add(connectId, container);
            }

            byte[] r = new byte[length];
            Buffer.BlockCopy(data, offset, r, 0, length);
            _queue[connectId].Data.AddRange(r);

            bool isContinueGetData = false;

            while (_queue[connectId].Data.Count > 12 && !isContinueGetData)
            {
                byte[] datas = Read(connectId, out isContinueGetData);
                if (datas != null && datas.Length > 0)
                {
                    if (!File.Exists(_queue[connectId].FilePath) || _override)
                    {
                        OnReceiveFile?.Invoke(connectId, _queue[connectId].FilePath);
                        _receivedQueue.Enqueue(new ReceviedFile {
                            Data = datas, FilePath = _queue[connectId].FilePath
                        });

                        //if (FileHelper.WriteFile(datas, queue[connectId].FilePath))
                        //{
                        //    OnReceiveFile?.Invoke(connectId, queue[connectId].FilePath);
                        //}
                        //else
                        //    OnError?.Invoke($"Received file success, but write file failed:{queue[connectId].FilePath}");
                    }
                    else
                    {
                        OnError?.Invoke($"Received file success, but file exist:{_queue[connectId].FilePath}");
                    }
                    _queue[connectId].InitData();
                }
            }
        }
Exemplo n.º 2
0
        public FileClient(uint headerFlag = 0xff, bool isOverride = false)
        {
            _override = isOverride;
            if (headerFlag < 0 || headerFlag > 1023)
            {
                headerFlag = 0;
            }
            this._headerFlag = headerFlag;
            Thread thread = new Thread(new ThreadStart(() =>
            {
                _queue                 = new FileDataContainer();
                _tcpClients            = new TcpClients(1024);
                _tcpClients.OnConnect += TCPClients_OnConnect;
                _tcpClients.OnReceive += TCPClients_OnReceive;
                _tcpClients.OnClose   += TCPClients_OnClose;
                _tcpClients.OnSend    += TCPClients_OnSend;
            }));

            thread.IsBackground = true;
            thread.Start();
            _recievedThread = new Thread(HandleMessageThread);
            _recievedThread.IsBackground = true;
            _recievedThread.Start();
        }