Jump to content


Photo
- - - - -

[Exa] C++ server 39dll + client


  • Please log in to reply
8 replies to this topic

#1 Aragon

Aragon

    GMC Member

  • GMC Member
  • 138 posts

Posted 09 November 2010 - 06:16 PM

  • Title: C++ server 39dll
  • Description: An example how to communicate a simpel game with a c++ server.
  • GM Version: GM 6/7/8
  • Registered: Yes
  • File Type: .gmk, .zip, [source of c++ server files]
  • File Size: 4 mb
  • File Link: Click here [hosted by Airload.nl]

Additional Info
This example will show you how to communicate with a c++ server. It requires C++ knowledge and Microsoft Visual Express 2008.

Credits:
Blackhawk
Michel
Evayr
39ster
And someone who interpreter 39dll to c++, but that example I couldn't find any-more.



Spoiler

Edited by icuurd12b42, 06 December 2011 - 12:14 AM.

  • 2

#2 Aragon

Aragon

    GMC Member

  • GMC Member
  • 138 posts

Posted 23 June 2011 - 09:01 AM

bumb, any comments?
  • 0

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14390 posts
  • Version:GM:Studio

Posted 23 June 2011 - 08:33 PM

?? pos
void CBuffer::StreamSet(int pos)
{
	readpos = 0;
	writepos = 0;
}


missing
if(data == NULL) return at every function that uses the buffer

CBuffer::CBuffer()
{
	BuffSize = 30;
	data = (char*)malloc(BuffSize);
	count = 0;
	readpos = 0;
	writepos = 0;
}
void CBuffer::clear()
{
	if(BuffSize > 30)
	{
		free(data);
		BuffSize = 30;
		data =(char*) malloc(BuffSize);
	}
	count = 0;
	readpos = 0;
	writepos = 0;
}

void CBuffer::StreamWrite(void *in, int size)
{
	if(writepos+size >= BuffSize)
	{
		BuffSize = writepos+size + 30;
		if((data = (char*)realloc(data, BuffSize)) == NULL)return;
	}
	memcpy(data+writepos, in, size);
	writepos += size;
	if(writepos > count)count = writepos;
}

frankly the buffer size of 30 is a little prone to fragmentation ans slowness. you should allocate a decent size from the start, at least in the KB range and in factor of 2^n , say 8K, 8192

as you write, you reallocate by another 8192 bytes or a few 8192 bytes according to size (if size is larger than chunk).
if(writepos+size >= BuffSize)
{
Buffsize += ceil(size/8192) * 8192
re-alloc
}
when you clear, you could just reset the memory index
count = 0;
readpos = 0;
writepos = 0;

and memset to 0 the size of BuffSize (probably you can keep the garbage in the buffer and no need to memset)

you would gradually have adjusted to the size of memory the application needs, you would allocate in a memory friendlily way, you would have just a few reallocs

instead of malloc and realloc and free, you can use the windows method, include <windowsx.h> (I think) and use GlobalAllocPtr() , GlobalReallocPtr and GLobalFreePtr
  • 1

#4 iceshield

iceshield

    GMC Member

  • GMC Member
  • 393 posts

Posted 25 June 2011 - 11:18 AM

This is what I was searching for . Thanks guys , you really did a freaking nice job .I'll start learning C++ just for this (I know just the basics ).
Still , the client and the server needs some modification .For ex : in the client stop_game function I think it should be an if that check if global.myid exists and only then send the package 3 , because the client can close with no connection done (and results a small error). And in the server main cpp could change the finding free place for new player (because it dosen't work as it should) from
Spoiler


to
Spoiler


Anyway , very good example , very good . Thanks again :)

Edited by iceshield, 25 June 2011 - 11:21 AM.

  • 0

#5 Schyler

Schyler

    Noskcirderf Derf

  • GMC Member
  • 2445 posts
  • Version:GM8.1

Posted 26 June 2011 - 10:33 PM

I havn't downloaded it to take a look, but you should use an std::vector<Player> to store the player sockets. It's much better than an array, as its size isn't predetermined and is automatically managed allowing for easy addition and removal.

Edit: Server stability wise, an array might be better - if your server has like 20mb of RAM :P

Edited by Schyler, 26 June 2011 - 10:34 PM.

  • 1

#6 iceshield

iceshield

    GMC Member

  • GMC Member
  • 393 posts

Posted 12 July 2011 - 08:40 AM

Could you come up with a tutorial for http connections ? I really can't convert/rewrite from gml to c++ :(
  • 1

#7 Stinkers12

Stinkers12

    GMC Member

  • GMC Member
  • 24 posts
  • Version:GM8.1

Posted 30 September 2011 - 12:29 AM

how do i use the server with my game?
  • 0

#8 Spider_hip

Spider_hip

    GMC Member

  • New Member
  • 1 posts
  • Version:GM8

Posted 01 December 2011 - 12:25 AM

Hey all. I wish I could understand what you talking about ^^. But as I understand you prefer better methods to use for this server to work better. I downloaded, tried, and seemed worked stable. I don't know why you think there should be change on codes(beceause I'm a c++ dummy) but I'm pretty sure you're right. So why don't you edit/improve these files and than share
with us ?

Ok nevermind, I just released it to an one .exe file. Named it as server ^^. Of course It would be better if I knew how to release it as windows app with gui. Now it's still on ms-dos mode.

Edited by Spider_hip, 02 December 2011 - 12:03 PM.

  • 0

#9 2DLuis

2DLuis

    Graphic Designer

  • GMC Member
  • 2493 posts
  • Version:GM8

Posted 02 December 2011 - 08:50 PM

A couple of friendly suggestions:

Use pointers to reduce memory consumption and be able to access player instance memory locations from nearly any file.
Use static classes (static variables + function declarations in C++) to make a player handling class

Readstring will freak out when reading multiple strings off of the buffer successively, make a "global" char* holding variable in buffer.cpp and return a memory address to that instead of the locally created retval.

Vector lists would be good as well, as someone suggested above, but you'd need an array to handle playerids (a bool array).
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users