Skip to content

Commit 5cbddc8

Browse files
author
Hiram
committed
modify logic
1 parent d408162 commit 5cbddc8

Some content is hidden

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

58 files changed

+173
-199
lines changed

HiSocket_2.8.0.zip

117 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using HiSocket.Message;
7+
8+
namespace HiSocket.Example
9+
{
10+
class BinaryMsg:BinaryMsgBase
11+
{
12+
public override void Regist()
13+
{
14+
BinaryMsgRegister.Regist(1001,OnMsg);
15+
}
16+
17+
public void OnMsg(BinaryReader reader)
18+
{
19+
int hp = reader.ReadInt32();
20+
int attack = reader.ReadInt32();
21+
}
22+
23+
public void Send(int hp,int attack)
24+
{
25+
Writer.Write(BitConverter.GetBytes(hp));
26+
Writer.Write(BitConverter.GetBytes(attack));
27+
Flush();
28+
}
29+
}
30+
}

example/csharp/HiSocket.Example/HiSocket.Example/HiSocket.Example.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@
5858
<Reference Include="System.Xml" />
5959
</ItemGroup>
6060
<ItemGroup>
61+
<Compile Include="BinaryMsg.cs" />
6162
<Compile Include="Package.cs" />
6263
<Compile Include="Program.cs" />
6364
<Compile Include="Properties\AssemblyInfo.cs" />
65+
<Compile Include="ProtobufMsg.cs" />
6466
</ItemGroup>
6567
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6668
</Project>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using HiSocket.Tcp;
33
using System;
44

5-
namespace HiSocket.Test
5+
namespace HiSocket.Example
66
{
77
public class Package : PackageBase
88
{

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
1+
using HiSocket.Message;
52
using HiSocket.Tcp;
6-
using HiSocket.Test;
73

84
namespace HiSocket.Example
95
{
@@ -13,8 +9,8 @@ static void Main(string[] args)
139
{
1410
ITcpConnection tcp = new TcpConnection(new Package());
1511
tcp.OnSendMessage += (x) => { };
16-
tcp.OnReceiveMessage += (x) => { };
17-
tcp.Connect("127.0.0.1",7777);
12+
tcp.OnReceiveMessage += (x) => { };
13+
tcp.Connect("127.0.0.1", 7777);
1814
tcp.Send(new byte[10]);
1915
}
2016
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using HiSocket.Message;
6+
7+
namespace HiSocket.Example
8+
{
9+
class ProtobufMsg:ProtobufMsgBase
10+
{
11+
public override void Regist()
12+
{
13+
ProtobufMsgRegister.Regist<ThisIsAProtobufClass>(OnMsg);
14+
}
15+
16+
public void OnMsg(ThisIsAProtobufClass msg)
17+
{
18+
var hp = msg.Hp;
19+
var attack = msg.Attack;
20+
}
21+
}
22+
23+
class ThisIsAProtobufClass
24+
{
25+
public int Hp;
26+
public int Attack;
27+
}
28+
}

example/unity/Assets/Example/Example.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55
66
***************************************************************/
77

8-
using HiSocket;
98
using HiSocketExample;
109
using System;
1110
using System.Net;
11+
using HiSocket.Tcp;
1212
using UnityEngine;
1313

1414
public class Example : MonoBehaviour
1515
{
1616
private TcpConnection _tcp;
1717

18-
private TestServer _server = new TestServer();
1918
private bool _isConnected;
2019
// Use this for initialization
2120
void Start()
2221
{
2322
var ip = IPAddress.Parse("127.0.0.1");
2423
var iep = new IPEndPoint(ip, 7777);
25-
_tcp = new TcpConnection(new PackageExample());
24+
_tcp = new TcpConnection(new Package());
2625
_tcp.OnConnecting += OnConnecting;
2726
_tcp.OnConnected += OnConnected;
28-
_tcp.OnReceive += OnReceive;
27+
_tcp.OnReceiveMessage += OnReceive;
2928

3029
_tcp.Connect(iep); //start connect
3130
}
@@ -66,6 +65,5 @@ void OnReceive(byte[] bytes)
6665
void OnApplicationQuit()
6766
{
6867
_tcp.Dispose();
69-
_server.Close();
7068
}
7169
}

example/unity/Assets/Example/HiSocketExample.meta

-10
This file was deleted.

example/unity/Assets/Example/HiSocketExample/PackageExample.cs

-57
This file was deleted.

example/unity/Assets/Example/HiSocketExample/PackageExample.cs.meta

-13
This file was deleted.

example/unity/Assets/Example/HiSocketExample/PluginExample.cs

-20
This file was deleted.

example/unity/Assets/Example/HiSocketExample/PluginExample.cs.meta

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/***************************************************************
2+
* Description:
3+
*
4+
* Documents: https://github.com/hiramtan/HiSocket
5+
6+
***************************************************************/
7+
8+
using HiFramework;
9+
using HiSocket.Tcp;
10+
using System;
11+
12+
namespace HiSocketExample
13+
{
14+
/// <summary>
15+
/// Example: Used to pack or unpack message
16+
/// You should inheritance IPackage interface and implement your own logic
17+
/// </summary>
18+
public class Package : PackageBase
19+
{
20+
protected override void Pack(BlockBuffer<byte> bytes, Action<byte[]> onPacked)
21+
{
22+
int length = bytes.WritePosition;
23+
var header = BitConverter.GetBytes(length);
24+
var newBytes = new byte[length + header.Length];
25+
Buffer.BlockCopy(header, 0, newBytes, 0, header.Length);
26+
Buffer.BlockCopy(bytes.Buffer, 0, newBytes, header.Length, length);
27+
onPacked(newBytes);
28+
}
29+
30+
protected override void Unpack(BlockBuffer<byte> bytes, Action<byte[]> onUnpacked)
31+
{
32+
while (bytes.WritePosition > 4)
33+
{
34+
int length = BitConverter.ToInt32(bytes.Buffer, 0);
35+
if (bytes.WritePosition >= 4 + length)
36+
{
37+
bytes.MoveReadPostion(4);
38+
var data = bytes.Read(length);
39+
onUnpacked(data);
40+
bytes.ResetIndex();
41+
}
42+
}
43+
}
44+
}
45+
}

example/unity/Assets/Example/TestServer.cs

-43
This file was deleted.

example/unity/Assets/Example/TestServer.cs.meta

-11
This file was deleted.
Binary file not shown.
Binary file not shown.
24 KB
Binary file not shown.
-512 Bytes
Binary file not shown.
Binary file not shown.

example/unity/Assets/HiSocket/HiSocket.Message.dll.mdb.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/unity/Assets/HiSocket/HiSocket.Message.dll.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

example/unity/Assets/HiSocket/HiSocket.Message.pdb.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-13 KB
Binary file not shown.
-7.95 KB
Binary file not shown.

example/unity/Assets/HiSocket/HiSocket.dll.mdb.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/unity/Assets/HiSocket/HiSocket.dll.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-46 KB
Binary file not shown.

0 commit comments

Comments
 (0)