예제 #1
0
        private async Task <string> ReceiveMessage(Socket socket)
        {
            var bytes         = new byte[MAX_RESPONSE_LENGTH];
            var bytesReceived = await Task.Factory.FromAsync(
                (callback, stateObject) => socket.BeginReceive(
                    bytes,
                    0,
                    MAX_RESPONSE_LENGTH,
                    SocketFlags.None,
                    callback,
                    stateObject),
                socket.EndReceive,
                null);

            return(TpLinkUtils.Decrypt(bytes));
        }
예제 #2
0
        /// <summary>
        /// Sends a message using the provided socket.
        /// </summary>
        private async Task SendMessage(Socket socket, string message)
        {
            var bytes     = TpLinkUtils.Encrypt(message);
            var bytesSent = await Task.Factory.FromAsync(
                (callback, stateObject) => socket.BeginSend(
                    bytes,
                    0,
                    bytes.Length,
                    SocketFlags.None,
                    callback,
                    stateObject),
                socket.EndSend,
                null);

            if (bytesSent != bytes.Length)
            {
                throw new SocketWriteException($"Tried to write {bytes.Length} bytes, only wrote {bytesSent} (msg={message})");
            }
        }