@@ -19,15 +19,16 @@ bot_t *init_bot(char *name, void (*bot_main)(void *)){
1919 // set the bot name
2020 bot_t * bot = calloc (1 , sizeof (bot_t ));
2121 bot -> packet_threshold = DEFAULT_THRESHOLD ;
22+ bot -> buf = calloc (1 , DEFAULT_THRESHOLD );
2223 size_t len = strlen (name );
2324 bot -> name = calloc (len + 1 , sizeof (char ));
2425 strncpy (bot -> name , name , len + 1 );
2526 bot -> bot_main = bot_main ;
2627 // initialize the callback data structure
27- bot -> callbacks = calloc (NUM_STATES , sizeof (function * * ));
28- bot -> callbacks [HANDSHAKE ] = calloc (HANDSHAKE_PACKETS , sizeof (function * ));
29- bot -> callbacks [LOGIN ] = calloc (LOGIN_PACKETS , sizeof (function * ));
30- bot -> callbacks [PLAY ] = calloc (PLAY_PACKETS , sizeof (function * ));
28+ bot -> callbacks = calloc (NUM_STATES , sizeof (function * ));
29+ bot -> callbacks [HANDSHAKE ] = calloc (HANDSHAKE_PACKETS , sizeof (function ));
30+ bot -> callbacks [LOGIN ] = calloc (LOGIN_PACKETS , sizeof (function ));
31+ bot -> callbacks [PLAY ] = calloc (PLAY_PACKETS , sizeof (function ));
3132 return bot ;
3233}
3334
@@ -37,15 +38,15 @@ void free_bot(bot_t *bot){
3738 // unrolled outer loop just cuz
3839 int i ;
3940 for (i = 0 ; i < HANDSHAKE_PACKETS ; i ++ ){
40- function * func = bot -> callbacks [HANDSHAKE ][i ];
41+ function * func = & bot -> callbacks [HANDSHAKE ][i ];
4142 free_list (func );
4243 }
4344 for (i = 0 ; i < LOGIN_PACKETS ; i ++ ){
44- function * func = bot -> callbacks [LOGIN ][i ];
45+ function * func = & bot -> callbacks [LOGIN ][i ];
4546 free_list (func );
4647 }
4748 for (i = 0 ; i < PLAY_PACKETS ; i ++ ){
48- function * func = bot -> callbacks [PLAY ][i ];
49+ function * func = & bot -> callbacks [PLAY ][i ];
4950 free_list (func );
5051 }
5152 free (bot );
@@ -59,50 +60,33 @@ void free_list(function *list){
5960
6061
6162void register_event (bot_t * bot , uint32_t state , uint32_t packet_id ,
62- void (* f )(void * )){
63- function * current = bot -> callbacks [state ][packet_id ];
64- while (current )
65- current = current -> next ;
66- current = calloc (1 , sizeof (function ));
67- current -> f = f ;
63+ void (* f )(bot_t * , void * )){
64+ function * parent = & bot -> callbacks [state ][packet_id ];
65+ while (parent -> next )
66+ parent = parent -> next ;
67+ function * child = calloc (1 , sizeof (function ));
68+ parent -> f = f ;
69+ parent -> next = child ;
6870}
6971
7072// initializes a bot structure with a socket. The socket is bound to the local address on
7173// some port and is connected to the server specified by the server_host and server_port
7274// the socket descriptor is returned by the function. If -1 is returned, then an error
7375// occured, and a message will have been printed out.
7476
75- int join_server (bot_t * your_bot , char * local_port , char * server_host ,
76- char * server_port ){
77- int status ;
77+ int join_server (bot_t * your_bot , char * server_host , char * server_port ){
7878 struct addrinfo hints , * res ;
7979 int sockfd ;
80- memset (& hints , 0 , sizeof (hints ));
80+ // first, load up address structs with getaddrinfo():
81+ memset (& hints , 0 , sizeof hints );
8182 hints .ai_family = AF_UNSPEC ;
8283 hints .ai_socktype = SOCK_STREAM ;
83- if ((status = getaddrinfo (NULL , local_port , & hints , & res ))){
84- fprintf (stderr , "Your computer is literally haunted: %s\n" ,
85- gai_strerror (status ));
86- return -1 ;
87- }
88- if ((sockfd = socket (res -> ai_family , res -> ai_socktype , res -> ai_protocol )) == -1 ){
89- fprintf (stderr , "Could not create socket for unknown reason.\n" );
90- return -1 ;
91- }
92- freeaddrinfo (res );
93- // socket bound to local address/port
84+ getaddrinfo (server_host , server_port , & hints , & res );
9485
95- memset (& hints , 0 , sizeof (hints ));
96- hints .ai_family = AF_UNSPEC ;
97- hints .ai_socktype = SOCK_STREAM ;
98- if ((status = getaddrinfo (server_host , server_port , & hints , & res ))){
99- fprintf (stderr , "Server could not be resolved: %s\n" ,
100- gai_strerror (status ));
101- return -1 ;
102- }
86+ // make a socket and connect
87+ sockfd = socket (res -> ai_family , res -> ai_socktype , res -> ai_protocol );
10388 connect (sockfd , res -> ai_addr , res -> ai_addrlen );
104- freeaddrinfo (res );
105- // connected to server
89+
10690 your_bot -> socketfd = sockfd ;
10791 return sockfd ;
10892}
@@ -148,7 +132,7 @@ int receive_packet(bot_t *bot) {
148132 assert (i != len );
149133
150134 packet_size += len ;
151- received = len + 1 ;
135+ received = i + 1 ;
152136 if (packet_size <= bot -> packet_threshold ) {
153137 while (received < packet_size ) {
154138 ret = receive_raw (bot , bot -> buf + received , packet_size - received );
@@ -160,13 +144,17 @@ int receive_packet(bot_t *bot) {
160144 ret = peek_packet (bot , bot -> buf );
161145 return ret ;
162146 } else {
163- // read in a huge buffer, but throw it away
164- while (received < packet_size ) {
147+ // read in a huge buffer, packet_threshold at a time
148+ while (received < packet_size - bot -> packet_threshold ) {
165149 ret = receive_raw (bot , bot -> buf , bot -> packet_threshold );
166150 if (ret <= 0 )
167151 return -1 ;
168152 received += ret ;
169153 }
154+ // read the last portion of the packet
155+ ret = receive_raw (bot , bot -> buf , packet_size - received );
156+ if (ret <= 0 )
157+ return -1 ;
170158 return -2 ;
171159 }
172160}
0 commit comments