mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-16 17:27:33 +02:00
%make-client-socket compiles
This commit is contained in:
parent
765d2fb663
commit
163c2cdfc8
1 changed files with 67 additions and 24 deletions
91
srfi/106.sld
91
srfi/106.sld
|
@ -5,6 +5,7 @@
|
||||||
(include-c-header "<netinet/in.h>")
|
(include-c-header "<netinet/in.h>")
|
||||||
(include-c-header "<arpa/inet.h>")
|
(include-c-header "<arpa/inet.h>")
|
||||||
(include-c-header "<netdb.h>")
|
(include-c-header "<netdb.h>")
|
||||||
|
(include-c-header "<unistd.h>")
|
||||||
(import (scheme base))
|
(import (scheme base))
|
||||||
(export
|
(export
|
||||||
make-client-socket make-server-socket socket?
|
make-client-socket make-server-socket socket?
|
||||||
|
@ -28,39 +29,81 @@
|
||||||
)
|
)
|
||||||
(begin
|
(begin
|
||||||
;; TODO: identifier for socket in vector
|
;; TODO: identifier for socket in vector
|
||||||
|
(define *socket-object-type* 'socket-object-type)
|
||||||
|
|
||||||
;; see: http://gnosis.cx/publish/programming/sockets.html
|
;; see:
|
||||||
|
;; http://gnosis.cx/publish/programming/sockets.html
|
||||||
|
;; http://beej.us/guide/bgnet/output/html/multipage/getaddrinfoman.html
|
||||||
(define-c %make-client-socket
|
(define-c %make-client-socket
|
||||||
"(void *data, int argc, closure _, object k,
|
"(void *data, int argc, closure _, object k,
|
||||||
object node, object service,
|
object anode, object aservice,
|
||||||
object family, object socktype,
|
object family, object socktype,
|
||||||
object flags, object protocol)"
|
object aflags, object protocol)"
|
||||||
;; TODO: how to pack socket objects?
|
" int sockfd = 0;
|
||||||
;; can we put sock fd in a vector, along with an identifier?
|
//struct sockaddr_in addr;
|
||||||
" int sock;
|
struct addrinfo hints, *servinfo, *p;
|
||||||
struct sockaddr_in addr;
|
const char *node = string_str(anode),
|
||||||
int af = obj_obj2int(family),
|
*service = string_str(aservice);
|
||||||
|
int rv,
|
||||||
|
flags = obj_obj2int(aflags),
|
||||||
|
af = obj_obj2int(family),
|
||||||
type = obj_obj2int(socktype),
|
type = obj_obj2int(socktype),
|
||||||
proto = obj_obj2int(protocol);
|
proto = obj_obj2int(protocol);
|
||||||
// TODO: put trace statement here
|
make_pair(sockobj, quote_socket_91object_91type, NULL);
|
||||||
// TODO: type check args to this function
|
|
||||||
|
|
||||||
if ((sock = socket(af, type, proto)) < 0) {
|
memset(&hints, 0, sizeof hints);
|
||||||
Cyc_rt_raise_msg(data, \"Failed to create socket\");
|
hints.ai_flags = flags;
|
||||||
|
hints.ai_family = af;
|
||||||
|
hints.ai_socktype = type;
|
||||||
|
hints.ai_protocol = proto;
|
||||||
|
|
||||||
|
if ((rv = getaddrinfo(node, service, &hints, &servinfo)) != 0) {
|
||||||
|
char buffer[1024];
|
||||||
|
snprintf(buffer, 1023, \"getaddrinfo: %s\", gai_strerror(rv));
|
||||||
|
Cyc_rt_raise_msg(data, buffer);
|
||||||
}
|
}
|
||||||
memset(&addr 0, sizeof(addr)); /* Clear struct */
|
|
||||||
// TODO: flags?
|
// loop through all the results and connect to the first we can
|
||||||
addr = af;
|
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||||
addr = inet_addr(string_str(node)); /* IP address */
|
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||||
addr = htons(obj_obj2int(service)); /* server port */
|
p->ai_protocol)) == -1) {
|
||||||
/* Establish connection */
|
//perror(\"socket\");
|
||||||
if (connect(sock,
|
continue;
|
||||||
(struct sockaddr *) &addr
|
}
|
||||||
sizeof(addr)) < 0) {
|
|
||||||
Cyc_rt_raise_msg(data, \"Failed to connect with server\");
|
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
||||||
|
//perror(\"socket\");
|
||||||
|
close(sockfd);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break; // if we get here, we must have connected successfully
|
||||||
}
|
}
|
||||||
// TODO: pack socket, and pass that to k. check code in Cyc_make_vector
|
|
||||||
return_closcall1(data, k, obj_int2obj()); ")
|
if (p == NULL) {
|
||||||
|
// looped off the end of the list with no connection
|
||||||
|
Cyc_rt_raise_msg(data, \"failed to connect client socket\");
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(servinfo); // all done with this structure
|
||||||
|
|
||||||
|
// if ((sock = socket(af, type, proto)) < 0) {
|
||||||
|
// Cyc_rt_raise_msg(data, \"Failed to create socket\");
|
||||||
|
// }
|
||||||
|
// memset(&addr, 0, sizeof(addr)); /* Clear struct */
|
||||||
|
// // TODO: flags?
|
||||||
|
// addr.sin_family = af;
|
||||||
|
// addr.sin_addr.s_addr = inet_addr(string_str(node)); /* IP address */
|
||||||
|
// addr.sin_port = htons(obj_obj2int(service)); /* server port */
|
||||||
|
// /* Establish connection */
|
||||||
|
// if (connect(sock,
|
||||||
|
// (struct sockaddr *) &addr,
|
||||||
|
// sizeof(addr)) < 0) {
|
||||||
|
// Cyc_rt_raise_msg(data, \"Failed to connect with server\");
|
||||||
|
// }
|
||||||
|
|
||||||
|
cdr(&sockobj) = obj_int2obj(sockfd);
|
||||||
|
return_closcall1(data, k, &sockobj); ")
|
||||||
|
|
||||||
(define-syntax make-const
|
(define-syntax make-const
|
||||||
(er-macro-transformer
|
(er-macro-transformer
|
||||||
|
|
Loading…
Add table
Reference in a new issue