Skip to content

Commit 35a4527

Browse files
committed
updated file transfer
1 parent 6b1bc59 commit 35a4527

File tree

8 files changed

+169
-77
lines changed

8 files changed

+169
-77
lines changed

Videocall.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ Global
3636
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|ARM64.Build.0 = Release|Any CPU
3737
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|x64.ActiveCfg = Release|Any CPU
3838
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|x64.Build.0 = Release|Any CPU
39-
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|x86.ActiveCfg = Release|x86
40-
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|x86.Build.0 = Release|x86
39+
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|x86.ActiveCfg = Debug|Any CPU
40+
{01361BE3-409B-4B8C-9FDD-875827605743}.Release|x86.Build.0 = Debug|Any CPU
4141
EndGlobalSection
4242
GlobalSection(SolutionProperties) = preSolution
4343
HideSolutionNode = FALSE

Videocall/H264SharpNetCore32.dll

171 KB
Binary file not shown.

Videocall/H264SharpNetCore64.dll

200 KB
Binary file not shown.

Videocall/Services/File Transfer/FileShare.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ internal void ReadBytes()
9393

9494
}
9595

96+
internal void ReadBytesInto(byte[] Data, int off, out int cnt)
97+
{
98+
cnt =0;
99+
if (count == 0)
100+
return;
101+
102+
using (var streamData = new FileStream(seed + FilePath, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read))
103+
{
104+
this.Data = Data;
105+
dataBufferOffset = off;
106+
107+
streamData.Seek(FileStreamStartIdx, SeekOrigin.Begin);
108+
streamData.Read(Data, off, count);
109+
cnt = count;
110+
}
111+
112+
}
113+
96114
internal void ReadBytesInto(PooledMemoryStream stream)
97115
{
98116
if (count == 0)
@@ -146,8 +164,11 @@ internal static FileChunk CreateFromMessage(MessageEnvelope msg)
146164

147165
internal void Release()
148166
{
149-
if (Data != null && canReturnBuffer)
150-
BufferPool.ReturnBuffer(Data);
167+
//if (canReturnBuffer)
168+
// if (Interlocked.Exchange(ref Data, null) !=null)
169+
// {
170+
// BufferPool.ReturnBuffer(Data);
171+
// }
151172
}
152173

153174
internal void ComputeHash()
@@ -325,9 +346,7 @@ public FileChunk HandleFileTransferMessage(MessageEnvelope msg, out string error
325346
if (Hashcode != ftMsg.Hashcode)
326347
{
327348
error = filePath + " is Corrupted";
328-
Console.WriteLine(error);
329349
}
330-
Console.WriteLine(Hashcode);
331350
}
332351
}
333352
else

Videocall/Services/File Transfer/FileTransferStateManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,16 @@ private void HandleCancelledSend(SendState sendState, Completion c)
9090
OnTransferCancelled?.Invoke(sendState, c);
9191
activeStates.TryRemove(sendState.StateId, out _);
9292
services.FileShare.CleanUp(sendState.StateId);
93+
sendState.Cleanup();
9394
}
9495

9596
private void HandleCompletedSend(SendState sendState, Completion completion)
9697
{
9798
OnTransferComplete?.Invoke(sendState, completion);
9899
activeStates.TryRemove(sendState.StateId, out _);
99100
services.FileShare.CleanUp(sendState.StateId);
101+
sendState.Cleanup();
102+
100103
}
101104

102105
private void HandleCancelledReceive(ReceiveState receiveState, Completion c)

Videocall/Services/File Transfer/SendState.cs

Lines changed: 123 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ class SendState : IFileTransferState
3333

3434
private bool forceTCP => services.MessageHandler.FTTransportLayer == "Tcp";
3535
private ServiceHub services => ServiceHub.Instance;
36-
36+
private AutoResetEvent signal = new AutoResetEvent(false);
37+
int exit = 0;
3738
public SendState(string[] files, Guid selectedPeer)
3839
{
3940
AssociatedPeer = selectedPeer;
4041
StateId = Guid.NewGuid();
42+
Thread thread = new Thread(SendRoutine);
43+
thread.Start();
4144
sw = Stopwatch.StartNew();
4245
ThreadPool.UnsafeQueueUserWorkItem((s) =>
4346
ExtractDirectoryTree(files), null);
47+
4448
}
4549

