diff options
author | Alexander Baldeck <alexander@archlinux.org> | 2004-12-26 14:46:39 +0000 |
---|---|---|
committer | Alexander Baldeck <alexander@archlinux.org> | 2004-12-26 14:46:39 +0000 |
commit | 0ad7cabf1feaa6af32f127254453f6b37d9fecc6 (patch) | |
tree | 7c4bae6232889672da3ff7bcf4fa042b37049daf /src/net/network.cpp | |
parent | f98f9d96ec60b5e64117f9f241f3a3a8fc300b7d (diff) | |
download | mana-0ad7cabf1feaa6af32f127254453f6b37d9fecc6.tar.gz mana-0ad7cabf1feaa6af32f127254453f6b37d9fecc6.tar.bz2 mana-0ad7cabf1feaa6af32f127254453f6b37d9fecc6.tar.xz mana-0ad7cabf1feaa6af32f127254453f6b37d9fecc6.zip |
- final touch to writing mechanism of the config system
- tmw now can read and write its ini file
- some fixes in login.cpp to get it to work properly
- Init method -> init
- Write method -> write
- minor code "beautification" in several files
Diffstat (limited to 'src/net/network.cpp')
-rw-r--r-- | src/net/network.cpp | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/src/net/network.cpp b/src/net/network.cpp index b030d92d..993abbc7 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -41,82 +41,86 @@ fd_set write_socket; /** Increase size of written data */ void WFIFOSET(int len) { - if(out_size+len>=buffer_size) - warning("Output buffer full"); - else out_size+=len; + if(out_size+len>=buffer_size) + warning("Output buffer full"); + else out_size+=len; } /** Convert an address from int format to string */ char *iptostring(int address) { - short temp1, temp2; - static char asciiIP[16]; + short temp1, temp2; + static char asciiIP[16]; - temp1 = LOWORD(address); - temp2 = HIWORD(address); - sprintf(asciiIP, "%i.%i.%i.%i", LOBYTE(temp1), HIBYTE(temp1), LOBYTE(temp2), HIBYTE(temp2)); - return asciiIP; + temp1 = LOWORD(address); + temp2 = HIWORD(address); + sprintf(asciiIP, "%i.%i.%i.%i", LOBYTE(temp1), HIBYTE(temp1), LOBYTE(temp2), HIBYTE(temp2)); + return asciiIP; } /** Open a session with a server */ SOCKET open_session(const char* address, short port) { - #ifdef WIN32 - WSADATA wsda; - #endif - struct hostent *server; - int ret; - - // Init WinSock and connect the socket - #ifdef WIN32 - WSAStartup(MAKEWORD(2,0), &wsda); - #endif - - sock = socket(PF_INET, SOCK_STREAM, 0); // Create socket for current session - if(sock==SOCKET_ERROR)return SOCKET_ERROR; - - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = inet_addr(address); - if(addr.sin_addr.s_addr == INADDR_NONE){ - server = NULL; - server = gethostbyname(address); - if(server == NULL)return SOCKET_ERROR; - memcpy(&addr.sin_addr, server->h_addr_list[0], server->h_length); - } - - ret = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); - if(ret == SOCKET_ERROR)return SOCKET_ERROR; - - // Init buffers - in = (char *)malloc(buffer_size); - out = (char *)malloc(buffer_size); - memset(in, '\0', buffer_size); - memset(out, '\0', buffer_size); - in_size = 0; - out_size = 0; - FD_CLR(sock, &read_socket); - FD_CLR(sock, &write_socket); - - return sock; + #ifdef WIN32 + WSADATA wsda; + #endif + struct hostent *server; + int ret; + + // Init WinSock and connect the socket + #ifdef WIN32 + WSAStartup(MAKEWORD(2,0), &wsda); + #endif + + sock = socket(PF_INET, SOCK_STREAM, 0); // Create socket for current session + if(sock==SOCKET_ERROR)return SOCKET_ERROR; + + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = inet_addr(address); + if(addr.sin_addr.s_addr == INADDR_NONE){ + server = NULL; + server = gethostbyname(address); + if(server == NULL)return SOCKET_ERROR; + memcpy(&addr.sin_addr, server->h_addr_list[0], server->h_length); + } + + ret = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); + if(ret == SOCKET_ERROR)return SOCKET_ERROR; + + // Init buffers + in = (char *)malloc(buffer_size); + out = (char *)malloc(buffer_size); + memset(in, '\0', buffer_size); + memset(out, '\0', buffer_size); + in_size = 0; + out_size = 0; + FD_CLR(sock, &read_socket); + FD_CLR(sock, &write_socket); + + return sock; } /** Close a session */ void close_session() { - FD_CLR(sock,&read_socket); - FD_CLR(sock,&write_socket); - closesocket(sock); - if(in!=NULL)free(in); - if(out!=NULL)free(out); - in = NULL; - out = NULL; - in_size = 0; - out_size = 0; - WSACleanup(); + FD_CLR(sock,&read_socket); + FD_CLR(sock,&write_socket); + closesocket(sock); + if(in!=NULL) { + free(in); + } + if(out!=NULL) { + free(out); + } + in = NULL; + out = NULL; + in_size = 0; + out_size = 0; + WSACleanup(); } /** Send and receive data waiting in the buffers */ void flush() { int ret = 0; - void *buf = out; //-kth5 + void *buf = out; timeval time_out; // Init the time_out struct to 0s so it won't block @@ -135,15 +139,11 @@ void flush() { // Send data if available if(FD_ISSET(sock, &write_socket)) { // While there wasn't a error or sent the whole data: handles partial packet send - while((ret!=SOCKET_ERROR)&&(out_size>0)) { + while((ret!=SOCKET_ERROR)&&(out_size>0)) { ret = send(sock, (char *)buf, out_size, 0); - /*FILE *file = fopen("log.log","wb"); - fprintf(file, "%s", out[0]); - fclose(file);*/ - // If not the whole data has been sent, empty the buffer from already sent bytes if(ret!=SOCKET_ERROR && ret>0) { - buf = (char*)buf+ret; //-kth5 + buf = (char*)buf+ret; out_size -= ret; } } @@ -151,7 +151,7 @@ void flush() { error("Socket Error"); #ifdef WIN32 log("Error", "Socket error: %i ", WSAGetLastError()); -#else +#else log("Error", "socket_error", "Undefined socket error"); #endif } @@ -159,16 +159,16 @@ void flush() { // Read data, if available if(FD_ISSET(sock, &read_socket)) { - /* There's no check for partial received packets because at this level - the app doesn't know packet length, but it will done when parsing received data */ - ret = recv(sock, in+in_size, RFIFOSPACE, 0); - if(ret==SOCKET_ERROR) { + /* There's no check for partial received packets because at this level + the app doesn't know packet length, but it will done when parsing received data */ + ret = recv(sock, in+in_size, RFIFOSPACE, 0); + if(ret==SOCKET_ERROR) { #ifdef WIN32 - log("Error", "Socket error: %i ", WSAGetLastError()); -#else - log("Error", "socket_error", "Undefined socket error"); + log("Error", "Socket error: %i ", WSAGetLastError()); +#else + log("Error", "socket_error", "Undefined socket error"); #endif - } else RFIFOSET(ret); // Set size of available data to read + } else RFIFOSET(ret); // Set size of available data to read } } |