-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
212 lines (161 loc) · 7.47 KB
/
Program.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Runtime.InteropServices;
using Windows.Networking.Proximity;
using System.IO;
namespace PeerFinderConsole
{
class Program
{
[STAThread]
static void Main(string[] args)
{
SetCurrentProcessExplicitAppUserModelID("PeerFinderApp");
if (args[0] == "host")
{
HostRole host = new HostRole();
host.Run();
return;
}
ClientRole client = new ClientRole();
client.Run();
}
class ClientRole
{
internal void Run()
{
PeerFinder.Role = PeerRole.Client;
PeerFinder.AllowWiFiDirect = true;
PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) !=
Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)
{
Console.WriteLine("Peer discovery using Wi-Fi Direct is not supported.\n");
return;
}
PeerFinder.Start();
var peerInfoCollection = PeerFinder.FindAllPeersAsync().AsTask().Result;
if (peerInfoCollection.Count == 0)
{
Console.WriteLine("No peers found");
return;
}
foreach (var info in peerInfoCollection)
{
Console.WriteLine("Peer found: {0}", info.DisplayName);
Windows.Networking.Sockets.StreamSocket socket =
PeerFinder.ConnectAsync(info).AsTask().Result;
Sender(socket);
}
}
void Sender(Windows.Networking.Sockets.StreamSocket socket)
{
using (Stream streamWrite = socket.OutputStream.AsStreamForWrite())
using (BinaryWriter writer = new BinaryWriter(streamWrite))
using (Stream streamRead = socket.InputStream.AsStreamForRead())
using (BinaryReader reader = new BinaryReader(streamRead))
{
int ini = Environment.TickCount;
long totalToSend = 64 * 1024 * 1024;
writer.Write(totalToSend);
Console.WriteLine("Going to send {0} bytes", totalToSend);
byte[] buffer = new byte[totalToSend];
writer.Write(buffer);
writer.Flush();
reader.ReadBoolean();
float MB = totalToSend / 1024 / 1024;
float secs = (Environment.TickCount - ini) / 1000;
Console.WriteLine("Sent {0} MB in {1} secs = {2:#0.##} MB/s",
MB, secs, MB / secs);
}
}
void TriggeredConnectionStateChanged(
object sender,
Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs e)
{
// THIS METHOD IS NEVER INVOKED, I don't know why yet
Console.WriteLine("Peer found! " + e.Id);
if (e.State == Windows.Networking.Proximity.TriggeredConnectState.PeerFound)
{
Console.WriteLine("Peer found. You may now pull your devices out of proximity.\n");
}
if (e.State == Windows.Networking.Proximity.TriggeredConnectState.Completed)
{
Console.WriteLine("Connected. You may now send a message.\n");
}
}
}
class HostRole
{
internal void Run()
{
if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) !=
Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)
{
Console.WriteLine("Peer discovery using Wi-Fi Direct is not supported.\n");
}
PeerFinder.Role = PeerRole.Host;
PeerFinder.DisplayName = "PeerFinderConsoleApp";
PeerFinder.AllowWiFiDirect = true;
PeerFinder.ConnectionRequested += ConnectionRequested;
PeerFinder.Start();
Console.ReadLine();
}
void ConnectionRequested(
object sender,
Windows.Networking.Proximity.ConnectionRequestedEventArgs e)
{
Console.WriteLine(
"Connection requested by " + e.PeerInformation.DisplayName + ". " +
"Will be accepted automatically.");
try
{
Receiver(e.PeerInformation);
}
catch (Exception ex)
{
Console.WriteLine("Error in receiver: {0}", ex.Message + Environment.NewLine + ex.StackTrace);
}
}
static void Receiver(PeerInformation peerInfo)
{
Console.WriteLine("Receiver waiting to connect async");
Windows.Networking.Sockets.StreamSocket socket =
PeerFinder.ConnectAsync(peerInfo).AsTask().Result;
Console.WriteLine("Receiver connection happened");
int ini = Environment.TickCount;
using (Stream streamRead = socket.InputStream.AsStreamForRead())
using (Stream streamWrite = socket.OutputStream.AsStreamForWrite())
using (BinaryReader reader = new BinaryReader(streamRead))
using (BinaryWriter writer = new BinaryWriter(streamWrite))
{
long totalToRead = reader.ReadInt64();
Console.WriteLine("Going to read {0} bytes", totalToRead);
long read = 0;
byte[] buffer = new byte[32 * 1024 * 1024];
while (read < totalToRead)
{
long toRead = totalToRead - read;
if (toRead >= buffer.Length)
{
toRead = buffer.Length;
}
reader.Read(buffer, 0, (int)toRead);
Console.WriteLine("{0}/{1}", read, totalToRead);
read += toRead;
writer.Write(true);
writer.Flush();
}
float MB = totalToRead / 1024 / 1024;
float secs = (Environment.TickCount - ini) / 1000;
Console.WriteLine("Received {0} MB in {1} secs = {2:#0.##} MB/s",
MB, secs, MB / secs);
}
}
}
//https://social.msdn.microsoft.com/Forums/en-US/ce649545-9ec6-45da-a4ee-71b9f2bed156/using-metro-win8-sdk-to-build-desktop-style-application?forum=winappswithnativecode
[DllImport("shell32.dll")]
static extern int SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);
}
}