protected async Task <byte[]> RetrieveMessage(StreamSocket connection) { // Retrieve the length of the data that is about to be sent. int size = 0; byte[] sizeBuffer = BitConverter.GetBytes(size); await P2PSession.ReceiveDataTCP(connection, sizeBuffer, sizeBuffer.Length); size = BitConverter.ToInt32(sizeBuffer, 0); // Retrieve the actual data. byte[] data = new byte[size]; await P2PSession.ReceiveDataTCP(connection, data, data.Length); return(data); }
protected async Task <bool> SendMessage(object message, string host, string port, Type type) { if (String.IsNullOrEmpty(host)) { throw new ArgumentException("Host value cannot be null or empty."); } if (String.IsNullOrEmpty(port)) { throw new ArgumentException("Port value cannot be null or empty."); } bool success = false; using (var socket = new StreamSocket()) { try { // Establish a connection with the host; verification is not necessary. await socket.ConnectAsync(new Windows.Networking.HostName(host), port); byte[] data = P2PSession.Serialize(message, type); // Send the size of the data first. int size = data.Length; byte[] sizeBuffer = BitConverter.GetBytes(size); await P2PSession.SendDataTCP(socket, sizeBuffer); // Send the serialized data. await P2PSession.SendDataTCP(socket, data); success = true; } catch (Exception) { } return(success); } }