Пример #1
0
 public void Enqueue(FilePacket p, bool immediately = false)
 {
     if (immediately)
     {
         List <byte> data = new List <byte>();
         data.AddRange(p.GetPacketBytes());
         BeginSend(data);
     }
     if (_sendList != null && p != null)
     {
         lock (_sendList)
         {
             _sendList.Enqueue(p);
         }
     }
 }
Пример #2
0
        public ServerConnection(TcpClient client, string _recvFilePath)
        {
            _client         = client;
            _client.NoDelay = true;
            _connected      = true;
            recvFilePath    = _recvFilePath;
            OutputLog(_client.Client.RemoteEndPoint.ToString() + " 已连接 ");

            new Thread(() =>
            {
                while (_connected && !_finished)
                {
                    try
                    {
                        while (_recvList != null && !_recvList.IsEmpty)
                        {
                            FilePacket p;
                            if (!_recvList.TryDequeue(out p))
                            {
                                continue;
                            }
                            ProcessPacket(p);
                        }
                        if (_sendList == null || _sendList.Count <= 0)
                        {
                            continue;
                        }
                        List <byte> data = new List <byte>();
                        while (_sendList.Count > 0)
                        {
                            FilePacket p = _sendList.Dequeue();
                            if (p == null)
                            {
                                continue;
                            }
                            data.AddRange(p.GetPacketBytes());
                        }
                        BeginSend(data);
                    }
                    catch (Exception ex)
                    {
                        Notify(ex);
                    }
                    Enqueue(new FilePacket {
                        TypeID = CMD.S2C_Current, Position = filePosition
                    });
                    Thread.Sleep(1);
                }

                if (fileStream != null && writer != null)
                {
                    OutputLog(string.Format("文件 {0} 在 {1} 中断开了", fileName, filePosition));
                    Enqueue(new FilePacket {
                        TypeID = CMD.S2C_Current, Position = filePosition
                    }, true);

                    writer.Flush();
                    fileStream.Flush();
                    writer.Close();
                    writer.Dispose();
                    fileStream.Close();
                    fileStream.Dispose();
                    writer     = null;
                    fileStream = null;
                    if (filePosition == fileLength)
                    {
                        OutputLog(string.Format("{0} 已接收完毕...", fileName));
                        if (File.Exists(filePath + ".cfg"))
                        {
                            File.Delete(filePath + ".cfg");
                        }
                    }
                    else
                    {
                        using (FileStream fs = new FileStream(filePath + ".cfg", FileMode.Create))
                            using (BinaryWriter wr = new BinaryWriter(fs))
                            {
                                wr.Write(fileLength);
                                wr.Write(filePosition);
                            }
                    }
                }
                Disconnect();
            })
            {
                IsBackground = true
            }.Start();

            BeginReceive();
        }