Skip to content

Commit 0d1cd7b

Browse files
author
hiram
committed
feature 3.0
1 parent 3b4f85b commit 0d1cd7b

File tree

86 files changed

+1035
-1421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1035
-1421
lines changed

.github/workflows/main.yml

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1+
# This is a basic workflow to help you get started with Actions
12

23
name: CI
34

4-
on: [push]
5+
# Controls when the workflow will run
6+
on:
7+
push:
8+
pull_request:
9+
workflow_dispatch:
510

11+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
612
jobs:
13+
# This workflow contains a single job called "build"
714
build:
8-
15+
# The type of runner that the job will run on
916
runs-on: windows-latest
17+
defaults:
18+
run:
19+
working-directory: ./src
20+
1021

22+
# Steps represent a sequence of tasks that will be executed as part of the job
1123
steps:
12-
- uses: actions/checkout@v1
13-
- name: MSBuild
14-
run: |
15-
cd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\"
16-
.\MSBuild.exe $Env:GITHUB_WORKSPACE\src\HiSocket.sln
24+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
25+
- uses: actions/checkout@v2
26+
27+
# Runs a single command using the runners shell
28+
- name: Hello world
29+
run: echo Hello, world!
30+
31+
- name: Setup .NET SDK
32+
uses: actions/[email protected]
33+
with:
34+
dotnet-version: 5.0.x
35+
36+
- name: Build
37+
run: dotnet build --configuration Release

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vs
2-
TestResults
2+
bin
3+
obj

.travis.yml

-2
This file was deleted.

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
It is a lightweight client socket solution, you can used it in Unity3d or C# project
44

55

6-
![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)
6+
![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)
77

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

1111
### How to use
1212

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

1717
Quick Start:
@@ -69,14 +69,15 @@ This project contains:
6969
- Support protobuf message
7070
- AES encryption
7171

72-
7372
### Details
7473
- Use async connection in main thread(avoid thread blocking).
7574
- Using [Circular_buffer](https://en.wikipedia.org/wiki/Circular_buffer) to avoid memory allocation every time, and reduce garbage collection.
7675
- You can get current connect state and message by adding listener of event.
7776
- If you use Tcp socket, you should implement IPackage interface to pack or unpack message.
7877
- 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)
7978

79+
### Framework
80+
![framework](/others/fwsZp3Arrs.png)
8081

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

