Skip to content

Commit

Permalink
modify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hiram3512 committed Jul 25, 2019
1 parent d408162 commit 5cbddc8
Show file tree
Hide file tree
Showing 58 changed files with 173 additions and 199 deletions.
Binary file added HiSocket_2.8.0.zip
Binary file not shown.
30 changes: 30 additions & 0 deletions example/csharp/HiSocket.Example/HiSocket.Example/BinaryMsg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using HiSocket.Message;

namespace HiSocket.Example
{
class BinaryMsg:BinaryMsgBase
{
public override void Regist()
{
BinaryMsgRegister.Regist(1001,OnMsg);
}

public void OnMsg(BinaryReader reader)
{
int hp = reader.ReadInt32();
int attack = reader.ReadInt32();
}

public void Send(int hp,int attack)
{
Writer.Write(BitConverter.GetBytes(hp));
Writer.Write(BitConverter.GetBytes(attack));
Flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BinaryMsg.cs" />
<Compile Include="Package.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProtobufMsg.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using HiSocket.Tcp;
using System;

namespace HiSocket.Test
namespace HiSocket.Example
{
public class Package : PackageBase
{
Expand Down
10 changes: 3 additions & 7 deletions example/csharp/HiSocket.Example/HiSocket.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HiSocket.Message;
using HiSocket.Tcp;
using HiSocket.Test;

namespace HiSocket.Example
{
Expand All @@ -13,8 +9,8 @@ static void Main(string[] args)
{
ITcpConnection tcp = new TcpConnection(new Package());
tcp.OnSendMessage += (x) => { };
tcp.OnReceiveMessage += (x) => { };
tcp.Connect("127.0.0.1",7777);
tcp.OnReceiveMessage += (x) => { };
tcp.Connect("127.0.0.1", 7777);
tcp.Send(new byte[10]);
}
}
Expand Down
28 changes: 28 additions & 0 deletions example/csharp/HiSocket.Example/HiSocket.Example/ProtobufMsg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HiSocket.Message;

namespace HiSocket.Example
{
class ProtobufMsg:ProtobufMsgBase
{
public override void Regist()
{
ProtobufMsgRegister.Regist<ThisIsAProtobufClass>(OnMsg);
}

public void OnMsg(ThisIsAProtobufClass msg)
{
var hp = msg.Hp;
var attack = msg.Attack;
}
}

class ThisIsAProtobufClass
{
public int Hp;
public int Attack;
}
}
8 changes: 3 additions & 5 deletions example/unity/Assets/Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
* Author: [email protected]
***************************************************************/

using HiSocket;
using HiSocketExample;
using System;
using System.Net;
using HiSocket.Tcp;
using UnityEngine;

public class Example : MonoBehaviour
{
private TcpConnection _tcp;

private TestServer _server = new TestServer();
private bool _isConnected;
// Use this for initialization
void Start()
{
var ip = IPAddress.Parse("127.0.0.1");
var iep = new IPEndPoint(ip, 7777);
_tcp = new TcpConnection(new PackageExample());
_tcp = new TcpConnection(new Package());
_tcp.OnConnecting += OnConnecting;
_tcp.OnConnected += OnConnected;
_tcp.OnReceive += OnReceive;
_tcp.OnReceiveMessage += OnReceive;

_tcp.Connect(iep); //start connect
}
Expand Down Expand Up @@ -66,6 +65,5 @@ void OnReceive(byte[] bytes)
void OnApplicationQuit()
{
_tcp.Dispose();
_server.Close();
}
}
10 changes: 0 additions & 10 deletions example/unity/Assets/Example/HiSocketExample.meta

This file was deleted.

57 changes: 0 additions & 57 deletions example/unity/Assets/Example/HiSocketExample/PackageExample.cs

This file was deleted.

This file was deleted.

20 changes: 0 additions & 20 deletions example/unity/Assets/Example/HiSocketExample/PluginExample.cs

This file was deleted.

13 changes: 0 additions & 13 deletions example/unity/Assets/Example/HiSocketExample/PluginExample.cs.meta

This file was deleted.

45 changes: 45 additions & 0 deletions example/unity/Assets/Example/PackageExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************
* Description:
*
* Documents: https://github.com/hiramtan/HiSocket
* Author: [email protected]
***************************************************************/

using HiFramework;
using HiSocket.Tcp;
using System;

namespace HiSocketExample
{
/// <summary>
/// Example: Used to pack or unpack message
/// You should inheritance IPackage interface and implement your own logic
/// </summary>
public class Package : PackageBase
{
protected override void Pack(BlockBuffer<byte> bytes, Action<byte[]> onPacked)
{
int length = bytes.WritePosition;
var header = BitConverter.GetBytes(length);
var newBytes = new byte[length + header.Length];
Buffer.BlockCopy(header, 0, newBytes, 0, header.Length);
Buffer.BlockCopy(bytes.Buffer, 0, newBytes, header.Length, length);
onPacked(newBytes);
}

protected override void Unpack(BlockBuffer<byte> bytes, Action<byte[]> onUnpacked)
{
while (bytes.WritePosition > 4)
{
int length = BitConverter.ToInt32(bytes.Buffer, 0);
if (bytes.WritePosition >= 4 + length)
{
bytes.MoveReadPostion(4);
var data = bytes.Read(length);
onUnpacked(data);
bytes.ResetIndex();
}
}
}
}
}
43 changes: 0 additions & 43 deletions example/unity/Assets/Example/TestServer.cs

This file was deleted.

11 changes: 0 additions & 11 deletions example/unity/Assets/Example/TestServer.cs.meta

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file added example/unity/Assets/HiSocket/HiFramework.dll
Binary file not shown.
Binary file modified example/unity/Assets/HiSocket/HiSocket.Message.dll
Binary file not shown.
Binary file modified example/unity/Assets/HiSocket/HiSocket.Message.dll.mdb
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/unity/Assets/HiSocket/HiSocket.Message.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified example/unity/Assets/HiSocket/HiSocket.Message.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion example/unity/Assets/HiSocket/HiSocket.Message.pdb.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified example/unity/Assets/HiSocket/HiSocket.dll
Binary file not shown.
Binary file modified example/unity/Assets/HiSocket/HiSocket.dll.mdb
Binary file not shown.
2 changes: 1 addition & 1 deletion example/unity/Assets/HiSocket/HiSocket.dll.mdb.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/unity/Assets/HiSocket/HiSocket.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified example/unity/Assets/HiSocket/HiSocket.pdb
Binary file not shown.
Loading

0 comments on commit 5cbddc8

Please sign in to comment.