Skip to content

Commit

Permalink
Update io_socket.c
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Edwards <joed6834@colorado.edu>
  • Loading branch information
bearycool11 authored Nov 12, 2024
1 parent 911a12d commit c6bccc3
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions io_socket.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

// IO Socket Structure
typedef struct {
int id;
int memory_silo_id;
int socket;
int id;
int memory_silo_id;
int socket;
} io_socket_t;

// Global variable to hold the IO socket instance
io_socket_t* io_socket_instance = NULL;

// IO Socket Functions
void io_socket_init(int memory_silo_id) {
// Initialize the IO socket
io_socket_t* io_socket = malloc(sizeof(io_socket_t));
io_socket->id = 1;
io_socket->memory_silo_id = memory_silo_id;
io_socket->socket = socket(AF_INET, SOCK_STREAM, 0);
// Initialize the IO socket
io_socket_instance = malloc(sizeof(io_socket_t));
if (io_socket_instance == NULL) {
perror("Failed to allocate memory for IO socket");
exit(EXIT_FAILURE);
}
io_socket_instance->id = 1;
io_socket_instance->memory_silo_id = memory_silo_id;
io_socket_instance->socket = socket(AF_INET, SOCK_STREAM, 0);
if (io_socket_instance->socket < 0) {
perror("Failed to create socket");
free(io_socket_instance);
exit(EXIT_FAILURE);
}
}

void io_socket_read(void* buffer, int length) {
// Read from the IO socket
if (io_socket_instance == NULL) {
fprintf(stderr, "IO socket not initialized.\n");
return;
}
ssize_t bytes_read = read(io_socket_instance->socket, buffer, length);
if (bytes_read < 0) {
perror("Read error");
}
}

void io_socket_read(int socket, void* buffer, int length) {
// Read from the IO socket
io_socket_t* io_socket = get_io_socket(socket);
read(io_socket->socket, buffer, length);
void io_socket_write(void* buffer, int length) {
// Write to the IO socket
if (io_socket_instance == NULL) {
fprintf(stderr, "IO socket not initialized.\n");
return;
}
ssize_t bytes_written = write(io_socket_instance->socket, buffer, length);
if (bytes_written < 0) {
perror("Write error");
}
}

void io_socket_write(int socket, void* buffer, int length) {
// Write to the IO socket
io_socket_t* io_socket = get_io_socket(socket);
write(io_socket->socket, buffer, length);
// Get IO Socket Function (for demonstration, returns the global instance)
io_socket_t* get_io_socket(void) {
// Get the IO socket associated with the socket
return io_socket_instance;
}

// Get IO Socket Function
io_socket_t* get_io_socket(int socket) {
// Get the IO socket associated with the socket
// ...
return NULL;
// Cleanup function to free allocated resources
void io_socket_cleanup() {
if (io_socket_instance != NULL) {
close(io_socket_instance->socket);
free(io_socket_instance);
io_socket_instance = NULL;
}
}

0 comments on commit c6bccc3

Please sign in to comment.