Changeset 21 in libcf
- Timestamp:
- 02/01/13 17:46:09 (12 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_debug.h
r16 r21 33 33 do { \ 34 34 CF_UPDATE_CTX (__ctx); \ 35 CF_Debug_Trace (__ctx,__bin,__len,__fmt,##__VA_ARGS__);\35 CF_Debug_TraceBin (__ctx,__bin,__len,__fmt,##__VA_ARGS__); \ 36 36 } while (0) 37 37 # define CF_DEBUG_CALLSTACK_PUSH(__ctx) \ -
trunk/include/cf_socket.h
r6 r21 7 7 #include "cf_base.h" 8 8 #include <stddef.h> 9 10 #ifdef _WIN32 11 # include <WinSock2.h> 12 #else 13 # include <netinet/in.h> 14 # include <sys/socket.h> 15 # include <arpa/inet.h> 16 # include <netdb.h> 17 # include <unistd.h> 18 #endif 9 19 10 20 #define CF_ERROR_SOCKET_INITIALIZE CF_ERROR_SOCKET - 1 … … 71 81 72 82 CF_EXPORT int 73 CF_Socket_Accept (const int sock,74 void * address /* struct sockaddr_in * */);83 CF_Socket_Accept (const int sock, 84 struct sockaddr_in * address); 75 85 76 86 CF_EXPORT int -
trunk/src/cf_socket.c
r11 r21 6 6 7 7 #ifdef _WIN32 8 # include <WinSock2.h>9 8 # pragma comment (lib, "ws2_32.lib") 10 9 # define close(__sock) closesocket(__sock) 11 10 # define sa_family_t unsigned short 12 11 #else 13 # include <netinet/in.h>14 # include <sys/socket.h>15 # include <arpa/inet.h>16 # include <netdb.h>17 # include <unistd.h>18 12 #endif 19 13 … … 216 210 } 217 211 218 return CF_OK;212 return sock; 219 213 } 220 214 … … 262 256 263 257 int 264 CF_Socket_Accept (const int sock,265 void* address)258 CF_Socket_Accept (const int sock, 259 struct sockaddr_in * address) 266 260 { 267 261 int sockClient; -
trunk/src/cf_thread.c
r8 r21 27 27 28 28 int 29 CF_ CLOSE_CTX(void * ctx)29 CF_Thread_Local_Close (void * ctx) 30 30 { 31 31 #ifdef _WIN32 … … 67 67 CHECK_INVALID_THREAD (*threadID); 68 68 69 CF_ CLOSE_CTX(*threadID);69 CF_Thread_Local_Close (*threadID); 70 70 71 71 return CF_OK; … … 80 80 WaitForSingleObject ((THREAD_TYPE) *threadID, INFINITE); 81 81 #else 82 int status ;83 pthread_join (*((THREAD_TYPE *) *threadID), (void *)&status);82 int status = 0; 83 pthread_join (*((THREAD_TYPE *) *threadID), &status); 84 84 #endif 85 85 … … 114 114 CHECK_INVALID_MUTEX (*mutex); 115 115 116 CF_ CLOSE_CTX(*mutex);116 CF_Thread_Local_Close (*mutex); 117 117 118 118 return CF_OK; -
trunk/test/test.c
r19 r21 9 9 10 10 #include <stdlib.h> 11 #include <string.h> 12 #include <errno.h> 13 14 #define THREAD_POOL 5 15 #define PORT 1234 11 16 12 17 CF_Debug_Ctx gDebugCtx; 13 18 CF_Debug_CallStack gDebugCallstack; 14 19 20 CF_Log_Ctx gLogCtx; 21 15 22 const char * file = "./log.txt"; 16 23 … … 19 26 int i, j; 20 27 char idname[16] = {0x00,}; 21 CF_Log_Ctx gLogCtx; 22 23 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 24 28 29 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 30 31 /* initialize */ 25 32 CF_Log_Initialize (10); 26 33 34 /* context {{{ */ 27 35 gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT); 28 36 if (gLogCtx == NULL) … … 41 49 42 50 CF_Log_DestroyCtx (gLogCtx); 43 44 /////////////////// 45 51 /* }}} context */ 52 53 /* id {{{ */ 46 54 for (i = 0 ; i < 10 ; i++) 47 55 { … … 57 65 CF_LOG_CLOSE (i); 58 66 } 59 67 /* }}} id */ 68 69 /* finalize */ 60 70 CF_Log_Finalize (); 61 71 } … … 63 73 void file_test (const char * message) 64 74 { 65 int size = 0;66 75 int fd = 0; 67 char * buffer = NULL;76 char buffer[128] = {0x00,}; 68 77 69 78 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); … … 73 82 CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n"); 74 83 75 size = CF_File_GetSize (fd); 76 if (size < 0) 77 CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n"); 78 79 buffer = (char *) calloc (1, (size_t) (size + 1)); 80 if (buffer == NULL) 81 CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n"); 82 83 if (CF_File_Read (fd, buffer, (size_t)size) < 0) 84 if (CF_File_Read (fd, buffer, sizeof (buffer) - 1) < 0) 84 85 CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n"); 85 86 else 86 CF_DEBUG_TRACE (gDebugCtx, "%s", buffer); 87 CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer, 88 sizeof (buffer) - 1, 89 "-- %d bytes of %d bytes\n", 90 sizeof (buffer) - 1, 91 CF_File_GetSize (fd)); 87 92 88 93 CF_File_Close (fd); … … 116 121 } 117 122 123 CF_THREAD_RETURN CF_THREAD_CALL socket_echo_server (void * arg) 124 { 125 int srvsock = *((int *)arg); 126 int clntsock = 0; 127 int recvd = 0; 128 char buf[1024] = {0x00,}; 129 130 /*------------------------------------------------------------*/ 131 clntsock = CF_Socket_Accept (srvsock, NULL); 132 if (clntsock < 0) 133 { 134 CF_DEBUG_PRINT (stderr, "failed to accept on server\n"); 135 return (CF_THREAD_RETURN)-1; 136 } 137 CF_DEBUG_PRINT (stderr, "SERVER : accepted\n"); 138 CF_Log_Write (gLogCtx, "SERVER", "accepted\n"); 139 140 if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0) 141 { 142 CF_DEBUG_PRINT (stderr, "failed to recv on server\n"); 143 return (CF_THREAD_RETURN)-2; 144 } 145 CF_DEBUG_PRINT (stderr, "SERVER : recv {%s}\n", buf); 146 CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf); 147 148 if (CF_Socket_Send (clntsock, buf, recvd) < 0) 149 { 150 CF_DEBUG_PRINT (stderr, "failed to send on server\n"); 151 return (CF_THREAD_RETURN)-3; 152 } 153 CF_DEBUG_PRINT (stderr, "SERVER : resp {%s}\n", buf); 154 CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf); 155 156 return (CF_THREAD_RETURN)0; 157 /*------------------------------------------------------------*/ 158 } 159 160 CF_THREAD_RETURN CF_THREAD_CALL socket_echo_client (void * arg) 161 { 162 int sock = 0; 163 int recvd = 0; 164 char buf[1024] = {0x00,}; 165 166 sprintf (buf, "...wow ? is it succeed ?"); 167 168 /*------------------------------------------------------------*/ 169 sock = CF_Socket_Connect ("localhost", PORT); 170 if (sock < 0) 171 { 172 CF_DEBUG_PRINT (stderr, "failed to connect on client\n"); 173 return (CF_THREAD_RETURN)-1; 174 } 175 CF_DEBUG_PRINT (stderr, "CLIENT : connected\n"); 176 CF_Log_Write (gLogCtx, "CLIENT", "connected\n"); 177 178 if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0) 179 { 180 CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno); 181 return (CF_THREAD_RETURN)-2; 182 } 183 CF_DEBUG_PRINT (stderr, "CLIENT : sent {%s}\n", buf); 184 CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf); 185 186 memset (buf, 0x00, sizeof (buf)); 187 188 if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0) 189 { 190 CF_DEBUG_PRINT (stderr, "failed to recv on client\n"); 191 return (CF_THREAD_RETURN)-3; 192 } 193 CF_DEBUG_PRINT (stderr, "CLIENT : recv {%s}\n", buf); 194 CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf); 195 196 CF_Socket_Close (sock); 197 198 return (CF_THREAD_RETURN)0; 199 /*------------------------------------------------------------*/ 200 } 201 202 void socket_test (void) 203 { 204 CF_Thread tid[THREAD_POOL]; 205 206 int sock = 0; 207 int iter = 0; 208 int reuse = 1; 209 210 /*------------------------------------------------------------*/ 211 gLogCtx = CF_Log_CreateCtx ("socket.txt", CF_LOG_BUFFER_DEFAULT); 212 if (gLogCtx == NULL) 213 CF_DEBUG_PRINT (stderr, "failed to open log\n"); 214 215 CF_Log_Write (gLogCtx, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n"); 216 217 if (CF_Socket_Initialize () < 0) 218 { 219 CF_DEBUG_PRINT (stderr, "failed to initialize socket\n"); 220 return ; 221 } 222 CF_Log_Write (gLogCtx, "SOCKET", "socket initialized\n"); 223 224 sock = CF_Socket_Server (PORT, 5); 225 if (sock < 0) 226 { 227 CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock); 228 return ; 229 } 230 CF_Log_Write (gLogCtx, "SOCKET", "socket ready\n"); 231 232 if (CF_Socket_SetOption (sock, SO_REUSEADDR, &reuse, sizeof (reuse)) < 0) 233 { 234 CF_DEBUG_PRINT (stderr, "failed to set option\n"); 235 return ; 236 } 237 CF_Log_Write (gLogCtx, "SOCKET", "set socket option\n"); 238 239 for (iter = 0 ; iter < THREAD_POOL ; iter++) 240 { 241 if (CF_Thread_Create (&tid[iter], socket_echo_server, &sock) < 0) 242 { 243 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter); 244 return ; 245 } 246 CF_Log_Write (gLogCtx, "SOCKET", "create server thread-%d\n", iter); 247 } 248 249 for (iter = 0 ; iter < THREAD_POOL ; iter++) 250 { 251 CF_Thread dummy; 252 if (CF_Thread_Create (&dummy, socket_echo_client, &sock) < 0) 253 { 254 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter); 255 return ; 256 } 257 CF_Log_Write (gLogCtx, "SOCKET", "create client thread-%d\n", iter); 258 } 259 260 for (iter = 0 ; iter < THREAD_POOL ; iter++) 261 { 262 int st; 263 CF_DEBUG_PRINT (stderr, "tid[%d] 0x%08x\n", iter, tid[iter]); 264 CF_Thread_Join (&tid[iter]); 265 // pthread_join (*((pthread_t *)*((CF_Thread *)&tid[iter])), &st); 266 CF_Thread_Release (&tid[iter]); 267 CF_Log_Write (gLogCtx, "SOCKET", "join server thread-%d\n", iter); 268 } 269 270 CF_Socket_Close (sock); 271 272 CF_Socket_Finalize (); 273 274 CF_Log_DestroyCtx (gLogCtx); 275 /*------------------------------------------------------------*/ 276 } 277 118 278 int main (int argc, char ** argv) 119 279 { … … 122 282 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 123 283 284 // 1 124 285 CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n"); 125 286 log_test ("LOG_TEST"); 126 287 288 // 2 127 289 CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n"); 128 290 file_test ("FILE_READ_TEST"); 129 291 292 // 3 130 293 CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n"); 131 294 callee1 (); 295 296 // 4 297 CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n"); 298 socket_test (); 132 299 133 300 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); … … 143 310 return 0; 144 311 } 312
Note:
See TracChangeset
for help on using the changeset viewer.