loudmouth/lm-socket.c
changeset 151 5b6598b5886b
parent 150 18a91b086713
child 154 207483c02cfc
equal deleted inserted replaced
150:18a91b086713 151:5b6598b5886b
    27 #endif /* G_OS_WIN32 */
    27 #endif /* G_OS_WIN32 */
    28 
    28 
    29 /* FIXME: Integrate with the SSL stuff */
    29 /* FIXME: Integrate with the SSL stuff */
    30 
    30 
    31 struct _LmSocket {
    31 struct _LmSocket {
    32 	LmSock sock;
    32 	LmSock          sock;
       
    33 	GIOChannel     *channel;
    33 	/* FIXME: Add the rest */
    34 	/* FIXME: Add the rest */
    34 	
    35 
    35 	LmSocketFuncs funcs;
    36 	LmSSL          *ssl;
    36 	
    37 	
    37 	LmSocketState state;
    38 	LmSocketFuncs   funcs;
    38 
    39 	
    39 	gboolean      is_blocking;
    40 	LmSocketState   state;
    40 	
    41 
    41 	gint          ref;
    42 	gboolean        is_blocking;
       
    43 	
       
    44 	gint            ref;
    42 };
    45 };
    43 
    46 
    44 static LmSocket *  socket_create (void);
    47 static LmSocket *  socket_create (void);
    45 static void        socket_free   (LmSocket *socket);
    48 static void        socket_free   (LmSocket *socket);
    46 
    49 
    50 	LmSocket *socket;
    53 	LmSocket *socket;
    51 
    54 
    52 	socket = g_new0 (LmSocket, 1);
    55 	socket = g_new0 (LmSocket, 1);
    53 	socket->ref_count = 1;
    56 	socket->ref_count = 1;
    54 	socket->is_blocking = FALSE;
    57 	socket->is_blocking = FALSE;
       
    58 	socket->state = LM_SOCKET_STATE_CLOSED;
       
    59 	socket->ssl = NULL;
    55 	
    60 	
    56 	return socket;
    61 	return socket;
    57 }
    62 }
    58 
    63 
    59 static void
    64 static void
    60 socket_free (LmSocket *socket)
    65 socket_free (LmSocket *socket)
    61 {
    66 {
    62 	/* FIXME: Free up the rest of the memory */
    67 	/* FIXME: Free up the rest of the memory */
       
    68 
       
    69 	if (socket->ssl) {
       
    70 		_lm_ssl_unref (socket->ssl);
       
    71 	}
       
    72 
    63 	g_free (socket->host);
    73 	g_free (socket->host);
    64 
    74 
    65 	g_free (socket);
    75 	g_free (socket);
    66 }
    76 }
    67 
    77 
    73 	socket = socket_create ();
    83 	socket = socket_create ();
    74 
    84 
    75 	socket->funcs = funcs;
    85 	socket->funcs = funcs;
    76 	socket->host = g_strdup (host);
    86 	socket->host = g_strdup (host);
    77 	socket->port = port;
    87 	socket->port = port;
       
    88 	socket->is_blocking = FALSE;
    78 
    89 
    79 	return socket;
    90 	return socket;
    80 }
    91 }
    81 
    92 
    82 void
    93 void
    83 lm_socket_open (LmSocket *socket)
    94 lm_socket_open (LmSocket *socket)
    84 {
    95 {
       
    96 	g_return_if_fail (socket != NULL);
       
    97 
       
    98 	/* Fork and DNS Lookup */
    85 }
    99 }
    86 
   100 
    87 int
   101 int
    88 lm_socket_get_fd (LmSocket *socket)
   102 lm_socket_get_fd (LmSocket *socket)
    89 {
   103 {
   127 lm_socket_write (LmSocket   *socket,
   141 lm_socket_write (LmSocket   *socket,
   128 		 gsize       size,
   142 		 gsize       size,
   129 		 gchar      *buf,
   143 		 gchar      *buf,
   130 		 GError    **error)
   144 		 GError    **error)
   131 {
   145 {
       
   146 	gint b_written;
       
   147 	
       
   148 	g_return_val_if_fail (socket != NULL, -1);
       
   149 
       
   150 	if (socket->ssl) {
       
   151 		b_written = _lm_ssl_send (socket->ssl, buf, len);
       
   152 	} else {
       
   153 		GIOStatus io_status = G_IO_STATUS_AGAIN;
       
   154 		gsize     bytes_written = 0;
       
   155 
       
   156 		while (io_status == G_IO_STATUS_AGAIN) {
       
   157 			io_status = g_io_channel_write_chars (socket->channel,
       
   158 							      buf, size,
       
   159 							      &bytes_written,
       
   160 							      NULL);
       
   161 		}
       
   162 
       
   163 		b_written = bytes_written;
       
   164 
       
   165 		if (io_status != G_IO_STATUS_NORMAL) {
       
   166 			b_written = -1;
       
   167 		}
       
   168 	}
       
   169 
       
   170 	return b_written;
   132 }
   171 }
   133 
   172 
   134 int
   173 int
   135 lm_socket_read (LmSocket   *socket,
   174 lm_socket_read (LmSocket   *socket,
   136 		gsize       size,
   175 		gsize       size,
   137 		gchar      *buf,
   176 		gchar      *buf,
   138 		GError    **error)
   177 		GError    **error)
   139 {
   178 {
       
   179 	gsize     bytes_read = 0;
       
   180 	GIOStatus status =  G_IO_STATUS_AGAIN;
       
   181 
       
   182 	g_return_val_if_fail (socket != NULL, -1);
       
   183 
       
   184 	while (status == G_IO_STATUS_AGAIN) {
       
   185 		if (socket->ssl) {
       
   186 			status = _lm_ssl_read (socket->ssl, 
       
   187 					       buf, size, &bytes_read);
       
   188 		} else {
       
   189 			status = g_io_channel_read_chars (socket->channel, 
       
   190 							  buf, size, 
       
   191 							  &bytes_read,
       
   192 							  NULL);
       
   193 		}
       
   194 	}
       
   195 
       
   196 	if (status != G_IO_STATUS_NORMAL || bytes_read < 0) {
       
   197 		/* FIXME: Set error */
       
   198 
       
   199 		return -1;
       
   200 	}
       
   201 
       
   202 	return bytes_read;
   140 }
   203 }
   141 
   204 
   142 gboolean
   205 gboolean
   143 lm_socket_close (LmSocket *socket, GError **error)
   206 lm_socket_close (LmSocket *socket, GError **error)
   144 {
   207 {
   151 
   214 
   152 LmSocket *
   215 LmSocket *
   153 lm_socket_ref (LmSocket *socket)
   216 lm_socket_ref (LmSocket *socket)
   154 {
   217 {
   155 	g_return_val_if_fail (socket != NULL, NULL);
   218 	g_return_val_if_fail (socket != NULL, NULL);
       
   219 
   156 	socket->ref++;
   220 	socket->ref++;
   157 	
   221 	
   158 	return socket;
   222 	return socket;
   159 }
   223 }
   160 
   224