-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
- Loading branch information
There are no files selected for viewing
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
@@ -66,6 +65,5 @@ void OnReceive(byte[] bytes) | |
void OnApplicationQuit() | ||
{ | ||
_tcp.Dispose(); | ||
_server.Close(); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.