Advertisement
Guest User

utils.c

a guest
Apr 8th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. /**
  2.  * Charming Chatroom
  3.  * CS 241 - Spring 2020
  4.  */
  5. #include <arpa/inet.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11.  
  12. // partner: nehap2, skondi2
  13. #include "utils.h"
  14. static const size_t MESSAGE_SIZE_DIGITS = 4;
  15.  
  16. char *create_message(char *name, char *message) {
  17.     int name_len = strlen(name);
  18.     int msg_len = strlen(message);
  19.     char *msg = calloc(1, msg_len + name_len + 4);
  20.     sprintf(msg, "%s: %s", name, message);
  21.  
  22.     return msg;
  23. }
  24.  
  25. ssize_t get_message_size(int socket) {
  26.     int32_t size;
  27.     ssize_t read_bytes =
  28.         read_all_from_socket(socket, (char *)&size, MESSAGE_SIZE_DIGITS);
  29.     if (read_bytes == 0 || read_bytes == -1)
  30.         return read_bytes;
  31.  
  32.     return (ssize_t)ntohl(size);
  33. }
  34.  
  35. // You may assume size won't be larger than a 4 byte integer
  36. ssize_t write_message_size(size_t size, int socket) {
  37.     // Your code here
  38.     int32_t size_ = htonl(size);
  39.     ssize_t write_bytes =
  40.         write_all_to_socket(socket, (char *)&size_, MESSAGE_SIZE_DIGITS);
  41.    
  42.     if (write_bytes == 0 || write_bytes == -1) {
  43.         return write_bytes;
  44.     }
  45.     return (ssize_t)size_;
  46. }
  47.  
  48. // source: CS 241 Textbook and lectures 25 & 26
  49. ssize_t read_all_from_socket(int socket, char *buffer, size_t count) {
  50.     // Your Code Here
  51.     size_t total_read = 0;
  52.     ssize_t result;
  53.     while ((total_read < count) && (result = read(socket, buffer+total_read, count - total_read))) {
  54.         if (result == -1 && errno == EINTR) {
  55.             continue;
  56.         }
  57.         if (result == -1) {
  58.             return -1;
  59.         }
  60.         if (result == 0) {
  61.             return 0;
  62.         }
  63.         total_read += result;
  64.     }
  65.     return total_read;
  66. }
  67.  
  68. // source: CS 241 Textbook and lectures 25 & 26
  69. ssize_t write_all_to_socket(int socket, const char *buffer, size_t count) {
  70.     // Your Code Here
  71.     size_t total_sent = 0;
  72.     ssize_t result;
  73.     while ((total_sent < count) && (result = write(socket, buffer+total_sent, count - total_sent))) {
  74.         if (result == -1 && errno == EINTR) {
  75.             continue;
  76.         }
  77.         if (result == -1) {
  78.             return -1;
  79.         }
  80.         if (result == 0) {
  81.             return 0;
  82.         }
  83.         total_sent += result;
  84.     }
  85.     return total_sent;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement