src/irc_conn.c
changeset 28 9c1050bc8709
parent 27 e6639132bead
child 29 3f0f2898fea3
equal deleted inserted replaced
27:e6639132bead 28:9c1050bc8709
     1 
       
     2 #include "irc_conn.h"
     1 #include "irc_conn.h"
     3 #include "irc_cmd.h"
     2 #include "irc_cmd.h"
     4 #include "log.h"
     3 #include "log.h"
     5 
     4 
     6 #include <stdlib.h>
     5 #include <stdlib.h>
    97     // initialize command handlers
    96     // initialize command handlers
    98     STAILQ_INIT(&conn->handlers);
    97     STAILQ_INIT(&conn->handlers);
    99     
    98     
   100     // add the core handlers 
    99     // add the core handlers 
   101     if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL)))
   100     if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL)))
   102         return ERROR_CODE(err);
   101         goto error;
   103 
   102 
   104     // create the line_proto, with our on_line handler
   103     // create the line_proto, with our on_line handler
   105     if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err))
   104     if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err))
   106         return ERROR_CODE(err);
   105         goto error;
   107 
   106 
   108     // ok
   107     // ok
   109     *conn_ptr = conn;
   108     *conn_ptr = conn;
   110 
   109 
   111     return SUCCESS;
   110     return SUCCESS;
       
   111 
       
   112 error:
       
   113     // release
       
   114     irc_conn_destroy(conn);
       
   115 
       
   116     return ERROR_CODE(err);    
       
   117 }
       
   118 
       
   119 void irc_conn_destroy (struct irc_conn *conn)
       
   120 {
       
   121     struct irc_cmd_chain *next = STAILQ_FIRST(&conn->handlers);
       
   122 
       
   123     // release the line_proto
       
   124     if (conn->lp)
       
   125         line_proto_release(conn->lp);
       
   126 
       
   127     // clean up any handler chains
       
   128     while (next) {
       
   129         struct irc_cmd_chain *node = next;
       
   130 
       
   131         // update next
       
   132         next = STAILQ_NEXT(node, node);
       
   133 
       
   134         // free
       
   135         free(node);
       
   136     }
       
   137 
       
   138     // free the irc_conn itself
       
   139     free(conn);
   112 }
   140 }
   113 
   141 
   114 err_t irc_conn_register_handler_chain (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg)
   142 err_t irc_conn_register_handler_chain (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg)
   115 {
   143 {
   116     struct irc_cmd_chain *item;
   144     struct irc_cmd_chain *item;