Almost at the point of issueing move commands just have some bugs
This commit is contained in:
parent
797cfeca05
commit
535e2a60f6
9 changed files with 86 additions and 10 deletions
17
src/client/command_cli.c
Normal file
17
src/client/command_cli.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include "command_cli.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Command *cmd__create() {
|
||||
Command initialCommand = COMMAND__INIT;
|
||||
Command *command = malloc(sizeof(initialCommand));
|
||||
memcpy(&initialCommand, command, sizeof(initialCommand));
|
||||
return command;
|
||||
}
|
||||
|
||||
void cmd__add_move(Command *command, move_direction direction) {
|
||||
// command->has_move_direction = 1;
|
||||
command->move_direction = direction;
|
||||
}
|
||||
|
||||
void cmd__destroy(Command *command) { free(command); }
|
||||
13
src/client/command_cli.h
Normal file
13
src/client/command_cli.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "world_structures.h"
|
||||
#include <../src/protobuf_gen/commands.pb-c.h>
|
||||
|
||||
//FIX: Create malloced command struct to be edited before being sent
|
||||
Command *cmd__create();
|
||||
|
||||
// Add movement direction to command struct
|
||||
void cmd__add_move(Command* command, move_direction direction);
|
||||
|
||||
// Free command struct when we're done with it
|
||||
void cmd__destroy(Command* command);
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
// Sorry in advance for all the comments. This project is also a way for me to
|
||||
// learn C.
|
||||
|
||||
#include "error.h"
|
||||
#include "message.h"
|
||||
#include <unistd.h>
|
||||
#include "command_cli.h"
|
||||
#include <error.h>
|
||||
#include <message.h>
|
||||
#include <netdb.h> // Contains host structure
|
||||
#include <netinet/in.h> //sockaddr_in
|
||||
#include <stdio.h>
|
||||
|
|
@ -12,6 +12,8 @@
|
|||
#include <strings.h>
|
||||
#include <sys/socket.h> // sockaddr
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <world_structures.h>
|
||||
|
||||
// void error_crash(const char *msg) {
|
||||
// fprintf(stderr, "error crash: %s\n", msg);
|
||||
|
|
@ -60,6 +62,11 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
msg__req_login(&sockfd, "username", "passwd");
|
||||
|
||||
Command *cmd = cmd__create();
|
||||
cmd__add_move(cmd, WEST);
|
||||
msg__req_command(&sockfd, cmd);
|
||||
cmd__destroy(cmd);
|
||||
|
||||
while (1) {
|
||||
bzero(msgbuffer, sizeof msgbuffer);
|
||||
fgets(msgbuffer, sizeof msgbuffer, stdin);
|
||||
|
|
|
|||
|
|
@ -28,9 +28,10 @@ void cmd__move_direction(move_direction direction, Room *current_room) {
|
|||
}
|
||||
}
|
||||
|
||||
void cmd__recv_proc_cmd(Player *player, Command *command) {
|
||||
void cmd__process_cmd(Player *player, Command *command) {
|
||||
if (command->has_move_direction) {
|
||||
move_direction dir = {command->move_direction};
|
||||
cmd__move_direction(dir, player->current_location);
|
||||
}
|
||||
command__free_unpacked(command, NULL);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
#include <world_structures.h>
|
||||
|
||||
// Will process a command given a player to execute it as
|
||||
void cmd__recv_proc_cmd(Player *player, Command *command);
|
||||
void cmd__process_cmd(Player *player, Command *command);
|
||||
|
||||
// Update room pointer based on move direction
|
||||
void cmd__move_direction(move_direction direction, Room *current_room);
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "world.h"
|
||||
#include "command.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Set up containers for file descriptor id's
|
||||
|
|
@ -31,6 +32,8 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
// Create world
|
||||
Room* world = wrld__create();
|
||||
Player player;
|
||||
player.current_location = &world[0];
|
||||
|
||||
// Create socket
|
||||
serv_sockfd = msg__create_serv_connection(portno);
|
||||
|
|
@ -41,6 +44,11 @@ int main(int argc, char *argv[]) {
|
|||
// Require login as first request
|
||||
msg__recv_login(&cli_sockfd);
|
||||
|
||||
//HACK: test recieve of movement command
|
||||
err__log(player.current_location->name);
|
||||
cmd__process_cmd(&player, msg__recv_command(&cli_sockfd));
|
||||
err__log(player.current_location->name);
|
||||
|
||||
while (1) {
|
||||
// Empty message buffer
|
||||
bzero(msgbuffer, sizeof msgbuffer);
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ int msg__accept_cli_connection(int server_sockfd);
|
|||
void msg__recv_login(int *con_sockfd);
|
||||
void msg__req_login(int *con_sockfd, char *usernm, char *passwd);
|
||||
|
||||
// send and recive commands
|
||||
// Recive commands, This creates a Command object that must be freed using
|
||||
// command__free_unpacked() once processed.
|
||||
Command *msg__recv_command(int *con_sockfd);
|
||||
void msg__req_command(int *con_sockfd, char *usernm, char *passwd);
|
||||
|
||||
// Sends a preconfigured command to the server
|
||||
void msg__req_command(int *con_sockfd, Command* command);
|
||||
|
|
|
|||
|
|
@ -7,3 +7,6 @@ void read_from_sock(uint8_t *recvbuf, unsigned bufsize, int *sockfd);
|
|||
|
||||
// One method to see how much data is in a buffer to unpack
|
||||
size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out);
|
||||
|
||||
// Send msg buffer to the server, crash on fail.
|
||||
void msg__send_msgbuf(int *con_sockfd, uint8_t *msgbuf, size_t msgbuf_size);
|
||||
|
|
|
|||
|
|
@ -1,12 +1,25 @@
|
|||
#include "message.h"
|
||||
#include "error.h"
|
||||
#include "protobuf_gen/commands.pb-c.h"
|
||||
#include "world_structures.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_MSG_SIZE 1024
|
||||
#define MAX_MSG_SIZE 4096
|
||||
|
||||
void msg__send_msgbuf(int *con_sockfd, uint8_t *msgbuf, size_t msgbuf_size) {
|
||||
if (msgbuf_size > MAX_MSG_SIZE) {
|
||||
err__crash("Msg buffer too big to send");
|
||||
free(msgbuf);
|
||||
}
|
||||
|
||||
if (write(*con_sockfd, msgbuf, msgbuf_size) < 0) {
|
||||
err__crash("Couldn't write message buffer to server");
|
||||
free(msgbuf);
|
||||
}
|
||||
}
|
||||
|
||||
void msg__amsg_test(int a) {
|
||||
AMessage msg = AMESSAGE__INIT; // The massage format
|
||||
AMessage msg = AMESSAGE__INIT; // The message format
|
||||
void *buf; // Buffer to store serialized data
|
||||
unsigned len; // length of serialized data
|
||||
|
||||
|
|
@ -161,7 +174,18 @@ Command *msg__recv_command(int *con_sockfd) {
|
|||
|
||||
return cmd;
|
||||
}
|
||||
void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {}
|
||||
|
||||
void msg__req_command(int *con_sockfd, Command *command) {
|
||||
void *msgbuf;
|
||||
unsigned len;
|
||||
|
||||
// FIX: Figure out why this malloced structure won't work here
|
||||
len = command__get_packed_size(command);
|
||||
msgbuf = malloc(len);
|
||||
command__pack(command, msgbuf);
|
||||
|
||||
msg__send_msgbuf(con_sockfd, msgbuf, len);
|
||||
}
|
||||
|
||||
int msg__accept_cli_connection(int server_sockfd) {
|
||||
struct sockaddr_in cli_addr;
|
||||
|
|
|
|||
Loading…
Reference in a new issue