-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFileTransportProtocol.cs
More file actions
64 lines (54 loc) · 1.97 KB
/
Copy pathFileTransportProtocol.cs
File metadata and controls
64 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// This file is part of TorrentCore.
// https://torrentcore.org
// Copyright (c) Samuel Fisher.
//
// Licensed under the GNU Lesser General Public License, version 3. See the
// LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TorrentCore.Application.BitTorrent;
using TorrentCore.Transport;
namespace CustomTransportProtocol
{
class FileTransportProtocol : ITransportProtocol
{
private readonly DirectoryInfo _rootDir;
private readonly PeerId _localPeerId;
private readonly List<ITransportStream> _streams;
private FileSystemWatcher _fileWatcher;
public FileTransportProtocol(DirectoryInfo rootDir, PeerId localPeerId)
{
_rootDir = rootDir;
_localPeerId = localPeerId;
_streams = new List<ITransportStream>();
}
public event Action<AcceptConnectionEventArgs> AcceptConnectionHandler;
public IEnumerable<ITransportStream> Streams => _streams;
public void Start()
{
var inDir = Path.Combine(_rootDir.FullName, _localPeerId.ToString());
Directory.CreateDirectory(inDir);
_fileWatcher = new FileSystemWatcher(inDir);
_fileWatcher.Created += FileWatcher_Created;
}
private void FileWatcher_Created(object sender, FileSystemEventArgs e)
{
var transportStream = new FileTransportStream(
new DirectoryInfo(e.FullPath),
new DirectoryInfo(Path.Combine(_rootDir.FullName, e.Name, _localPeerId.ToString())));
AcceptConnectionHandler?.Invoke(new AcceptConnectionEventArgs(transportStream, () =>
{
_streams.Add(transportStream);
}));
}
public void Stop()
{
foreach (var stream in Streams)
{
stream.Disconnect();
}
}
}
}