-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 ;)
Não descobri como impedir o commit do readme feito pelo gihub. |
There was a problem hiding this 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.
|
||
Message(std::string send, std::string sent, std::string mess) | ||
: send_to { std::move(send) } |
There was a problem hiding this comment.
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
#define SOCKET_NAME "/tmp/server.socket" | ||
#define STD_SIZE 128 |
There was a problem hiding this comment.
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.
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); |
There was a problem hiding this comment.
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.
|
||
static int transfer(Message com, int* sockets, int size); | ||
|
||
static Message strToMsg(char* com); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mesma coisa do static.
/** 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; | ||
} |
There was a problem hiding this comment.
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.
|
||
if (atoi(buffer) == -1) { | ||
printf("message not sent\n"); | ||
} | ||
else if (atoi(buffer) == 0) { | ||
printf("destination unavailable\n"); | ||
} | ||
|
There was a problem hiding this comment.
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.
int SocketHandler::sendMessage(int socket, SocketHandler::Message com) { | ||
std::string men = com.send_to + "," + com.sent_from + "," + com.message; | ||
|
There was a problem hiding this comment.
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.
Added socket handler class, with functions for the communication between server and clients through unix sockets.