Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

General cleanup and fixes

*fixed memory leak
*corrected stylization
*added a pointer to connection struct to optionally hold data to send


Signed-off-by: Tigerbot <slgibelyou@gmail.com>
  • Loading branch information...
commit 84b8b9254728df3138053aefcc50beff1674ff5c 1 parent 8ee3fbc
authored February 11, 2011
0  include/bool.h → include/evn-bool.h
File renamed without changes
7  include/buffer-list.h → include/evn-buffer-list.h
... ...
@@ -1,3 +1,6 @@
  1
+#ifndef EVN_BUFFER_LIST_H_
  2
+#define EVN_BUFFER_LIST_H_
  3
+
1 4
 #include <stdio.h> // printf
2 5
 
3 6
 #define evn_error(...) printf("[EVN] " __VA_ARGS__)
@@ -38,7 +41,7 @@ void evn_buffer_destroy(evn_buffer* buffer);
38 41
 
39 42
 // Create a "smart" buffer
40 43
 evn_bufferlist* evn_bufferlist_create(int min_block_size, int slices);
41  
-// Copy data into the buffer 
  44
+// Copy data into the buffer
42 45
 int evn_bufferlist_add(evn_bufferlist* bufferlist, void* data, int size);
43 46
 // Copy all data into a single buffer
44 47
 evn_buffer* evn_bufferlist_concat(evn_bufferlist* bufferlist);
@@ -47,3 +50,5 @@ evn_buffer* evn_bufferlist_concat(evn_bufferlist* bufferlist);
47 50
 
48 51
 // Free all memory associated with the bufferlist and the buffers it contains
49 52
 void evn_bufferlist_destroy(evn_bufferlist* bufferlist);
  53
+
  54
+#endif
0  include/inbuf.h → include/evn-inbuf.h
File renamed without changes
9  include/evn.h
@@ -14,11 +14,9 @@
14 14
 
15 15
 #include <ev.h>
16 16
 
17  
-#include "buffer-list.h"
18  
-#include "inbuf.h"
19  
-#include "bool.h"
20  
-
21  
-#define EVN_DEBUG 1
  17
+#include "evn-buffer-list.h"
  18
+#include "evn-inbuf.h"
  19
+#include "evn-bool.h"
22 20
 
23 21
 #ifndef EVN_DEBUG
24 22
   #if defined DEBUG
@@ -105,6 +103,7 @@ struct evn_stream {
105 103
   int socket_len;
106 104
   EV_P;
107 105
   char type;
  106
+  void* send_data;
108 107
 };
109 108
 
110 109
 
2  libevnet/buffer-list-test.c
@@ -2,7 +2,7 @@
2 2
 #include <stdio.h> // printf
3 3
 #include <stdlib.h> // exit
4 4
 
5  
-#include "buffer-list.h"
  5
+#include "evn-buffer-list.h"
6 6
 #include "evn-assert.c"
7 7
 
8 8
 int main (int argc, char* argv[])
2  libevnet/buffer-list.c
@@ -3,7 +3,7 @@
3 3
 #include <stdlib.h> // malloc, calloc, realloc
4 4
 #include <string.h> // memcpy
5 5
 
6  
-#include "buffer-list.h"
  6
+#include "evn-buffer-list.h"
7 7
 
8 8
 // indices are always at the 'free' position
9 9
 // if the position is equal to the length, the buffer is full
31  libevnet/evn.c
@@ -12,7 +12,6 @@ static int evn_priv_unix_create(struct sockaddr_un* socket_un, char* sock_path);
12 12
 static int evn_priv_tcp_create(struct sockaddr_in* socket_in, int port, char* sock_path);
13 13
 
14 14
 static bool evn_stream_priv_send(struct evn_stream* stream, void* data, int size);
15  
-static char recv_data[EVN_MAX_RECV];
16 15
 