91-
9292
### Instructions
9393
- Tcp
9494
[Transmission Control Protocol](https://en.wikipedia.org/wiki/Transmission_Control_Protocol)

README_zh.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

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

5-
![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)
5+
![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)
66

77
-----
88

99
### 如何使用
1010

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

1515

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

77+
### 结构
78+
![framework](/others/fwsZp3Arrs.png)
79+
7780
### 高级功能
7881
- 如果对Socket很熟悉,也可以使用TcpSocket来实现功能,但是还是推荐使用TcpConnection的方式.
7982
- 通过接口可以访问底层Socket对象扩展逻辑,比如修改超时时间.

others/fwsZp3Arrs.png

20.5 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/***************************************************************
2+
* Description: Block buffer for reuse array
3+
*
4+
* Documents: https://github.com/hiram3512/HiSocket
5+
* Support: [email protected]
6+
***************************************************************/
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Diagnostics;
11+
using System.Text;
12+
13+
namespace HiSocket.Tcp.Example
14+
{
15+
public class ExamplePackage1 : IPackage
16+
{
17+
public void Pack(byte[] message, IBlockBuffer<byte> sendBuffer)
18+
{
19+
sendBuffer.WriteAtEnd(message);
20+
}
21+
public void Unpack(IBlockBuffer<byte> receiveBuffer, ref byte[] message)
22+
{
23+
var length = receiveBuffer.Index;
24+
if (length > 0)
25+
{
26+
message = receiveBuffer.ReadFromHead(length);
27+
}
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/***************************************************************
2+
* Description: Block buffer for reuse array
3+
*
4+
* Documents: https://github.com/hiram3512/HiSocket
5+
* Support: [email protected]
6+
***************************************************************/
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
12+
namespace HiSocket.Tcp.Example
13+
{
14+
public class ExamplePackage2 : IPackage
15+
{
16+
public void Pack(byte[] message, IBlockBuffer<byte> sendBuffer)
17+
{
18+
//消息头,假设用int标识,4字节
19+
int length = message.Length;
20+
byte[] lengthBytes = BitConverter.GetBytes(length);
21+
sendBuffer.WriteAtEnd(lengthBytes);
22+
sendBuffer.WriteAtEnd(message);
23+
}
24+
public void Unpack(IBlockBuffer<byte> receiveBuffer, ref byte[] message)
25+
{
26+
//消息头,假设用int标识,4字节
27+
if (receiveBuffer.Index >= 4)
28+
{
29+
byte[] lengthBytes = receiveBuffer.TryReadFromHead(4);
30+
int length = BitConverter.ToInt32(lengthBytes, 0);
31+
if (receiveBuffer.Index >= length)//buffer中有足够数据
32+
{
33+
receiveBuffer.IncreaseIndex(4);//消息头,假设用int标识,4字节,开始读取消息体
34+
message = receiveBuffer.ReadFromHead(length);
35+
}
36+
}
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************
2+
* Description: Block buffer for reuse array
3+
*
4+
* Documents: https://github.com/hiram3512/HiSocket
5+
* Support: [email protected]
6+
***************************************************************/
7+
8+
using System;
9+
using HiSocket;
10+
11+
namespace HiSocket.Tcp.Example
12+
{
13+
public class ExampleTcpConnection1
14+
{
15+
private TcpConnection tcpConnection;
16+
void Main()
17+
{
18+
var package = new ExamplePackage2();
19+
tcpConnection = new TcpConnection(package);
20+
tcpConnection.OnConnecting += OnConnecting;
21+
tcpConnection.OnConnected += OnConnected;
22+
tcpConnection.OnReceiveMessage += OnReceiveMessage;
23+
tcpConnection.Connect("127.0.0.1", 999);
24+
}
25+
void OnConnecting()
26+
{
27+
Console.WriteLine("Connecting");
28+
}
29+
void OnConnected()
30+
{
31+
Console.WriteLine("Connected");
32+
var data = new byte[] { 1, 2, 3 };
33+
tcpConnection.Send(data);
34+
}
35+
void OnReceiveMessage(byte[] data)
36+
{
37+
Console.WriteLine("Receive data");
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/***************************************************************
2+
* Description: Block buffer for reuse array
3+
*
4+
* Documents: https://github.com/hiram3512/HiSocket
5+
* Support: [email protected]
6+
***************************************************************/
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
12+
namespace HiSocket.Tcp.Example
13+
{
14+
public class ExampleTcpConnection2
15+
{
16+
private TcpConnection tcpConnection;
17+
void Main()
18+
{
19+
var package = new ExamplePackage2();
20+
tcpConnection = new TcpConnection(package);
21+
tcpConnection.OnConnected += OnConnected;
22+
tcpConnection.OnReceiveMessage += OnReceiveMessage;
23+
tcpConnection.OnException += OnException;
24+
tcpConnection.Connect("127.0.0.1", 999);
25+
tcpConnection.Socket.NoDelay = true;
26+
tcpConnection.Socket.SendTimeout = 100;
27+
tcpConnection.Socket.ReceiveTimeout = 200;
28+
}
29+
30+
void OnConnected()
31+
{
32+
Console.WriteLine("Connected");
33+
var data = new byte[] { 1, 2, 3 };
34+
tcpConnection.Send(data);
35+
}
36+
void OnReceiveMessage(byte[] data)
37+
{
38+
Console.WriteLine("Receive data");
39+
}
40+
void OnException(Exception e)
41+
{
42+
Console.WriteLine(e.ToString());
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\HiSocket.Tcp\HiSocket.Tcp.csproj" />
9+
</ItemGroup>
10+
11+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
12+
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
13+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\HiSocket.Tcp.Example\HiSocket.Tcp.Example.csproj" />
18+
<ProjectReference Include="..\HiSocket.Tcp\HiSocket.Tcp.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)