Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket: add socket class #35

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

socket: add socket class #35

wants to merge 2 commits into from

Conversation

guilherme-ls
Copy link
Member

Added socket handler class, with functions for the communication between server and clients through unix sockets.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thank you for opening your first Pull Request ! 🚀 Don't forget to read our README.md for contribution guidelines.
We'll be reviewing this PR soon ;)

@guilherme-ls guilherme-ls linked an issue Oct 30, 2022 that may be closed by this pull request
@guilherme-ls
Copy link
Member Author

Não descobri como impedir o commit do readme feito pelo gihub.

@guilherme-ls guilherme-ls reopened this Oct 30, 2022
@guilherme-ls guilherme-ls self-assigned this Oct 31, 2022
@Math-42 Math-42 self-requested a review October 31, 2022 23:46
@Grillo-0 Grillo-0 changed the base branch from main to develop October 31, 2022 23:50
@guilherme-ls guilherme-ls changed the title Added socket class socket: add socket class Nov 1, 2022
Copy link
Member

@Math-42 Math-42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No geral, remova todas as políticas de dentro do socket, deixe ele apenas como uma ferramenta de enviar e ler bytes e outras funcionalidades como bind, listen e etc, no geral pecou apenas pelo excesso.

Comment on lines +24 to +26

Message(std::string send, std::string sent, std::string mess)
: send_to { std::move(send) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trocar abreviação de message para message ou pra msg

Comment on lines +13 to +14
#define SOCKET_NAME "/tmp/server.socket"
#define STD_SIZE 128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remover esses defines trocar para atributos internos, trocar por atributos internos da classe.

Comment on lines +35 to +45
static int start();

static int openSocket(std::string name);

static int listenClient(SocketHandler::Message* com, struct timeval dropout_time);

static int listenServer(int* sockets, std::string* connection_list, int size, SocketHandler::Message* server_com, struct timeval dropout_time);

static int sendMessage(int socket, Message com);

static void closeSocket(int dis_socket);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não precisa ser static, se não não será possível criar mais de um socket.

Comment on lines +48 to +51

static int transfer(Message com, int* sockets, int size);

static Message strToMsg(char* com);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mesma coisa do static.

Comment on lines +256 to +276
/** Converts strings to Message format
* @param com string to be converted
* @return returns Message recovered from string
*/
SocketHandler::Message SocketHandler::strToMsg(char* com) {
std::string cut[3];
int i = 0, j = 0;
while (com[i] != '\0' && j < 3) {
if (com[i] != ',') {
cut[j] += com[i];
}
else {
j++;
}
i++;
}

SocketHandler::Message mes {cut[0], cut[1], cut[2]};

return mes;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A ideia do socket é que ele seja agnóstico e envie apena bytes, não precisa se preocupar nesse nível de implementação sobre converter strings, lidar com tipos de dados e etc. Nesse caso essa função não é necessária.

Comment on lines +224 to +231

if (atoi(buffer) == -1) {
printf("message not sent\n");
}
else if (atoi(buffer) == 0) {
printf("destination unavailable\n");
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apenas disparar o erro (throw), não precisa se preocupar imprimir o erro.

Comment on lines +213 to +215
int SocketHandler::sendMessage(int socket, SocketHandler::Message com) {
std::string men = com.send_to + "," + com.sent_from + "," + com.message;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como já dito, se preocupe em apenas enviar uma quantidade de n bytes, não precisa construir a mensagem ainda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Criar classe Socket
2 participants