17 16
 inline struct evn_stream* evn_stream_create(int fd) {
18 17
   evn_debug("evn_stream_create");
@@ -41,6 +40,7 @@ bool evn_stream_destroy(EV_P_ struct evn_stream* stream)
41 40
   stream->ready_state = evn_CLOSED;
42 41
   if (stream->on_close) { stream->on_close(EV_A_ stream, result); }
43 42
   evn_inbuf_destroy(stream->_priv_out_buffer);
  43
+  free(stream->socket);
44 44
   free(stream);
45 45
 
46 46
   return result;
@@ -53,9 +53,10 @@ void evn_stream_priv_on_read(EV_P_ ev_io *w, int revents)
53 53
   struct evn_exception error;
54 54
   int length;
55 55
   struct evn_stream* stream = (struct evn_stream*) w;
  56
+  char recv_data[EVN_MAX_RECV];
56 57
 
57 58
   evn_debugs("EV_READ - stream->io.fd");
58  
-  length = recv(stream->io.fd, &recv_data, 4096, 0);
  59
+  length = recv(stream->io.fd, &recv_data, EVN_MAX_RECV, 0);
59 60
 
60 61
   if (length < 0)
61 62
   {
@@ -64,8 +65,8 @@ void evn_stream_priv_on_read(EV_P_ ev_io *w, int revents)
64 65
   else if (0 == length)
65 66
   {
66 67
     evn_debugs("received FIN");
67  
-    // We no longer have data to ready but we may still have data to write
68  
-    stream->ready_state = evn_READ_ONLY;
  68
+    // We no longer have data to read but we may still have data to write
  69
+    stream->ready_state = evn_WRITE_ONLY;
69 70
     if (stream->io.events & EV_WRITE)
70 71
     {
71 72
       int fd = stream->io.fd;
@@ -392,7 +393,7 @@ static void evn_stream_priv_on_writable(EV_P_ ev_io *w, int revents)
392 393
     ev_io_start(EV_A_ &stream->io);
393 394
     evn_debugs("pre-drain");
394 395
     if (stream->on_drain) { stream->on_drain(EV_A_ stream); }
395  
-    // and the 
  396
+    // and the
396 397
     return;
397 398
   }
398 399
   evn_debugs("post-null");
@@ -464,10 +465,12 @@ struct evn_stream* evn_create_connection_tcp_stream(EV_P_ int port, char* addres
464 465
   stream->socket_len = sizeof(struct sockaddr);
465 466
 
466 467
   if (-1 == connect(stream_fd, (struct sockaddr*) stream->socket, stream->socket_len)) {
467  
-      perror("connect");
468  
-      exit(EXIT_FAILURE);
  468
+    fprintf(stderr, "[EVN] connect to %s: %s\n", address, strerror(errno));
  469
+    //exit(EXIT_FAILURE);
  470
+    evn_stream_destroy(EV_A_ stream);
  471
+    stream = NULL;
469 472
   }
470  
-  
  473
+
471 474
   return stream;
472 475
 }
473 476
 
@@ -479,8 +482,8 @@ struct evn_stream* evn_create_connection_unix_stream(EV_P_ char* sock_path)
479 482
 
480 483
   stream_fd = socket(AF_UNIX, SOCK_STREAM, 0);
481 484
   if (-1 == stream_fd) {
482  
-      perror("[EVN] Unix Stream socket connection");
483  
-      exit(EXIT_FAILURE);
  485
+    perror("[EVN] Unix Stream socket connection");
  486
+    exit(EXIT_FAILURE);
484 487
   }
485 488
   stream = evn_stream_create(stream_fd);
486 489
   stream->socket = malloc(sizeof(struct sockaddr_un));
@@ -495,9 +498,11 @@ struct evn_stream* evn_create_connection_unix_stream(EV_P_ char* sock_path)
495 498
   stream->socket_len = strlen(sock->sun_path) + 1 + sizeof(sock->sun_family);
496 499
 
497 500
   if (-1 == connect(stream_fd, (struct sockaddr *) sock, stream->socket_len)) {
498  
-      perror("connect");
499  
-      exit(EXIT_FAILURE);
  501
+    fprintf(stderr, "[EVN] connect to %s: %s\n", sock_path, strerror(errno));
  502
+    //exit(EXIT_FAILURE);
  503
+    evn_stream_destroy(EV_A_ stream);
  504
+    stream = NULL;
500 505
   }
501  
-  
  506
+
502 507
   return stream;
503 508
 }
2  libevnet/inbuf-test.c
... ...
@@ -1,6 +1,6 @@
1 1
 #include <stdio.h>
2 2
 
3  
-#include "inbuf.h"
  3
+#include "evn-inbuf.h"
4 4
 #include "evn-assert.c"
5 5
 #include "string.h"
6 6
 
2  libevnet/inbuf.c
@@ -3,7 +3,7 @@
3 3
 
4 4
 //#include <stdio.h>
5 5
 
6  
-#include "inbuf.h"
  6
+#include "evn-inbuf.h"
7 7
 
8 8
 static inline void* bufcpy(void* dst, void* src, size_t size)
9 9
 {

0 notes on commit 84b8b92

Please sign in to comment.
Something went wrong with that request. Please try again.