clib/test/project/server_proj/server_proj.c
2023-11-02 22:53:52 +08:00

144 lines
3.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <ssocket.h>
#include <tthread.h>
#include <iini.h>
#include <llog.h>
#define BUFFER_SIZE 128
#define CLIENT_SIZE 32
SOCKET sfd;
SOCKET cfds[CLIENT_SIZE];
MUTEX std_mutex;
char buf[BUFFER_SIZE];
const log_mask_t g_mask = LOG_MASK_BASE_DEFAULT;
#define _STR(str) #str
#define STR(str) _STR(str)
void send_the_message(int sock, const char *Buff, int szBuf)
{
char Buf[2048] = {0};
int tempsock;
sprintf(Buf, "<from %d>:", sock);
strcat(Buf, Buff);
for(int i = 0; i < CLIENT_SIZE; i++) {
if(cfds[i] != 0 && cfds[i] != sock) {
tempsock = cfds[i];
send(tempsock, Buf, strlen(Buf)+1, 0);
}
}
}
void receive_message(void* param)
{
int res;
char Buf[1024] = {0};
int sock = *((SOCKET*)param);
free(param);
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "recv start %d\n", sock);
while(1) {
res = recv(sock, Buf, sizeof(Buf), 0);
if(res == -1 || res > sizeof(Buf) || res == 0) {
log_head_error_ex(buf, BUFFER_SIZE, "receive", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "recv error %d\n", sock);
break;
}
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "recv buf:%d,%s", res, Buf);
send_the_message(sock, Buf, strlen(Buf)+1);
}
log_head_info_ex(buf, BUFFER_SIZE, "receive", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "socket <%d> exit\n", sock);
for(int i = 0; i < CLIENT_SIZE; i++) {
if(cfds[i] == sock) {
cfds[i] = 0;
}
}
}
void acceptfunc(void* param) {
char *ip;
unsigned short port;
SOCKET* psock;
SOCKET sock;
psock = (SOCKET*)malloc(sizeof(SOCKET));
if(psock == NULL) {
log_head_fatal_ex(buf, BUFFER_SIZE, "accept", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "server start error... press enter to continue\n");
exit(-1);
}
while(1) {
if (sock_accpet(sfd, &sock, &ip, &port) != 0) {
continue;
}
psock = (SOCKET*)malloc(sizeof(SOCKET));
if (psock == NULL) {
log_head_fatal_ex(buf, BUFFER_SIZE, "accept", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "malloc error... press enter to continue\n");
if (getchar());
exit(-1);
}
*psock = sock;
for(int i = 0; i < CLIENT_SIZE; i++) {
if(cfds[i] == 0) {
cfds[i] = sock;
log_head_info_ex(buf, BUFFER_SIZE, "accept", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "<ip = %s, port = %u, socket = %lld>\n", ip, port, sock);
free(ip);
thread_create(NULL, receive_message, (void*)psock);
goto CONTINUE;
}
}
log_head_warn_ex(buf, BUFFER_SIZE, "accept", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "client full");
CONTINUE:
psock = NULL;
}
}
int main()
{
char param[2][BUFFER_SIZE] = { 0 };
ini_get_str("server.ini", "base", "server_ip", _SOCKET_TEST_IP4, param[0], BUFFER_SIZE);
ini_get_str("server.ini", "base", "server_port", STR(_SOCKET_TEST_PORT), param[1], BUFFER_SIZE);
thread_mutex_init(&std_mutex);
int res = make_server_sock_ex(&sfd, param[0], param[1], NULL);
if(res != 0) {
log_head_fatal_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "server start error... press enter to continue\n");
if(getchar());
return 0;
}
log_head_info_ex(buf, BUFFER_SIZE, "ssocket", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "server start... [%s:%s]\n", param[0], param[1]);
thread_create(NULL, acceptfunc, NULL);
while(1) {
fgets(buf, sizeof(buf), stdin);
if(strncasecmp(buf, "exit", strlen("exit")) == 0) {
log_head_info_ex(buf, BUFFER_SIZE, "command", g_mask);
tprintf(&std_mutex, buf);
tprintf(&std_mutex, "server exit... press enter to continue\n");
if(getchar());
exit(-1);
}
}
return 0;
}