Issues trying to create a VZVirtioSocketDevice

Hello all, I am trying to setup a socket for communication between the host/guest. According to the documentation after adding a VZVIrtioSocketDeviceConfiguration object to configuration.socketDevices, the resulting virtualMachine object should have an array of VZVirtioSocketDevices with the member function setListener.

So I have:

    let socketConfig = VZVirtioSocketDeviceConfiguration()
    configuration.socketDevices = [socketConfig]
    ...
    let virtualMachine = VZVirtualMachine(configuration: configuration)
    virtualMachine.socketDevices[0].setSocketListener(<#T##VZVirtioSocketListener#>, forPort: <#T##UInt32#>)

but Xcode throws this error

Value of type 'VZSocketDevice' has no member 'setSocketListener'

Is there something I am missing?

Accepted Reply

On the configuration side, VZSocketDeviceConfiguration is an abstract type. There are concrete subclasses, like VZVirtioSocketDeviceConfiguration.

This relationship is mirrored on the VM side. The socketDevices property is an array of VZSocketDevice instances, where each instance is actually a concrete subclass based on the configuration. A VZVirtioSocketDeviceConfiguration yields a VZVirtioSocketDevice, and it’s this that has the listener method. This means you have to cast:

let device = virtualMachine.socketDevices[0] as! VZVirtioSocketDevice
device.setSocketListener(listener, forPort: 12345)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Replies

On the configuration side, VZSocketDeviceConfiguration is an abstract type. There are concrete subclasses, like VZVirtioSocketDeviceConfiguration.

This relationship is mirrored on the VM side. The socketDevices property is an array of VZSocketDevice instances, where each instance is actually a concrete subclass based on the configuration. A VZVirtioSocketDeviceConfiguration yields a VZVirtioSocketDevice, and it’s this that has the listener method. This means you have to cast:

let device = virtualMachine.socketDevices[0] as! VZVirtioSocketDevice
device.setSocketListener(listener, forPort: 12345)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Awesome! That worked perfectly. Thanks a ton!