-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use generated PInvokes and exchange types (#50685)
- Loading branch information
Showing
69 changed files
with
1,036 additions
and
2,507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,89 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace TestClient | ||
namespace TestClient; | ||
|
||
public class Program | ||
{ | ||
public class Program | ||
private const string Address = | ||
"http://localhost:5000/public/1kb.txt"; | ||
// "https://localhost:9090/public/1kb.txt"; | ||
|
||
public static void Main(string[] args) | ||
{ | ||
private const string Address = | ||
"http://localhost:5000/public/1kb.txt"; | ||
// "https://localhost:9090/public/1kb.txt"; | ||
Console.WriteLine("Ready"); | ||
Console.ReadKey(); | ||
|
||
public static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Ready"); | ||
Console.ReadKey(); | ||
var handler = new HttpClientHandler(); | ||
handler.MaxConnectionsPerServer = 500; | ||
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; | ||
// handler.UseDefaultCredentials = true; | ||
HttpClient client = new HttpClient(handler); | ||
|
||
var handler = new HttpClientHandler(); | ||
handler.MaxConnectionsPerServer = 500; | ||
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; | ||
// handler.UseDefaultCredentials = true; | ||
HttpClient client = new HttpClient(handler); | ||
RunParallelRequests(client); | ||
|
||
RunParallelRequests(client); | ||
// RunManualRequests(client); | ||
|
||
// RunManualRequests(client); | ||
// RunWebSocketClient().Wait(); | ||
|
||
// RunWebSocketClient().Wait(); | ||
Console.WriteLine("Done"); | ||
// Console.ReadKey(); | ||
} | ||
|
||
Console.WriteLine("Done"); | ||
// Console.ReadKey(); | ||
private static void RunManualRequests(HttpClient client) | ||
{ | ||
while (true) | ||
{ | ||
Console.WriteLine("Press any key to send request"); | ||
Console.ReadKey(); | ||
var result = client.GetAsync(Address).Result; | ||
Console.WriteLine(result); | ||
} | ||
} | ||
|
||
private static void RunManualRequests(HttpClient client) | ||
private static void RunParallelRequests(HttpClient client) | ||
{ | ||
int completionCount = 0; | ||
int iterations = 100000; | ||
for (int i = 0; i < iterations; i++) | ||
{ | ||
while (true) | ||
{ | ||
Console.WriteLine("Press any key to send request"); | ||
Console.ReadKey(); | ||
var result = client.GetAsync(Address).Result; | ||
Console.WriteLine(result); | ||
} | ||
client.GetAsync(Address) | ||
.ContinueWith(t => Interlocked.Increment(ref completionCount)); | ||
} | ||
|
||
private static void RunParallelRequests(HttpClient client) | ||
while (completionCount < iterations) | ||
{ | ||
int completionCount = 0; | ||
int iterations = 100000; | ||
for (int i = 0; i < iterations; i++) | ||
{ | ||
client.GetAsync(Address) | ||
.ContinueWith(t => Interlocked.Increment(ref completionCount)); | ||
} | ||
|
||
while (completionCount < iterations) | ||
{ | ||
Thread.Sleep(10); | ||
} | ||
Thread.Sleep(10); | ||
} | ||
} | ||
|
||
public static async Task RunWebSocketClient() | ||
{ | ||
ClientWebSocket websocket = new ClientWebSocket(); | ||
|
||
string url = "ws://localhost:5000/"; | ||
Console.WriteLine("Connecting to: " + url); | ||
await websocket.ConnectAsync(new Uri(url), CancellationToken.None); | ||
|
||
public static async Task RunWebSocketClient() | ||
string message = "Hello World"; | ||
Console.WriteLine("Sending message: " + message); | ||
byte[] messageBytes = Encoding.UTF8.GetBytes(message); | ||
await websocket.SendAsync(new ArraySegment<byte>(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None); | ||
|
||
byte[] incomingData = new byte[1024]; | ||
WebSocketReceiveResult result = await websocket.ReceiveAsync(new ArraySegment<byte>(incomingData), CancellationToken.None); | ||
|
||
if (result.CloseStatus.HasValue) | ||
{ | ||
Console.WriteLine("Closed; Status: " + result.CloseStatus + ", " + result.CloseStatusDescription); | ||
} | ||
else | ||
{ | ||
ClientWebSocket websocket = new ClientWebSocket(); | ||
|
||
string url = "ws://localhost:5000/"; | ||
Console.WriteLine("Connecting to: " + url); | ||
await websocket.ConnectAsync(new Uri(url), CancellationToken.None); | ||
|
||
string message = "Hello World"; | ||
Console.WriteLine("Sending message: " + message); | ||
byte[] messageBytes = Encoding.UTF8.GetBytes(message); | ||
await websocket.SendAsync(new ArraySegment<byte>(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None); | ||
|
||
byte[] incomingData = new byte[1024]; | ||
WebSocketReceiveResult result = await websocket.ReceiveAsync(new ArraySegment<byte>(incomingData), CancellationToken.None); | ||
|
||
if (result.CloseStatus.HasValue) | ||
{ | ||
Console.WriteLine("Closed; Status: " + result.CloseStatus + ", " + result.CloseStatusDescription); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Received message: " + Encoding.UTF8.GetString(incomingData, 0, result.Count)); | ||
} | ||
Console.WriteLine("Received message: " + Encoding.UTF8.GetString(incomingData, 0, result.Count)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.