Example #1
0
        public FilePacket ReceivePacket(byte[] rawBytes, out byte[] extra)
        {
            extra = rawBytes;

            FilePacket p = new FilePacket();

            if (rawBytes.Length < 4)
            {
                return(null);                     //| 4Bytes: Packet Size
            }
            int length = BitConverter.ToInt32(rawBytes, 0);

            if (length > rawBytes.Length || length < 4)
            {
                return(null);
            }

            using (MemoryStream stream = new MemoryStream(rawBytes, 4, length - 4))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    try
                    {
                        p.ReadPacket(reader);
                    }
                    catch
                    {
                        return(null);
                    }
                }

            extra = new byte[rawBytes.Length - length];
            Buffer.BlockCopy(rawBytes, length, extra, 0, rawBytes.Length - length);

            return(p);
        }
Example #2
0
 public void Enqueue(FilePacket p)
 {
     if (_sendList != null && p != null)
     {
         lock (_sendList)
         {
             _sendList.Enqueue(p);
         }
     }
 }
Example #3
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);
         }
     }
 }
Example #4
0
 public void ProcessPacket(FilePacket p)
 {
     //OutputLog(p.ToString());
     if (p.TypeID == CMD.S2C_Error || p.TypeID == CMD.S2C_End)
     {
         OutputLog(p.Token);
         Disconnect();
         return;
     }
     if (p.TypeID == CMD.S2C_Current)
     {
         Notify(string.Format("{0}", p.Position / 1024));
         if (p.Position == fileLength)
         {
             OutputLog("上传完毕...");
             Enqueue(new FilePacket {
                 TypeID = CMD.C2S_End
             });
             Disconnect();
         }
         return;
     }
     if (p.TypeID == CMD.S2C_Start)
     {
         if (p.Position != 0 && p.Position < fileLength)
         {
             filePosition = p.Position;
             fileStream.Seek(p.Position, SeekOrigin.Begin);
         }
         if (p.Position != 0 && p.Position == fileLength)
         {
             Enqueue(new FilePacket {
                 TypeID = CMD.C2S_End
             });
             Disconnect();
             return;
         }
         new Thread(() =>
         {
             while (Connected)
             {
                 FilePacket datapkg = new FilePacket {
                     TypeID = CMD.C2S_Data
                 };
                 if (fileLength - filePosition < 4096)
                 {
                     datapkg.Data    = reader.ReadBytes((int)(fileLength - filePosition));
                     datapkg.DataLen = (int)(fileLength - filePosition);
                 }
                 else
                 {
                     datapkg.Data    = reader.ReadBytes(4096);
                     datapkg.DataLen = 4096;
                 }
                 filePosition = filePosition + datapkg.DataLen;
                 Enqueue(datapkg);
                 Thread.Sleep(1);
             }
         })
         {
             IsBackground = true
         }.Start();
     }
 }
Example #5
0
        private void ProcessPacket(FilePacket p)
        {
            if (p.TypeID == CMD.C2S_Init)
            {
                OutputLog(p.ToString());
                if (!File.Exists(@"./tokens/" + p.Token))
                {
                    Enqueue(new FilePacket {
                        TypeID = CMD.S2C_Error, Token = "未登陆验证"
                    });
                    OutputLog("非法客户端: " + _client.Client.RemoteEndPoint.ToString());
                    Disconnect();
                    return;
                }
                isLogin    = true;
                filePath   = recvFilePath + "\\" + p.FileName;
                fileName   = p.FileName;
                fileLength = p.FileLen;
                if (File.Exists(filePath))
                {
                    if (File.Exists(filePath + ".cfg"))
                    {
                        using (FileStream fs = new FileStream(filePath + ".cfg", FileMode.Open))
                            using (BinaryReader reader = new BinaryReader(fs))
                            {
                                if (reader.ReadInt64() != fileLength)
                                {
                                    //已存在同名的文件,拒绝接收
                                    Enqueue(new FilePacket {
                                        TypeID = CMD.S2C_Error, Token = "已存在同名的文件,服务器拒绝接收"
                                    });
                                    Disconnect();
                                    return;
                                }
                                filePosition = reader.ReadInt64();
                            }
                        fileStream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
                        fileStream.Seek(filePosition, SeekOrigin.Begin);
                        writer = new BinaryWriter(fileStream);
                        Enqueue(new FilePacket {
                            TypeID = CMD.S2C_Start, Position = filePosition
                        });
                        return;
                    }
                    //已存在同名同大小的文件,拒绝接收
                    Enqueue(new FilePacket {
                        TypeID = CMD.S2C_Error, Token = "已存在同名的文件,服务器拒绝接收"
                    });
                    Disconnect();
                    return;
                }
                else
                {
                    fileStream = File.Create(filePath);
                    writer     = new BinaryWriter(fileStream);
                    Enqueue(new FilePacket {
                        TypeID = CMD.S2C_Start
                    });
                }
                return;
            }
            if (!isLogin)
            {
                Enqueue(new FilePacket {
                    TypeID = CMD.S2C_Error, Token = "未登陆验证"
                });
                OutputLog("非法客户端: " + _client.Client.RemoteEndPoint.ToString());
                Disconnect();
                return;
            }
            if (p.TypeID == CMD.C2S_Data && p.DataLen > 0)
            {
                writer.Write(p.Data);
                filePosition += p.DataLen;
                if (filePosition == fileLength)
                {
                    _finished = true;
                }
            }

            if (p.TypeID == CMD.C2S_End && filePosition == fileLength)
            {
                OutputLog(p.ToString());
                _finished = true;
                if (File.Exists(filePath + ".cfg"))
                {
                    File.Delete(filePath + ".cfg");
                }
            }
        }
Example #6
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();
        }