4650
private void ExtractDirectoryTree(string[] files)
@@ -98,89 +102,135 @@ public void Cancel(string why)
98102
{
99103
Cancelled?.Invoke(new Completion() {AdditionalInfo=why, Directory = tree.seed });
100104
}
101-
private readonly object mtex = new object();
102-
private void SendNextChunk()
105+
106+
private void SendRoutine()
103107
{
104-
//try
105-
//{
106-
// if (Interlocked.Increment(ref currentChunk) == fileDatas.Count)
107-
// {
108-
// // done
109-
// Interlocked.Exchange(ref AllSent, 1);
110-
// CheckFinalisation();
111-
// }
112-
// else if (Interlocked.CompareExchange(ref currentChunk, 0, 0) < fileDatas.Count)
113-
// {
114-
// var fileData = fileDatas[currentChunk];
115-
// fileData.ReadBytes();
116-
// CalcHash(fileData);
108+
var buffer = new byte[PersistentSettingConfig.Instance.ChunkSize];
117109

118-
// int numUnacked = Interlocked.Increment(ref unAcked);
110+
while (true)
111+
{
112+
signal.WaitOne();
113+
if (Interlocked.CompareExchange(ref exit, 0, 0) == 1)
114+
return;
119115

120-
// var envelope = fileData.ConvertToMessageEnvelope(out byte[] chunkBuffer);
121-
// envelope.MessageId = StateId;
122-
// services.MessageHandler.SendAsyncMessage(AssociatedPeer, envelope, forceTCP);
116+
while (true)
117+
{
118+
if (Interlocked.CompareExchange(ref exit, 0, 0) == 1)
119+
return;
123120

124-
// PublishStatus(fileData);
125-
// fileData.Release();
121+
var cc = Interlocked.Increment(ref currentChunk);
122+
if (cc == fileDatas.Count)
123+
{
124+
// done
125+
Interlocked.Exchange(ref AllSent, 1);
126+
CheckFinalisation();
127+
break;
128+
}
129+
else if (cc > fileDatas.Count)
130+
break;
131+
132+
var fileData = fileDatas[cc];
133+
fileData.ReadBytesInto(buffer,0,out int cnt);
134+
CalcHash(fileData);
126135

127-
// if (Interlocked.Add(ref totalOnWire, fileData.count) < WindowSize)
128-
// {
129-
// SendNextChunk();
130-
// }
131-
// }
132-
//}
133-
//catch (Exception e)
134-
//{
135-
// CancelExplicit("Exception Occurred : " + e.ToString());
136-
//}
137136

137+
var envelope = fileData.ConvertToMessageEnvelope(out byte[] chunkBuffer);
138+
envelope.MessageId = StateId;
139+
services.MessageHandler.SendAsyncMessage(AssociatedPeer, envelope, forceTCP);
138140

139-
//-------------------------------------------
141+
PublishStatus(fileData);
142+
fileData.Release();
140143

144+
if (Interlocked.Add(ref totalOnWire, fileData.count) > WindowSize)
145+
break;
146+
147+
148+
}
149+
150+
}
151+
}
152+
private readonly object mtex = new object();
153+
private void SendNextChunk()
154+
{
155+
signal.Set();
156+
return;
141157
try
142158
{
143-
144-
while (true)
159+
if (Interlocked.Increment(ref currentChunk) == fileDatas.Count)
160+
{
161+
// done
162+
Interlocked.Exchange(ref AllSent, 1);
163+
CheckFinalisation();
164+
}
165+
else if (Interlocked.CompareExchange(ref currentChunk, 0, 0) < fileDatas.Count)
145166
{
146-
lock (mtex)
167+
var fileData = fileDatas[currentChunk];
168+
fileData.ReadBytes();
169+
CalcHash(fileData);
170+
171+
int numUnacked = Interlocked.Increment(ref unAcked);
172+
173+
var envelope = fileData.ConvertToMessageEnvelope(out byte[] chunkBuffer);
174+
envelope.MessageId = StateId;
175+
services.MessageHandler.SendAsyncMessage(AssociatedPeer, envelope, forceTCP);
176+
177+
PublishStatus(fileData);
178+
fileData.Release();
179+
180+
if (Interlocked.Add(ref totalOnWire, fileData.count) < WindowSize)
147181
{
148-
var cc = Interlocked.Increment(ref currentChunk);
149-
if (cc == fileDatas.Count)
150-
{
151-
// done
152-
Interlocked.Exchange(ref AllSent, 1);
153-
CheckFinalisation();
154-
break;
155-
}
156-
else if (cc > fileDatas.Count)
157-
break;
158-
159-
var fileData = fileDatas[cc];
160-
fileData.ReadBytes();
161-
CalcHash(fileData);
162-
163-
int numUnacked = Interlocked.Increment(ref unAcked);
164-
165-
var envelope = fileData.ConvertToMessageEnvelope(out byte[] chunkBuffer);
166-
envelope.MessageId = StateId;
167-
services.MessageHandler.SendAsyncMessage(AssociatedPeer, envelope, forceTCP);
168-
169-
PublishStatus(fileData);
170-
fileData.Release();
171-
172-
if (Interlocked.Add(ref totalOnWire, fileData.count) > WindowSize)
173-
break;
182+
SendNextChunk();
174183
}
175-
176184
}
177-
178185
}
179186
catch (Exception e)
180187
{
181188
CancelExplicit("Exception Occurred : " + e.ToString());
182189
}
183190

191+
//try
192+
//{
193+
194+
// while (true)
195+
// {
196+
// lock (mtex)
197+
// {
198+
// var cc = Interlocked.Increment(ref currentChunk);
199+
// if (cc == fileDatas.Count)
200+
// {
201+
// // done
202+
// Interlocked.Exchange(ref AllSent, 1);
203+
// CheckFinalisation();
204+
// break;
205+
// }
206+
// else if (cc > fileDatas.Count)
207+
// break;
208+
209+
// var fileData = fileDatas[cc];
210+
// fileData.ReadBytes();
211+
// CalcHash(fileData);
212+
213+
// int numUnacked = Interlocked.Increment(ref unAcked);
214+
215+
// var envelope = fileData.ConvertToMessageEnvelope(out byte[] chunkBuffer);
216+
// envelope.MessageId = StateId;
217+
// services.MessageHandler.SendAsyncMessage(AssociatedPeer, envelope, forceTCP);
218+
219+
// PublishStatus(fileData);
220+
// fileData.Release();
221+
222+
// if (Interlocked.Add(ref totalOnWire, fileData.count) > WindowSize)
223+
// break;
224+
// }
225+
226+
// }
227+
228+
//}
229+
//catch (Exception e)
230+
//{
231+
// CancelExplicit("Exception Occurred : " + e.ToString());
232+
//}
233+
184234
}
185235
//private void SendNextChunk2()
186236
//{
@@ -262,6 +312,7 @@ public void CancelExplicit(string why)
262312
};
263313

