Skip to content

A socket class provides asynchronous IO operation and coroutine co_await support

License

Notifications You must be signed in to change notification settings

a1q123456/AsyncIocpSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

A socket class provides asynchronous IO operation and co_await support

Compile Requirement

  • VS 2017 15.6.1 or later
  • Windows SDK 10.0.16299.0 or later

Usage

// Server

Socket socket(EAddressFamily::InternetworkV4, ESocketType::Stream, EProtocolType::Tcp);
socket.Bind("0.0.0.0", 1568);
socket.Listen(128);

auto acceptFuture = socket.AcceptAsync();
try
{
	std::byte buffer[2] = { std::byte(1), std::byte(2) };
	auto clientSocket = co_await acceptFuture;
	co_await clientSocket.SendAsync(buffer);
	co_await clientSocket.ReceiveAsync(buffer);
}
catch (const SocketError& /* e */)
{
	printf("server exception\n");
}

// Client

Socket client(EAddressFamily::InternetworkV4, ESocketType::Stream, EProtocolType::Tcp);
co_await client.ConnectAsync("127.0.0.1", 1568);

std::byte buffer[2] = { std::byte(3), std::byte(4) };
co_await client.SendAsync(buffer);
co_await client.ReceiveAsync(buffer);

Methods

Socket.h

  • Bind
  • Listen
  • AcceptAsync
  • ConnectAsync
  • ReceiveAsync
  • SendAsync
  • ReceiveLineAsync
  • Dispose

Await.h

  • Then
  • Wait
  • WaitFor
  • WaitUntil
  • Get
  • GetFor
  • GetUntil
  • WaitAll
  • WaitForAll
  • WaitUntilAll

Bind

socket.Bind(std::string ipAddress, int port);

Listen

socket.Listen(int backLog);

AcceptAsync

Async::Awaiter<Socket> future = socket.AcceptAsync();
Socket client = co_await future;

// or

auto future = co_await socket.AcceptAsync();

ConnectAsync

co_await socket.ConnectAsync();

ReceiveAsync

std::byte* buf = new std::byte[100];
socket.ReceiveAsync(buf, 100);

// or

std::byte buf[100];
socket.ReceiveAsync(buf);

SendAsync

std::byte* buf = new std::byte[100];
// TODO: fill the buffer
co_await socket.SendAsync(buf, 100);

// or

std::byte buf[100];
// TODO: fill the buffer
co_await socket.SendAsync(buf);

Dispose

socket.Dispose();

Then

awaiter.Then([]{
	std::cout << "done" << std::endl;
};

Wait

awaiter.Wait();

WaitFor

#include <chrono>

using namespace std::chrono_literals;
awaiter.WaitFor(10s);

WaitUntil

#include <chrono>

using namespace std::chrono_literals;

awaiter.WaitUntil(std::chrono::steady_clock::now() + 10s);

About

A socket class provides asynchronous IO operation and coroutine co_await support

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published