1 /*-------------------------------------------------------------------------
\r
3 * df32func extensions for Console Mode DataFlex 3.2
\r
5 * Copyright (c) 2007-2009, glyn@8kb.co.uk
\r
6 * Author: Glyn Astill <glyn@8kb.co.uk>
\r
8 *-------------------------------------------------------------------------
\r
11 #include "df32func.h"
\r
12 #include <windows.h>
\r
15 #include <winsock.h>
\r
17 SOCKET s, sc; /* Socket handle */
\r
20 * The number of cycles used by the processor since the start obtained on x86
\r
21 * processors (Intel, AMD), with the assembly command rdtsc.
\r
25 __asm__ __volatile__("rdtsc");
\r
29 * CLIENTSOCKET
\96 Creates a communication socket and connects to a remote host on the
\r
30 * supplied port and IP
\r
32 DLLIMPORT int ClientSocket(int PortNo, char* IPAddress){
\r
33 /* Start up Winsock */
\r
36 int error = WSAStartup(0x0202, &wsadata);
\r
38 /* Did something happen? */
\r
43 /* Did we get the right Winsock version? */
\r
44 if (wsadata.wVersion != 0x0202){
\r
45 WSACleanup(); /* Clean up Winsock */
\r
49 /* Fill out the information needed to initialize a socket */
\r
50 SOCKADDR_IN target; /* Socket address information */
\r
52 target.sin_family = AF_INET; /* address family Internet */
\r
53 target.sin_port = htons (PortNo); /* Port to connect on */
\r
54 target.sin_addr.s_addr = inet_addr (IPAddress); /* Target IP */
\r
56 s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Create socket */
\r
57 if (s == INVALID_SOCKET){
\r
58 return -1; /* Couldn't create the socket */
\r
61 /* Try connecting */
\r
62 if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR){
\r
63 return -1; /* Couldn't connect */
\r
66 return s; /* Success - return our socket number */
\r
71 * SERVERSOCKET
\96 Creates a communication socket and a tcp server listening on a the
\r
72 * supplied port number
\r
74 DLLIMPORT int ServerSocket(int PortNo){
\r
75 /* Start up Winsock */
\r
78 int error = WSAStartup(0x0202, &wsadata);
\r
80 /* Did something happen? */
\r
85 /* Did we get the right Winsock version? */
\r
86 if (wsadata.wVersion != 0x0202){
\r
87 WSACleanup(); /* Clean up Winsock */
\r
91 /* Fill out the information needed to initialize a socket */
\r
92 SOCKADDR_IN target; /* Socket address information */
\r
94 target.sin_family = AF_INET; /* address family Internet */
\r
95 target.sin_port = htons (PortNo); /* Port to connect on */
\r
96 target.sin_addr.s_addr = INADDR_ANY; /* Target IP */
\r
98 s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Create socket */
\r
99 if (s == INVALID_SOCKET){
\r
100 return -1; /* Couldn't create the socket */
\r
103 /* Try to bind to the socket */
\r
104 if (bind(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) {
\r
105 return -1; /* Couldn't connect */
\r
108 /* Now we can start listening, We allow SOMNAXCONN connections. This will not return until we get a connection */
\r
109 if (listen(s, SOMAXCONN) == SOCKET_ERROR) {
\r
119 * CLOSECONNECTION
\96 shuts down a communication socket and closes any connection on it
\r
121 DLLIMPORT void CloseConnection (int socket){
\r
122 /* Close the socket if it exists */
\r
124 closesocket(socket);
\r
127 WSACleanup(); /* Clean up Winsock */
\r
131 * SEND - Send data over the communication socket
\r
133 DLLIMPORT int Send(int socket, char* data){
\r
134 return send(socket, data, strlen(data), 0);
\r
138 * RECEIVE - Recieve data over the client socket
\r
140 DLLIMPORT int Receive(int socket, char *pData){
\r
141 char data[255] = {0};
\r
144 memset(data, 0, sizeof(data));
\r
145 bytesIn = recv(socket, data, sizeof(data), 0);
\r
146 if (bytesIn == SOCKET_ERROR) {
\r
149 sprintf (pData, "%s", data );
\r
154 * ACCEPTCLIENT - Accept a client connection
\r
156 DLLIMPORT int AcceptClient(){
\r
157 SOCKADDR_IN client;
\r
158 int clientSize = sizeof(client);
\r
159 sc = accept(s, (SOCKADDR *)&client, &clientSize);
\r
160 if (sc == INVALID_SOCKET){
\r
167 * Generate a pseudo random integer from an integer
\r
169 DLLIMPORT unsigned int PseudoRand(unsigned int w){
\r
170 unsigned int m_w = w;
\r
171 unsigned int m_z = (w / 2);
\r
173 m_z = 36969 * (m_z & 65535) + (m_z >> 16);
\r
174 m_w = 18000 * (m_w & 65535) + (m_w >> 16);
\r
175 return (m_z << 16) + m_w; /* 32-bit result */
\r
179 * Generate a random integer from the cpu rdtsc
\r
181 DLLIMPORT unsigned int RdtscRand(){
\r
182 unsigned int n = 0;
\r
184 /* First digit must be non-zero: */
\r
190 for(i = 1; i < 8; i++)
\r
202 BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
\r
203 DWORD reason /* Reason this function is being called. */ ,
\r
204 LPVOID reserved /* Not used. */ )
\r
208 case DLL_PROCESS_ATTACH:
\r
211 case DLL_PROCESS_DETACH:
\r
214 case DLL_THREAD_ATTACH:
\r
217 case DLL_THREAD_DETACH:
\r
221 /* Returns TRUE on success, FALSE on failure */
\r