264314
services.MessageHandler.SendAsyncMessage(AssociatedPeer, msg, forceTCP);
315+
Cleanup();
265316
}
266317

267318
private void PublishStatus(FileChunk fileData)
@@ -298,7 +349,15 @@ private void HandleCompletion()
298349
response.MessageId = StateId;
299350
response.KeyValuePairs = new Dictionary<string, string>() { { name, null } };
300351
services.MessageHandler.SendAsyncMessage(AssociatedPeer, response, forceTCP);
352+
353+
Interlocked.Exchange(ref exit, 1);
354+
signal.Set();
301355
}
302356

357+
internal void Cleanup()
358+
{
359+
Interlocked.Exchange(ref exit, 1);
360+
signal.Set();
361+
}
303362
}
304363
}

Videocall/Services/Network/MessageHandler.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ public MessageHandler()
6666
private void InitializeClient()
6767
{
6868
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
69-
// var cert = new X509Certificate2(path + "/client.pfx", "greenpass");
70-
client = new RelayClient(0);
69+
X509Certificate2 cert = null;
70+
if (File.Exists(path + "/client.pfx"))
71+
cert = new X509Certificate2(path + "/client.pfx", "greenpass");
72+
client = new RelayClient(cert,0);
7173
// client.MaxUdpPackageSize = 32000;
7274
//client.EnableJumboUdpRateControl = true;
7375

@@ -87,6 +89,9 @@ private void HandleDisconnected()
8789
}
8890
registeredPeers.Clear();
8991
OnDisconnected?.Invoke();
92+
//client.Dispose();
93+
//client = null;
94+
//InitializeClient();
9095
}
9196

9297
private void HandlePeerUnregistered(Guid obj)
@@ -297,9 +302,9 @@ internal void Disconnect()
297302
}
298303
registeredPeers.Clear();
299304
client.Disconnect();
300-
client.Dispose();
301-
client = null;
302-
InitializeClient();
305+
//client.Dispose();
306+
//client = null;
307+
//InitializeClient();
303308
}
304309

305310
internal Task<bool> ConnectAsync(string v1, int v2)

Videocall/Videocall.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@
5353
</PropertyGroup>
5454
<PropertyGroup>
5555
<SignManifests>false</SignManifests>
56-
<SupportedOSPlatformVersion>8.0</SupportedOSPlatformVersion>
56+
<SupportedOSPlatformVersion>10.0.17763.0</SupportedOSPlatformVersion>
5757
</PropertyGroup>
5858
<ItemGroup>
5959
<None Update="client.pfx">
6060
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6161
</None>
62+
<None Update="H264SharpNetCore32.dll">
63+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
64+
</None>
65+
<None Update="H264SharpNetCore64.dll">
66+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
67+
</None>
6268
<None Update="Models\Settings\Settings.json">
6369
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6470
</None>
@@ -157,7 +163,7 @@
157163
</ItemGroup>
158164
<ItemGroup>
159165
<Reference Include="H264SharpNetCore64">
160-
<HintPath>..\..\..\h264 release\64\H264SharpNetCore64.dll</HintPath>
166+
<HintPath>H264SharpNetCore64.dll</HintPath>
161167
</Reference>
162168
<Reference Include="NetworkLibrary">
163169
<HintPath>..\..\NetworkExperiments\Protobuff\bin\Release\net6.0\NetworkLibrary.dll</HintPath>

0 commit comments

Comments
 (0)