public unsafe static byte[] GetSha1(byte[] path) { var ps = PenguinSanitizer.Extensions.ToBytePtr(path); var newFd = LibC.open(ps, LibC.O_RDONLY); // Tap the stream so we can get a hash on it. var hash = HashAlgorithm.Create("SHA1"); var buf = new Byte[32 * 1024 * 1024]; while (true) { ssize_t rd = 0; fixed(void *b = buf) { rd = LibC.read(newFd, b, buf.Length); hash.TransformBlock(buf, 0, (int)rd, buf, 0); } if (rd < 1) { break; } } LibC.close(newFd); buf = null; hash.TransformFinalBlock(buf, 0, 0); return(hash.Hash); }
private static unsafe void Copy(int socketFileDescriptor) { fixed(byte *pinnedMessageBuffer = PerThreadBuffer) { ssize_t bytesRead = recv(socketFileDescriptor, pinnedMessageBuffer, MAX_MESSAGE_LENGTH, 0); send(socketFileDescriptor, pinnedMessageBuffer, (size_t)bytesRead, 0); } }
private long_t(ssize_t value) => __value = value;
internal static unsafe void Run(int portNumber, bool executeOnEpollThread) { int socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0); if (socketFileDescriptor < 0) { Environment.FailFast($"Failed to create socket, socket returned {socketFileDescriptor}"); } sockaddr_in socketAddress = new sockaddr_in { sin_family = AF_INET, sin_port = htons((ushort)portNumber), sin_addr = INADDR_ANY }; int bindResult = bind(socketFileDescriptor, (sockaddr *)&socketAddress, sizeof(sockaddr_in)); if (bindResult < 0) { Environment.FailFast($"Failed to bind, bind returned {bindResult}"); } int listenResult = listen(socketFileDescriptor, BACKLOG); if (listenResult < 0) { Environment.FailFast($"Failed to start listening, listen returned {listenResult}"); } int epollFileDescriptor = epoll_create(MAX_EVENTS); if (epollFileDescriptor < 0) { Environment.FailFast($"Failed to create epoll, epoll_create returned {epollFileDescriptor}"); } epoll_event epollAddSocketEvent = new epoll_event { events = EPOLLIN, data = new epoll_data_t { fd = socketFileDescriptor } }; if (epoll_ctl(epollFileDescriptor, EPOLL_CTL_ADD, socketFileDescriptor, &epollAddSocketEvent) == -1) { Environment.FailFast("Failed to add new socket file descriptor to epoll"); } epoll_event[] epollEvents = new epoll_event[MAX_EVENTS]; byte[] messageBuffer = new byte[MAX_MESSAGE_LENGTH]; fixed(epoll_event *pinnedEvents = epollEvents) fixed(byte *pinnedMessageBuffer = messageBuffer) { while (true) { int eventsCount = epoll_wait(epollFileDescriptor, pinnedEvents, MAX_EVENTS, -1); if (eventsCount == -1) { Environment.FailFast("epoll_wait returned -1"); } for (int i = 0; i < eventsCount; i++) { int currentSocketFileDescriptor = epollEvents[i].data.fd; if (currentSocketFileDescriptor == socketFileDescriptor) { sockaddr_in clientAddress; socklen_t clientAddressSize = sizeof(sockaddr_in); int acceptResult = accept4(socketFileDescriptor, (sockaddr *)&clientAddress, &clientAddressSize, SOCK_NONBLOCK); if (acceptResult == -1) { Environment.FailFast($"accept4 returned {acceptResult}"); } epollAddSocketEvent.events = EPOLLIN | EPOLLET; epollAddSocketEvent.data.fd = acceptResult; if (epoll_ctl(epollFileDescriptor, EPOLL_CTL_ADD, acceptResult, &epollAddSocketEvent) == -1) { Environment.FailFast("Failed to add socket to epoll"); } } else if (executeOnEpollThread) { ssize_t bytesRead = recv(currentSocketFileDescriptor, pinnedMessageBuffer, MAX_MESSAGE_LENGTH, 0); send(currentSocketFileDescriptor, pinnedMessageBuffer, (size_t)bytesRead, 0); } else { ThreadPool.UnsafeQueueUserWorkItem <int>(Copy, currentSocketFileDescriptor, false); } } } } }
public static PosixResult FromReturnValue(ssize_t rv) { return(rv < 0 ? new PosixResult(-Tmds.Linux.LibC.errno) : new PosixResult(rv)); }
public PosixResult(ssize_t value) { _value = value; }
internal size_t(ssize_t arg) { __value = (uint)arg.Value; }
internal size_t(ssize_t arg) { __value = (ulong)arg.Value; }