static void OnReceive(Udp udp, IDatagramReadCompletion completion) { if (completion.Error != null) { Console.WriteLine($"Echo server receive error {completion.Error}"); udp.CloseHandle(OnClosed); return; } IPEndPoint remoteEndPoint = completion.RemoteEndPoint; ReadableBuffer data = completion.Data; string message = data.Count > 0 ? data.ReadString(data.Count, Encoding.UTF8) : null; if (string.IsNullOrEmpty(message)) { return; } Console.WriteLine($"Echo server received : {message} from {remoteEndPoint}"); Console.WriteLine($"Echo server sending echo back to {remoteEndPoint}."); byte[] array = Encoding.UTF8.GetBytes($"ECHO [{message}]"); WritableBuffer buffer = WritableBuffer.From(array); udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted); }
public void Bind(bool reuseAddress) { var endPoint = new IPEndPoint(IPAddress.Loopback, Port); Udp udp1 = this.loop.CreateUdp(); udp1.Bind(endPoint, reuseAddress); Udp udp2 = this.loop.CreateUdp(); if (reuseAddress) { udp2.Bind(endPoint, true); } else { var error = Assert.Throws <OperationException>(() => udp2.Bind(endPoint)); Assert.Equal(ErrorCode.EADDRINUSE, error.ErrorCode); } udp1.CloseHandle(this.OnClose); udp2.CloseHandle(this.OnClose); this.loop.RunDefault(); Assert.Equal(2, this.closeCount); }
static void OnReceive(Udp udp, IDatagramReadCompletion completion) { if (completion.Error != null) { completion.Dispose(); Console.WriteLine($"{nameof(EchoServer)} receive error {completion.Error}"); udp.ReceiveStop(); udp.CloseHandle(OnClose); return; } ReadableBuffer data = completion.Data; string message = data.ReadString(Encoding.UTF8); data.Dispose(); IPEndPoint remoteEndPoint = completion.RemoteEndPoint; if (string.IsNullOrEmpty(message) || remoteEndPoint == null) { return; } WritableBuffer buffer = WritableBuffer.From(Encoding.UTF8.GetBytes(message)); udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted); }
void OnReceive(Udp udp, IDatagramReadCompletion completion) { if (completion.Error != null || completion.RemoteEndPoint == null) { return; } ReadableBuffer buffer = completion.Data; if (buffer.Count == 0) { return; } string message = buffer.ReadString(buffer.Count, Encoding.UTF8); if (message == "PING" || message == "PANG") { this.serverReceiveCount++; } if (this.serverReceiveCount == 2) { udp.CloseHandle(this.OnClose); } }
static void OnSendCompleted(Udp udp, Exception exception) { if (exception != null) { udp.CloseHandle(OnClose); Console.WriteLine($"{nameof(EchoServer)} send error {exception}"); } }
static void OnSendCompleted(Udp udp, Exception exception) { if (exception != null) { Console.WriteLine($"Echo server send error {exception}"); udp.CloseHandle(OnClosed); } }
void OnSendCompleted(Udp udp, Exception exception) { if (exception == null) { this.connectedCount++; } udp.CloseHandle(this.OnClose); }
void OnSendCompleted(Udp udp, Exception exception) { if (exception == null) { this.clientSendCount++; } if (this.clientSendCount == 2) { udp.CloseHandle(this.OnClose); } }
void OnClientReceive(Udp udp, IDatagramReadCompletion completion) { ReadableBuffer buffer = completion.Data; string message = buffer.ReadString(Encoding.UTF8); if (message == "PONG") { this.clientReceiveCount++; } udp.CloseHandle(this.OnClose); }
static void OnReceive(Udp udp, IDatagramReadCompletion completion) { if (completion.Error != null) { Console.WriteLine($"Echo client receive error {completion.Error}"); } IPEndPoint remoteEndPoint = completion.RemoteEndPoint; ReadableBuffer data = completion.Data; string message = data.ReadString(Encoding.UTF8); Console.WriteLine($"Echo client received : {message} from {remoteEndPoint}"); udp.CloseHandle(OnClosed); }
void OnClientReceive(Udp udp, IDatagramReadCompletion completion) { this.receiveError = completion.Error; ReadableBuffer buffer = completion.Data; string message = buffer.ReadString(Encoding.UTF8); if (message == "PING") { this.clientReceiveCount++; } /* we are done with the client handle, we can close it */ udp.CloseHandle(this.OnClose); }
void OnServerSendCompleted(Udp udp, Exception exception) { if (exception == null) { this.serverSendCount++; } else { var error = exception as OperationException; if (error != null && error.ErrorCode == ErrorCode.ENETUNREACH) { this.serverSendCount++; } } udp.CloseHandle(this.OnClose); }
void OnServerReceive(Udp udp, IDatagramReadCompletion completion) { this.receiveError = completion.Error; ReadableBuffer data = completion.Data; string message = data.ReadString(Encoding.UTF8); if (message == "EXIT") { this.serverReceiveCount++; } udp.CloseHandle(this.OnClose); this.client?.CloseHandle(this.OnClose); this.server.ReceiveStop(); this.server.CloseHandle(this.OnClose); }
void OnSendCompleted(Udp udp, Exception exception) { if (exception == null) { this.sendErrorValid = true; } else { var error = exception as OperationException; if (error != null) { this.sendErrorValid = error.ErrorCode == ErrorCode.ENETUNREACH; } } this.sendCount++; udp.CloseHandle(this.OnClose); }
void OnReceive(Udp udp, IDatagramReadCompletion completion) { if (completion.Error == null) { ReadableBuffer data = completion.Data; if (data.Count == 0) { return; } IPEndPoint localEndPoint = udp.GetLocalEndPoint(); if (Equals(localEndPoint.Address, IPAddress.Any) && localEndPoint.Port == Port) { this.connectionCount++; } } udp.CloseHandle(this.OnClose); }
public void NoBind() { Udp udp = this.loop.CreateUdp(); var error = Assert.Throws <OperationException>(() => udp.MulticastTtl(32)); Assert.Equal(ErrorCode.EBADF, error.ErrorCode); error = Assert.Throws <OperationException>(() => udp.Broadcast(true)); Assert.Equal(ErrorCode.EBADF, error.ErrorCode); error = Assert.Throws <OperationException>(() => udp.Ttl(1)); Assert.Equal(ErrorCode.EBADF, error.ErrorCode); error = Assert.Throws <OperationException>(() => udp.MulticastInterface(IPAddress.Any)); Assert.Equal(ErrorCode.EBADF, error.ErrorCode); udp.CloseHandle(OnClose); this.loop.RunDefault(); }
void OnSendCompleted(Udp udp, Exception exception) { if (exception != null) { var error = exception as OperationException; if (error != null && error.ErrorCode == ErrorCode.ECANCELED) { return; } Console.WriteLine( $"Udp pummel {this.numberOfSenders}v{this.numberOfReceivers} failed, {exception}"); } if (this.exiting) { return; } if (this.timeout > 0 || this.packetCounter > 0) { IPEndPoint endPoint = this.senders[udp]; udp.QueueSend(this.content, endPoint, this.OnSendCompleted); this.sendCount++; } if (this.timeout != 0) { return; } this.packetCounter--; if (this.packetCounter == 0) { udp.CloseHandle(this.OnClose); } }
void OnServerSendCompleted(Udp udp, Exception exception) { this.sendError = exception; this.serverSendCount++; udp.CloseHandle(this.OnClose); }