Skip to content

Commit

Permalink
feature 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hiram3512 committed Dec 12, 2021
1 parent 3b4f85b commit 0d1cd7b
Show file tree
Hide file tree
Showing 86 changed files with 1,035 additions and 1,421 deletions.
35 changes: 28 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
# This is a basic workflow to help you get started with Actions

name: CI

on: [push]
# Controls when the workflow will run
on:
push:
pull_request:
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:

# The type of runner that the job will run on
runs-on: windows-latest
defaults:
run:
working-directory: ./src


# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v1
- name: MSBuild
run: |
cd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\"
.\MSBuild.exe $Env:GITHUB_WORKSPACE\src\HiSocket.sln
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

# Runs a single command using the runners shell
- name: Hello world
run: echo Hello, world!

- name: Setup .NET SDK
uses: actions/setup-dotnet@v1.7.2
with:
dotnet-version: 5.0.x

- name: Build
run: dotnet build --configuration Release
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vs
TestResults
bin
obj
File renamed without changes.
2 changes: 0 additions & 2 deletions .travis.yml

This file was deleted.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
It is a lightweight client socket solution, you can used it in Unity3d or C# project


![Packagist](https://img.shields.io/packagist/l/doctrine/orm.svg) ![](https://github.com/hiramtan/hisocket/workflows/CI/badge.svg?branch=master) [![Build Status](https://travis-ci.org/hiramtan/HiSocket.svg?branch=master)](https://travis-ci.org/hiramtan/HiSocket) [![GitHub release](https://img.shields.io/github/release/hiramtan/HiSocket.svg)](https://github.com/hiramtan/HiSocket/releases)
![Packagist](https://img.shields.io/packagist/l/doctrine/orm.svg) ![](https://github.com/hiramtan/hisocket/workflows/CI/badge.svg?branch=master) [![GitHub release](https://img.shields.io/github/release/hiramtan/HiSocket.svg)](https://github.com/hiramtan/HiSocket/releases) [![Github Releases](https://img.shields.io/github/downloads/atom/atom/total.svg)](https://github.com/hiramtan/HiSocket/releases)

-----
[中文说明](https://github.com/hiramtan/HiSocket/blob/master/README_zh.md)
[中文说明](https://github.com/hiram3512/HiSocket/blob/master/README_zh.md)

### How to use

- Use source code: [source code](/src)
- Download dll from release path: [![Github Releases](https://img.shields.io/github/downloads/atom/atom/total.svg)](https://github.com/hiramtan/HiSocket/releases)
- Download dll from release path: [Github Releases](https://github.com/hiramtan/HiSocket/releases)
- Use Nuget: [HiSocket NuGet package](https://www.nuget.org/packages/HiSocket)

Quick Start:
Expand Down Expand Up @@ -69,14 +69,15 @@ This project contains:
- Support protobuf message
- AES encryption


### Details
- Use async connection in main thread(avoid thread blocking).
- Using [Circular_buffer](https://en.wikipedia.org/wiki/Circular_buffer) to avoid memory allocation every time, and reduce garbage collection.
- You can get current connect state and message by adding listener of event.
- If you use Tcp socket, you should implement IPackage interface to pack or unpack message.
- Ping: there is a ping plugin you can used, but if you are used in unity3d because of the bug of mono, it will throw an error on .net2.0(.net 4.6 will be fine, also you can use unity's api to get ping time)

### Framework
![framework](/others/fwsZp3Arrs.png)

### Advanced
- If you are clear about socket, you also can use TcpSocket to achieve your logic, anyway the recommend is TcpConnection.
Expand All @@ -88,7 +89,6 @@ This project contains:
- The encryption is use AES, if you want to use encryption you can use the API to encrypte your bytes.
- .etc


### Instructions
- Tcp
[Transmission Control Protocol](https://en.wikipedia.org/wiki/Transmission_Control_Protocol)
Expand Down
7 changes: 5 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

客户端轻量Socket通信逻辑,可以在C#项目或Unity3d项目中使用.

![Packagist](https://img.shields.io/packagist/l/doctrine/orm.svg) [![Build Status](https://travis-ci.org/hiramtan/HiSocket.svg?branch=master)](https://travis-ci.org/hiramtan/HiSocket) [![GitHub release](https://img.shields.io/github/release/hiramtan/HiSocket.svg)](https://github.com/hiramtan/HiSocket/releases)
![Packagist](https://img.shields.io/packagist/l/doctrine/orm.svg) ![](https://github.com/hiramtan/hisocket/workflows/CI/badge.svg?branch=master) [![GitHub release](https://img.shields.io/github/release/hiramtan/HiSocket.svg)](https://github.com/hiramtan/HiSocket/releases) [![Github Releases](https://img.shields.io/github/downloads/atom/atom/total.svg)](https://github.com/hiramtan/HiSocket/releases)

-----

### 如何使用

- 使用源码: [source code](/src)
- 发布页下载dll: [![Github Releases](https://img.shields.io/github/downloads/atom/atom/total.svg)](https://github.com/hiramtan/HiSocket/releases)
- 发布页下载dll: [Github Releases](https://github.com/hiramtan/HiSocket/releases)
- 使用 Nuget: [HiSocket NuGet package](https://www.nuget.org/packages/HiSocket)


Expand Down Expand Up @@ -74,6 +74,9 @@
- 如果使用Tcp协议需要实现IPackage接口处理粘包拆包.
- Ping: 源码包含一个Ping插件可以使用,但是如果用在unity3d工程中会报错(因为mono的问题,在.net2.0会报错.net4.6可以正常使用)

### 结构
![framework](/others/fwsZp3Arrs.png)

### 高级功能
- 如果对Socket很熟悉,也可以使用TcpSocket来实现功能,但是还是推荐使用TcpConnection的方式.
- 通过接口可以访问底层Socket对象扩展逻辑,比如修改超时时间.
Expand Down
Binary file added others/fwsZp3Arrs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions src/HiSocket.Tcp.Example/ExamplePackage1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/***************************************************************
* Description: Block buffer for reuse array
*
* Documents: https://github.com/hiram3512/HiSocket
* Support: hiramtan@live.com
***************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace HiSocket.Tcp.Example
{
public class ExamplePackage1 : IPackage
{
public void Pack(byte[] message, IBlockBuffer<byte> sendBuffer)
{
sendBuffer.WriteAtEnd(message);
}
public void Unpack(IBlockBuffer<byte> receiveBuffer, ref byte[] message)
{
var length = receiveBuffer.Index;
if (length > 0)
{
message = receiveBuffer.ReadFromHead(length);
}
}
}
}
39 changes: 39 additions & 0 deletions src/HiSocket.Tcp.Example/ExamplePackage2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***************************************************************
* Description: Block buffer for reuse array
*
* Documents: https://github.com/hiram3512/HiSocket
* Support: hiramtan@live.com
***************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace HiSocket.Tcp.Example
{
public class ExamplePackage2 : IPackage
{
public void Pack(byte[] message, IBlockBuffer<byte> sendBuffer)
{
//消息头,假设用int标识,4字节
int length = message.Length;
byte[] lengthBytes = BitConverter.GetBytes(length);
sendBuffer.WriteAtEnd(lengthBytes);
sendBuffer.WriteAtEnd(message);
}
public void Unpack(IBlockBuffer<byte> receiveBuffer, ref byte[] message)
{
//消息头,假设用int标识,4字节
if (receiveBuffer.Index >= 4)
{
byte[] lengthBytes = receiveBuffer.TryReadFromHead(4);
int length = BitConverter.ToInt32(lengthBytes, 0);
if (receiveBuffer.Index >= length)//buffer中有足够数据
{
receiveBuffer.IncreaseIndex(4);//消息头,假设用int标识,4字节,开始读取消息体
message = receiveBuffer.ReadFromHead(length);
}
}
}
}
}
40 changes: 40 additions & 0 deletions src/HiSocket.Tcp.Example/ExampleTcpConnection1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/***************************************************************
* Description: Block buffer for reuse array
*
* Documents: https://github.com/hiram3512/HiSocket
* Support: hiramtan@live.com
***************************************************************/

using System;
using HiSocket;

namespace HiSocket.Tcp.Example
{
public class ExampleTcpConnection1
{
private TcpConnection tcpConnection;
void Main()
{
var package = new ExamplePackage2();
tcpConnection = new TcpConnection(package);
tcpConnection.OnConnecting += OnConnecting;
tcpConnection.OnConnected += OnConnected;
tcpConnection.OnReceiveMessage += OnReceiveMessage;
tcpConnection.Connect("127.0.0.1", 999);
}
void OnConnecting()
{
Console.WriteLine("Connecting");
}
void OnConnected()
{
Console.WriteLine("Connected");
var data = new byte[] { 1, 2, 3 };
tcpConnection.Send(data);
}
void OnReceiveMessage(byte[] data)
{
Console.WriteLine("Receive data");
}
}
}
45 changes: 45 additions & 0 deletions src/HiSocket.Tcp.Example/ExampleTcpConnection2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************
* Description: Block buffer for reuse array
*
* Documents: https://github.com/hiram3512/HiSocket
* Support: hiramtan@live.com
***************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace HiSocket.Tcp.Example
{
public class ExampleTcpConnection2
{
private TcpConnection tcpConnection;
void Main()
{
var package = new ExamplePackage2();
tcpConnection = new TcpConnection(package);
tcpConnection.OnConnected += OnConnected;
tcpConnection.OnReceiveMessage += OnReceiveMessage;
tcpConnection.OnException += OnException;
tcpConnection.Connect("127.0.0.1", 999);
tcpConnection.Socket.NoDelay = true;
tcpConnection.Socket.SendTimeout = 100;
tcpConnection.Socket.ReceiveTimeout = 200;
}

void OnConnected()
{
Console.WriteLine("Connected");
var data = new byte[] { 1, 2, 3 };
tcpConnection.Send(data);
}
void OnReceiveMessage(byte[] data)
{
Console.WriteLine("Receive data");
}
void OnException(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
11 changes: 11 additions & 0 deletions src/HiSocket.Tcp.Example/HiSocket.Tcp.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\HiSocket.Tcp\HiSocket.Tcp.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions src/HiSocket.Tcp.Test/HiSocket.Tcp.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HiSocket.Tcp.Example\HiSocket.Tcp.Example.csproj" />
<ProjectReference Include="..\HiSocket.Tcp\HiSocket.Tcp.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 0d1cd7b

Please sign in to comment.