Skip to content

Commit 5902673

Browse files
author
Hiram
committed
modify read me
1 parent 89fc6b8 commit 5902673

File tree

138 files changed

+471
-439
lines changed

Some content is hidden

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

138 files changed

+471
-439
lines changed
File renamed without changes.

HiSocket/HiSocket.Message.dll

8.5 KB
Binary file not shown.

HiSocket/HiSocket.Message.pdb

31.5 KB
Binary file not shown.

HiSocket/HiSocket.dll

14 KB
Binary file not shown.

HiSocket/HiSocket.pdb

33.5 KB
Binary file not shown.
File renamed without changes.

HiSocket_2.8.1.zip

-143 KB
Binary file not shown.

README.md

+51-32
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ It is a lightweight client socket solution, you can used it in Unity3d or C# pro
4040
```
4141

4242
More example:
43-
- C# project example:[Example](/example/csharp)
44-
- Unity project example:[Example](/example/unity)
43+
- C# project example:[Example](src/HiSocket.Example)
44+
- Unity project example:[Example](src/HiSocket.Example_Unity)
4545

4646
-----
4747

@@ -78,11 +78,11 @@ This project contains:
7878

7979

8080
### Advanced
81-
- If you are clear about socket, you also can use TcpSocket(UdpSocket) to achieve your logic, anyway the recommend is TcpConnection(UdpConnection).
81+
- If you are clear about socket, you also can use TcpSocket to achieve your logic, anyway the recommend is TcpConnection.
8282
- You can use API get socket and do extra logic, for example modify socket's out time
8383
- You can use API get send and receive buffer, for example when disconnect, how to handle buffer's data? just clear or resend to server.
8484
- OnSocketReceive and OnReceive are diffrent, for example OnSocketReceive size is 100 byte, if user do nothing when uppack OnReceive size is 100. but when user do some zip/unzip(encription.etc) OnReceive size is not 100 anymore.
85-
- You can add many different plugins based on TcpConnection(UdpConnection) to achieve different functions.
85+
- You can add many different plugins based on TcpConnection to achieve different functions.
8686
- There are a message register base class help user to quick register id and callback(based on reflection)
8787
- The encryption is use AES, if you want to use encryption you can use the API to encrypte your bytes.
8888
- .etc
@@ -138,27 +138,33 @@ There are many example in **HiSocketExample** project or in **HiSocket.unitypack
138138

139139
Package example:
140140
```csharp
141-
public class Package : PackageBase
141+
public class PackageExample:PackageBase
142142
{
143143
protected override void Pack(BlockBuffer<byte> bytes, Action<byte[]> onPacked)
144144
{
145+
//Use int as header
145146
int length = bytes.WritePosition;
146147
var header = BitConverter.GetBytes(length);
147-
var newBytes = new byte[length + header.Length];
148-
Buffer.BlockCopy(header, 0, newBytes, 0, header.Length);
149-
Buffer.BlockCopy(bytes.Buffer, 0, newBytes, header.Length, length);
150-
onPacked(newBytes);
148+
var newBytes = new BlockBuffer<byte>(length + header.Length);
149+
//Write header and body to buffer
150+
newBytes.Write(header);
151+
newBytes.Write(bytes.Buffer);
152+
//Notice pack funished
153+
onPacked(newBytes.Buffer);
151154
}
152155

153156
protected override void Unpack(BlockBuffer<byte> bytes, Action<byte[]> onUnpacked)
154157
{
158+
//Because header is int and cost 4 byte
155159
while (bytes.WritePosition > 4)
156160
{
157161
int length = BitConverter.ToInt32(bytes.Buffer, 0);
162+
//If receive body
158163
if (bytes.WritePosition >= 4 + length)
159164
{
160165
bytes.MoveReadPostion(4);
161166
var data = bytes.Read(length);
167+
//Notice unpack finished
162168
onUnpacked(data);
163169
bytes.ResetIndex();
164170
}
@@ -168,40 +174,53 @@ public class Package : PackageBase
168174
```
169175

170176
```csharp
171-
private IPackage package = new PackageExample();
172-
private TcpConnection _tcp;
173-
static void Main(string[] args)
177+
TcpConnection tcp;
178+
void Connect()
174179
{
180+
tcp = new TcpConnection(new PackageExample());
181+
tcp.OnDisconnected += OnDisconnect;
182+
tcp.Connect("127.0.0.1", 999);
183+
tcp.Socket.NoDelay = true;
184+
tcp.Socket.SendTimeout = 100;
185+
tcp.Socket.ReceiveTimeout = 200;
186+
//...
175187
176-
}
177-
void Init()
178-
{
179-
tcp = new TcpConnection(package);
180-
tcp.OnConnected += OnConnected;
181-
tcp.OnReceive += Receive;
182-
//_tcp.OnError
183-
//_tcp.OnDisconnected
184-
}
185-
void OnConnected()
186-
{
187-
//connect success
188-
tcp.Send(new byte[10]);//send message
189-
tcp.DisConnect();//disconnect
188+
189+
// you can add plugin sub from IPlugins
190+
tcp.AddPlugin(new StatisticalPlugin("Statistical"));//this plugin calculate how many send
190191
}
191192

192-
void Receive(byte[] bytes)
193+
void OnDisconnect()
193194
{
194-
//get message from server
195+
var length = tcp.SendBuffer.WritePosition;
196+
Console.WriteLine("Still have {0} not send to server when abnormal shutdown");
197+
var data = tcp.SendBuffer.Read(length);
198+
tcp.SendBuffer.ResetIndex();
199+
200+
//use can handle these data, for example maybe can send next time when connect again
201+
//tcp.Send(data);
195202
}
196203
```
197204

198205
```csharp
199-
void Init()
206+
/// <summary>
207+
/// The recommend is use TcpConnection
208+
/// </summary>
209+
class Example3
210+
{
211+
TcpSocket tcp; //The recommend is use TcpConnection
212+
void Connect()
200213
{
201-
var tcp = new TcpConnection(new PackageExample());
202-
tcp.AddPlugin(new PingPlugin("ping", tcp));
203-
//tcp.GetPlugin("ping");
214+
tcp = new TcpSocket(1024);//set buffer size
215+
tcp.OnReceiveBytes += OnReceive;
216+
tcp.Connect("127.0.0.1", 999);
204217
}
218+
219+
void OnReceive(byte[] bytes)
220+
{
221+
//split bytes here
222+
}
223+
}
205224
```
206225

207226

README_zh.md

+51-32
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
}
4040
```
4141
更多示例:
42-
- C#项目示例:[示例](/example/csharp)
43-
- Unity项目示例:[示例](/example/unity)
42+
- C#项目示例:[示例](src/HiSocket.Example)
43+
- Unity项目示例:[示例](src/HiSocket.Example_Unity)
4444

4545
-----
4646

@@ -75,11 +75,11 @@
7575
- Ping: 源码包含一个Ping插件可以使用,但是如果用在unity3d工程中会报错(因为mono的问题,在.net2.0会报错.net4.6可以正常使用)
7676

7777
### 高级功能
78-
- 如果对Socket很熟悉,也可以使用TcpSocket(UdpSocket)来实现功能,但是还是推荐使用TcpConnection(UdpConnection)的方式.
78+
- 如果对Socket很熟悉,也可以使用TcpSocket来实现功能,但是还是推荐使用TcpConnection的方式.
7979
- 通过接口可以访问底层Socket对象扩展逻辑,比如修改超时时间.
8080
- 通过接口可以获得发送接收缓冲区,比如断开连接时用户如何处理缓冲区数据?直接清空还是重连后继续发送.n
8181
- OnSocketReceive和OnReceive是不同的,比如当OnSocketReceive接受大小是100字节,当用户解包时不做操作,OnReceive大小是100字节,当用户解包时做解压缩(解密等)操作后,OnReceive大小不再是100.
82-
- 可以向TcpConnection(UdpConnection)添加不同的插件完成所需的功能,
82+
- 可以向TcpConnection添加不同的插件完成所需的功能,
8383
- 注册基类可以方便快速注册消息(基于反射)
8484
- 加密采用AES的方式,如果想使用加密可以调用这部分的接口加密字节数据.
8585
- .etc
@@ -138,27 +138,33 @@ Udp协议提供不可靠的报文消息,用户无法知道当前连接状态,但
138138

139139
Package example:
140140
```csharp
141-
public class Package : PackageBase
141+
public class PackageExample:PackageBase
142142
{
143143
protected override void Pack(BlockBuffer<byte> bytes, Action<byte[]> onPacked)
144144
{
145+
//Use int as header
145146
int length = bytes.WritePosition;
146147
var header = BitConverter.GetBytes(length);
147-
var newBytes = new byte[length + header.Length];
148-
Buffer.BlockCopy(header, 0, newBytes, 0, header.Length);
149-
Buffer.BlockCopy(bytes.Buffer, 0, newBytes, header.Length, length);
150-
onPacked(newBytes);
148+
var newBytes = new BlockBuffer<byte>(length + header.Length);
149+
//Write header and body to buffer
150+
newBytes.Write(header);
151+
newBytes.Write(bytes.Buffer);
152+
//Notice pack funished
153+
onPacked(newBytes.Buffer);
151154
}
152155

153156
protected override void Unpack(BlockBuffer<byte> bytes, Action<byte[]> onUnpacked)
154157
{
158+
//Because header is int and cost 4 byte
155159
while (bytes.WritePosition > 4)
156160
{
157161
int length = BitConverter.ToInt32(bytes.Buffer, 0);
162+
//If receive body
158163
if (bytes.WritePosition >= 4 + length)
159164
{
160165
bytes.MoveReadPostion(4);
161166
var data = bytes.Read(length);
167+
//Notice unpack finished
162168
onUnpacked(data);
163169
bytes.ResetIndex();
164170
}
@@ -168,40 +174,53 @@ public class Package : PackageBase
168174
```
169175

170176
```csharp
171-
private IPackage package = new PackageExample();
172-
private TcpConnection tcp;
173-
static void Main(string[] args)
177+
TcpConnection tcp;
178+
void Connect()
174179
{
180+
tcp = new TcpConnection(new PackageExample());
181+
tcp.OnDisconnected += OnDisconnect;
182+
tcp.Connect("127.0.0.1", 999);
183+
tcp.Socket.NoDelay = true;
184+
tcp.Socket.SendTimeout = 100;
185+
tcp.Socket.ReceiveTimeout = 200;
186+
//...
175187
176-
}
177-
void Init()
178-
{
179-
tcp = new TcpConnection(package);
180-
tcp.OnConnected += OnConnected;
181-
tcp.OnReceive += Receive;
182-
//_tcp.OnError
183-
//_tcp.OnDisconnected
184-
}
185-
void OnConnected()
186-
{
187-
//connect success
188-
tcp.Send(new byte[10]);//send message
189-
tcp.DisConnect();//disconnect
188+
189+
// you can add plugin sub from IPlugins
190+
tcp.AddPlugin(new StatisticalPlugin("Statistical"));//this plugin calculate how many send
190191
}
191192

192-
void Receive(byte[] bytes)
193+
void OnDisconnect()
193194
{
194-
//get message from server
195+
var length = tcp.SendBuffer.WritePosition;
196+
Console.WriteLine("Still have {0} not send to server when abnormal shutdown");
197+
var data = tcp.SendBuffer.Read(length);
198+
tcp.SendBuffer.ResetIndex();
199+
200+
//use can handle these data, for example maybe can send next time when connect again
201+
//tcp.Send(data);
195202
}
196203
```
197204

198205
```csharp
199-
void Init()
206+
/// <summary>
207+
/// The recommend is use TcpConnection
208+
/// </summary>
209+
class Example3
210+
{
211+
TcpSocket tcp; //The recommend is use TcpConnection
212+
void Connect()
200213
{
201-
var tcp = new TcpConnection(new PackageExample());
202-
tcp.AddPlugin(new PingPlugin("ping", tcp));
203-
//tcp.GetPlugin("ping");
214+
tcp = new TcpSocket(1024);//set buffer size
215+
tcp.OnReceiveBytes += OnReceive;
216+
tcp.Connect("127.0.0.1", 999);
204217
}
218+
219+
void OnReceive(byte[] bytes)
220+
{
221+
//split bytes here
222+
}
223+
}
205224
```
206225

207226

example/csharp/HiSocket.Example/HiSocket.Example.sln

-25
This file was deleted.

example/csharp/HiSocket.Example/HiSocket.Example/BinaryMsg.cs

-30
This file was deleted.

example/csharp/HiSocket.Example/HiSocket.Example/Package.cs

-34
This file was deleted.

example/csharp/HiSocket.Example/HiSocket.Example/Program.cs

-17
This file was deleted.

0 commit comments

Comments
 (0)