Skip to content

Conversation

@beefon
Copy link
Contributor

@beefon beefon commented Dec 31, 2025

Only implementation for the server socket which listens to the unix socket, and client which lets you talk to the server over socket. Plus some basic tests which describe high level usage.

@beefon beefon force-pushed the dev/beefon/UnixSocketServerAndClient branch from fd5f861 to 2ee1cb0 Compare January 2, 2026 12:55
Copy link
Contributor

@marciniwanicki marciniwanicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big thanks @beefon! Very excited for curie to get the ability to be controlled remotely. Raw sockets are a bit tricky, I wonder if it would make sense to try to use https://github.com/apple/swift-nio.


var addr = sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)
strcpy(&addr.sun_path.0, socketPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strongSelf.handleClient(clientFileDescriptor: clientFileDescriptor, handler: handler)
}
}
listeningHandle.close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there chance here that the Darwin.close will be called twice for the same file descriptor?

public final class ServerListeningHandle {
public let socketPath: String
public let fileDescriptor: Int32
public private(set) var isListening: Bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility of data race, ie the property being accessed from multiple threads?

throw NSError(domain: "SocketBindError", code: 2)
}

listen(serverFileDescriptor, 10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially check the return code to handle errors.

) throws -> Response {
let clientFileDescriptor = socket(AF_UNIX, SOCK_STREAM, 0)
guard clientFileDescriptor >= 0 else {
throw NSError(domain: "SocketError", code: 1)
Copy link
Contributor

@marciniwanicki marciniwanicki Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're in Swift land, we can probably just throw CoreError.

clientMessage: "from client",
expectedServerMessage: "from server"
)
wait(for: [serverHandlerCalled])
Copy link
Contributor

@marciniwanicki marciniwanicki Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to set a timeout.

@beefon beefon force-pushed the dev/beefon/UnixSocketServerAndClient branch from 2ee1cb0 to 1848945 Compare January 5, 2026 22:20
@beefon beefon force-pushed the dev/beefon/UnixSocketServerAndClient branch from 1848945 to b3fe9fb Compare January 5, 2026 22:21
@beefon
Copy link
Contributor Author

beefon commented Jan 5, 2026

Thanks @marciniwanicki , I think i have addressed comments. Please have a look.

Re: swift-nio. I have attempted to do the socket using that library few months ago when i was doing my change, and I wasn't happy with result. It was super overengineered for such simple use case as i want to have in curie. Also, pulling in library that has more code than this project sounds weird. If this is blocker to you though, I can try to re-do the sockets based upon swift-nio.

Copy link
Contributor

@marciniwanicki marciniwanicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @beefon for the follow ups. I've seen few more small edge cases which might be problematic, but we can address them later. Great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants