• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
busho

how to process asynchronous messages with libuv

0 posts in this topic

Hello, I'm a long time gamedev lurker and now it's time for my first post smile.png

 

I have recently began working on my first C/C++ project. As part of it I am developing a networking functionality, and to not reinvent the wheel I have decided to integrate libuv library. I have a very specific question about asynchronous message processing. As for documentation on how to integrate libuv networking, my code currently looks like this:

void onConnectCb ( .......) {
......

uv_read_start((uv_stream_t*)(&_client->tcp), ClientConnection::locCbOnAlloc, ClientConnection::locCbOnRead);
}

And locCbOnAlloc and locCbOnRead look simply like this:

/* On allocation */
void ClientConnection::locCbOnAlloc(uv_handle_t *handle, size_t suggestedSize, uv_buf_t* buf) {
	*buf = uv_buf_init((char*)malloc(suggestedSize), suggestedSize);
}

/* On read */
void ClientConnection::locCbOnRead(uv_stream_t *tcp, ssize_t nread, const uv_buf_t* buf) {
	if (nread < 0) {	//ERROR
		uv_close((uv_handle_t *)tcp, ClientConnection::locCbOnClose);
	}	
	else if (nread > 0) {
		usninfo("usnetwork", "Reading %d bytes from NS", nread); //is nread bytes or chars or what?
		fwrite(buf->base, sizeof(char), nread, stdout);
	}
	free(buf->base);
}

My question is, because libuv invokes these callback asynchronisly the variable nread varies. Supposedly I will have some sort of standard protocol for communication between my services, the messages will have a length parameter. What should happen if locCbOnRead reads too few bytes or too many (truncates the message or includes the next message).

 

I have thought about using one huge double way buffer per client connection like this:

struct cli_buffer_t {
	char *buffer;
	int readLoc;
	int writeLoc;
	int allocSize;
	int logicalSize;
};

void initBuffer(cli_buffer_t *buffer) {
	buffer->buffer = (char *)malloc(sizeof(char)* 2048 * 2048);
	buffer->readLoc = buffer->writeLoc = 0;
	buffer->allocSize = 2048 * 2048;
	buffer->logicalSize = 0;
}

char *getWriteLoc(cli_buffer_t *buffer) {
	return &buffer->buffer[buffer->writeLoc];
}

void advanceWrite(cli_buffer_t *buffer, int nread) {
	buffer->writeLoc += nread; //SAMPLE CODE, NOT CHECKING OUT OF BUFFER BOUNDS YET.
}

Reading from buffer would increment readLoc, writing would increment writeLoc(both required to check if buffer has too few bytes or is full).

So allocation and reading callbacks become this:

/* On allocation */
void ClientConnection::locCbOnAlloc(uv_handle_t *handle, size_t suggestedSize, uv_buf_t* buf) {
    buf->base = getWriteLoc(memory);
    buf->len = suggestedSize;
}

/* On read */
void ClientConnection::locCbOnRead(uv_stream_t *tcp, ssize_t nread, const uv_buf_t* buf) {
	....	
	advanceWrite(memory, nread); //WE HAVE READ NREAD BYTES INTO BUFFER THAT STARTED AT GETWRITELOC(MEMORY). aDVANCE THE WRITE LOCATION FOR NEXT CALL.
}

Would something like this be appropriate approach? This way, I can invoke app level cb from advanceWrite() and try reading only desired amount of bytes (however much the message length will be). Does libuv provide any means to do this without this code? (ea, it stores data in internal buffer and I can read exact number of bytes from it). 

Also this code would be extremely buggy without locking mechanisms as there is no guarantee that onRead will be called exactly after onAlloc (maybe onAlloc gets called 2 times before onRead which would overwrite data etc). It could get complicated and messy really fast so I decided to ask first smile.png

 

TL;DR: How to work with asynchronous messages that can have too many or too few bytes (according to my own length parameter in message).

Thanks for help smile.png

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0