Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 07:12

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[D]dasocks - Async Sockets

Discussion on [D]dasocks - Async Sockets within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
Super Aids's Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
[D]dasocks - Async Sockets

As I'm working on a new server written in D then I've been working on implementing asynchronous sockets. The sockets should be able to run on any Windows and Posix (Linux, OSX etc.) based systems.

It uses the standard library, no 3rd party libraries and is compatible with DMD2.

The following modules from the standard library is used:
core.thread

std.socket

std.c.string
-- Standard C Library <string.h>

std.array

std.conv

std.string


The library provides the following modules:
asynctcpsocket.d
-- The socket wrapper for an asynchronous tcp socket
asyncthread.d
-- The thread wrapper for an asynchronous socket thread
error.d
-- The wrapper for exceptions
events.d
-- The event wrapper for events across the library
package.d
-- The package import (Makes it possible to do import dasocks;)
packet.d
-- A datapacket wrapper (Supporting a string packer too.)
states.d
-- A state wrapper
thread.d
-- A threading wrapper to provide possibility of events to be called whenever a new thread is run
uid.d
-- An UID generator

The zip archive includes the following:
All modules in the dasocks library
example.d (A simple example usage of the library -- Same as provided in the thread.)
cmd.txt (A text file containing the command line argument to compile example.d along with dasocks.)

All modules are documented.

example.d
Code:
import dasocks;

// Only required for bind() and Client test
import std.socket : InternetAddress, TcpSocket;
import std.stdio;

void main() {
	// Creates an asynchronous socket server
	auto server = new AsyncTcpSocket;
	// Sets the events of the server
	server.setEvents(new AsyncSocketEvent(&onAccept), new AsyncSocketEvent(&onReceive), new AsyncSocketEvent(&onDisconnect));
	// Begins to accept a connection
	server.beginAccept();	
	// Binds the server to 127.0.0.1:9988
	server.bind(new InternetAddress("127.0.0.1", 9988));
	// Starts listening for connections
	server.listen(500);
	
	// Client test ...
	writeln("Press ENTER to connect...");
	readln();
	// Creates a new NON-asynchronous tcp socket
	auto client = new TcpSocket;
	// Connects to 127.0.0.1:9988
	client.connect(new InternetAddress("127.0.0.1", 9988));
	writeln("Connected...");
	while (true) {
		writeln("Press ENTER to send a packet...");
		readln();
		// Creates a 10 byte packet
		ubyte[] buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
		// Sends the packet
		client.send(buffer);
	}
}

void onAccept(AsyncTcpSocket server) {
	// Ends the acceptance of a socket and returns the accepted socket
	auto socket = server.endAccept();
	writeln("Socket[", socket.socketId, "] has connected.");
	// Begins to receive a 10 byte packet from the accepted socket
	socket.beginReceive(10);
	
	// Begins to accept a new connection
	server.beginAccept();
}

void onReceive(AsyncTcpSocket client) {
	import std.conv : to;
	
	// Gets the received packet from the client
	ubyte[] buffer = client.endReceive();
	string outStr;
	foreach (b; buffer)
		outStr ~= to!string(b) ~ ", ";
	outStr.length -= 2;
	writeln("[", outStr, "] from Socket[", client.socketId, "]");
	
	// Begins to receive a 10 byte packet from the client
	client.beginReceive(10);
}

void onDisconnect(AsyncTcpSocket socket) {
	if (socket.listening) {
		// If the socket is listening then it's a server socket
		writeln("The server was shutdown!");
	}
	else {
		// If the socket isn't listening then it's a client
		writeln("Socket[", socket.socketId, "] has disconnected.");
	}
}
Download:


If you got trouble or have any questions feel free to ask.

For more information visit:
Super Aids is offline  
Old 12/21/2014, 16:53   #2


 
KraHen's Avatar
 
elite*gold: 0
Join Date: Jul 2006
Posts: 2,216
Received Thanks: 794
@OP : Have you tried to benchmark this implementation against something like boost::asio or the standard .NET socket implementation? I`d be curious of the performance difference, say 10k clients.
KraHen is offline  
Old 12/21/2014, 17:29   #3
 
elite*gold: 0
Join Date: Jul 2014
Posts: 402
Received Thanks: 540
Quote:
Originally Posted by KraHen View Post
@OP : Have you tried to benchmark this implementation against something like boost::asio or the standard .NET socket implementation? I`d be curious of the performance difference, say 10k clients.
It's doing lots of (unnecessary) allocations, from what I see it's basically written in like 99% C# style so I'm guessing it wasn't meant to be super high-performance. The project description doesn't state anything about performance either.
Best Coder 2014 is offline  
Old 12/22/2014, 00:15   #4
 
Super Aids's Avatar
 
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
Quote:
Originally Posted by KraHen View Post
Thank you for the slightly racist and totally unconstructive comment wasting around 100 bytes of memory somewhere in the epvp cloud.

@OP : Have you tried to benchmark this implementation against something like boost::asio or the standard .NET socket implementation? I`d be curious of the performance difference, say 10k clients.
I haven't done any benchmarks.

Quote:
Originally Posted by Best Coder 2014 View Post
It's doing lots of (unnecessary) allocations, from what I see it's basically written in like 99% C# style so I'm guessing it wasn't meant to be super high-performance. The project description doesn't state anything about performance either.
It was my first attempt ever to write asynchronous sockets in D and my goal was really just to have it working.

Working on something way more optimized though.
Super Aids is offline  
Reply


Similar Threads Similar Threads
[C#]Async Socket
01/09/2014 - CO2 PServer Guides & Releases - 2 Replies
Hey , this is my first test of coding a socket :D ServerSocket - iPiraTe - Pastebin.com Notice : i didn't finished it yet .. i wanted to know ur opinion first
A look at async/await and its applications
08/08/2013 - CO2 Programming - 6 Replies
I'm pretty sure most programmers here are still stuck using the legacy async methods (Begin..., End...) instead of the new keywords, so here's a small overview of using them. You can quickly see how applicable they are to development and how useful it can be. Emphasis on "small overview". It also becomes extremely apparent how much cleaner your code becomes using async/await rather than the legacy methods which forced you to write spaghetti-like code.
[Release]Async Sockets (No packet-splitter)
09/20/2012 - CO2 Programming - 16 Replies
So after seeing this thread: http://www.elitepvpers.com/forum/co2-pserver-discu ssions-questions/2129268-problem-packet-splitting. html I thought I'd make some socket server that could handle the packets without needing to split. Source: BasicClient.cs using System;
Async Socket Wrapper
11/11/2011 - CO2 Programming - 8 Replies
So I was bored and thought I would code a socket wrapper. It's a class library, but full project is available for download + an example use for a socket server. It contains both wrapper for server and client stuff, which means it can be used for either private servers as server-socket or for a proxy. WinAsync - This class contains all the events. public delegate void WinEvent(WinClient wClient); public delegate void WinBufferEvent(WinClient wClient, WinBuffer Buffer); ...



All times are GMT +1. The time now is 07:12.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.