a serious network situation

Posted By: pjotr987

a serious network situation - 10/30/12 10:58

hi guys, in my recent project i tried the network mode of the 3DGS. this causes me some headache now.
the idea behind my project is, that every client has to send an integer matrix (int test_int[20][20][3]) and a var matrix (var[20][20][3]) permanently to all clients. In short words every client has to send this 2 matrices and also receive data in same matrices from other clients permanently for visualisation.
My skills are sadly good anough just to make the script to send single variables but no matrix.

would be great if someone would have mercy with me and give a small and simple example code to do this network job. or atleast some productive suggestions

thanks in advance
pjotr
Posted By: 3dgs_snake

Re: a serious network situation - 10/30/12 11:20

Hi,

You can perhaps look at send_data_to
Posted By: pjotr987

Re: a serious network situation - 10/30/12 11:38

thanks for reply.
this send_data function seems to be suitable, but i dont understand it fully. as far as i understand from the reference i can understand the following code as following:

typedef struct MYDATA {
int test_int[20][20[3];
var _test_var[20][20[3];


} MYDATA;


that was the definition
so how to fill the metrices with data? i use 2 for loops to insert the data into the matrices. how to do it in this case?

then i send the data with from every client by

// sending data
send_data_to(NULL,mydata,sizeof(MYDATA));

with following function every client should get the data

//receiving data
function on_client_event(void* buffer)
{
if (event_type == EVENT_DATA)
memcpy(mydata,buffer,sizeof(MYDATA));
}

now the second question how to access the data stored in tis 2 matrices that were reiceved with the data?

would be greatefull for any help.
thanks
Posted By: 3dgs_snake

Re: a serious network situation - 10/30/12 12:05

Hi,

(Not tested wink )
Code:
typedef struct MYDATA
{
   int test_int[20][20][3];
   var test_var[20][20][3];
} MYDATA;

function fill_data(MYDATA *data)
{
   int i = 0;
   int j = 0;
   int k = 0;
   
   for (i = 0; i < 20; i++)
   {
      for (j = 0; j < 20; j++)
      {
        for (k = 0; k < 3; k++)
        {
           data->test_int[i][j][k] = i * j * k;
           data->test_var[i][j][k] = (var)((i * j * k) + 1);
        }
      }  
   }
}
...



You access the data using :
  • if using a normal variable : data.test_int[i][j][k]
  • if using a pointer : data->test_int[i][j][k]
Posted By: pjotr987

Re: a serious network situation - 10/30/12 15:46

thanks 3dgs_snake for the explanation,
but i am too dump
to fugure out several things.
1: how to call the function fill_data(MYDATA *data) function, so that the data can be filled. i insert this line in my client code:
send_data_to(NULL,mydata,sizeof(MYDATA)); but it doesnt arrive in another client.

2: how to send the data from a client to another one (if possible a simple function and how to call it)

3: how to receive the data and access it on another client(if possible a simple function and how to call it)

waiting desparately for help laugh

thanks in advance for any trial
Posted By: frankjiang

Re: a serious network situation - 10/31/12 02:40

I suggest write socket by yourself to do this things,Lite_c`s network is not good.
Code:
#include <string.h>
#include <WINSOCK2.H>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#include "conf/BufferConf.h"

#include "memPoolList.h"

#include "core/Queue.h"
/*
*
*	
*/
void startRun(ServerObject* t);





DWORD WINAPI listenThreadFun(LPVOID lpParam){
	

	ServerObject* se=Server_getinstance();
	queue* q=se->mProtocolQueue;
	void*pool=memPoolList_get();
	int re;
	while(true){
		re = QueueIsEmpty(q);
		if(re!=1 && se->mQueueLock==0){
			
			se->mQueueLock = 1;
			int i;
			QueueDelete(q,&i);
			SocketProtocol* sp = (SocketProtocol*)i;
			
			ParseData_get(sp);
			
			mempool_free(pool,sp->buffer);
			mempool_free(pool,sp);
			
			printfMemFree(pool);
			
			tool_printf(LOG_DEBUG,"bytes)ParseData_get finish...");
			
			Sleep(1);
			se->mQueueLock = 0;
		}
		Sleep(1);
	}
	return 0;
}
ServerObject* Server_getinstance(){
	if(ser==NULL){
		
		void* pool = memPoolList_get();
		
		ser=(ServerObject*)mempool_malloc(pool,sizeof(ServerObject));
		
		queue* q=QueueInit(10);

		ser->mProtocolQueue = q;
		
		ser->port = 6002;
		
		GameWorld_Instance();
		
		
		HANDLE hThread = CreateThread(NULL,0,listenThreadFun,NULL,0,NULL);
		CloseHandle(hThread);
		ser->mQueueLock = 0;
		
		
		startRun(ser);
	}
	return ser;
}

void startRun(ServerObject* t){
	u_short _port = t->port;
	queue* que = t->mProtocolQueue;
	void*  pool=memPoolList_get();

	if(!t->hWnd){
		
	}

	WORD wVersionRequested;
	WSADATA wsaData;
	int _err;
	wVersionRequested = MAKEWORD(1,1);
	_err = WSAStartup( wVersionRequested, &wsaData );
	if(LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion)!=1||_err!=0||_port==0){
		WSACleanup();
		return;
	}
	SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
	SOCKADDR_IN addrSrv;
	addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
	addrSrv.sin_family=AF_INET;
	addrSrv.sin_port=htons(_port);
	bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
	listen(sockSrv,5);
	SOCKADDR_IN addrClient;
	int len=sizeof(SOCKADDR);

	char rbuf[SOCKET_MAXBUFFER_SIZE];
	int bufferSize = SOCKET_MAXBUFFER_SIZE; 
	memset(rbuf,0,bufferSize);
	printfMemFree(pool);
	while(true){		
		SOCKET _socket=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
		
		
		ParseData_socket((int)_socket);
		
		
		recv(_socket,rbuf,bufferSize,0);
		
		int len;
		ParseData_len(rbuf,&len);
		SocketProtocol* sp=(SocketProtocol*)mempool_malloc(pool,sizeof(SocketProtocol));
		tool_getclientIp(sp->ip,int(_socket),addrClient);
		sp->buffer	=(char*)mempool_malloc(pool,len);
		memcpy(sp->buffer,rbuf,len);
		
		
				
		
		QueueEnter(que,(int)sp);
		

		closesocket(_socket);
	}
}

© 2024 lite-C Forums