private void BeginReceiveMessage() { var buffer = new byte[UdpSocket.BufferSize]; var any = UdpSocket.Any(); try { lock (_disposeLock) { if (_disposed) { return; } _socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref any, ReceiveMessageCallback, buffer); } } catch (SocketException se) { MessagerException?.Invoke(this, new ExceptionEventArgs(se)); BeginReceiveMessage(); } catch (ObjectDisposedException) { } }
public void SendMessage(IPEndPoint target, MessageType type, string value = "") { var message = MessageFor(type, value); var buffer = SerializeMessage(message); try { lock (_disposeLock) { if (_disposed) { return; } if (buffer.Length >= UdpSocket.BufferSize) { // switch to TCP mode to handle big messages var port = TcpListener.Queue(buffer); if (port > 0) { // success, replace original message with "switch to tcp" marker + port information + buffer length message = MessageFor(MessageType.Tcp, string.Concat(port, ':', buffer.Length)); buffer = SerializeMessage(message); } } _socket.BeginSendTo(buffer, 0, Math.Min(buffer.Length, UdpSocket.BufferSize), SocketFlags.None, target, SendMessageCallback, null); } } catch (SocketException se) { MessagerException?.Invoke(this, new ExceptionEventArgs(se)); } }
private void SendMessageCallback(IAsyncResult result) { try { lock (_disposeLock) { if (_disposed) { return; } _socket.EndSendTo(result); } } catch (SocketException se) { MessagerException?.Invoke(this, new ExceptionEventArgs(se)); } catch (ObjectDisposedException) { } }
public void SendMessage(IPEndPoint target, MessageType type, string value = "") { var message = MessageFor(type, value); var buffer = SerializeMessage(message); try { lock (_disposeLock) { if (_disposed) { return; } _socket.BeginSendTo(buffer, 0, Math.Min(buffer.Length, UdpSocket.BufferSize), SocketFlags.None, target, SendMessageCallback, null); } } catch (SocketException se) { MessagerException?.Invoke(this, new ExceptionEventArgs(se)); } }
private void RaiseMessagerException(Exception e) { MessagerException?.Invoke(this, new ExceptionEventArgs(e)); }