1 public class MyEasyClient 2 { 3 EasyClient<MyPackageInfo> client = null; 4 public async void Connect(string ip, int prot) 5 { 6 client = new EasyClient<MyPackageInfo>(); 7 client.Initialize(new MyReceiveFilter()); 8 client.Connected += OnClientConnected; 9 client.NewPackageReceived += OnPackageReceived; 10 client.Error += OnClientError; 11 client.Closed += OnClientClosed; 12 var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Parse(ip), prot)); 13 if (connected) 14 { 15 //连接成功 16 } 17 else 18 { 19 //连接失败 20 } 21 } 22 23 private void DebugLog(string log) 24 { 25 System.Diagnostics.Debug.WriteLine("MyEasyClient:" + log); 26 } 27 28 private void OnPackageReceived(object sender, PackageEventArgs<MyPackageInfo> e) 29 { 30 DebugLog("收到数据:" + e.Package.Body); 31 if (OnRecive != null) 32 { 33 OnRecive(e.Package.Body); 34 } 35 } 36 37 private void OnClientConnected(object sender, EventArgs e) 38 { 39 DebugLog("已连接到服务器..."); 40 } 41 42 private void OnClientClosed(object sender, EventArgs e) 43 { 44 DebugLog("连接已断开..."); 45 } 46 47 private void OnClientError(object sender, SuperSocket.ClientEngine.ErrorEventArgs e) 48 { 49 DebugLog("客户端错误:" + e.Exception.Message); 50 } 51 52 public bool IsConnected() 53 { 54 if (client != null && client.IsConnected == true) 55 { 56 return true; 57 } 58 else 59 { 60 return false; 61 } 62 } 63 64 /// <summary> 65 /// 发送数据 66 /// </summary> 67 public void Send(byte[] data) 68 { 69 if (IsConnected()) 70 { 71 client.Send(data); 72 } 73 } 74 }
1 public class MyReceiveFilter : FixedHeaderReceiveFilter<MyPackageInfo> 2 { 3 public MyReceiveFilter() 4 : base(12) 5 { 6 7 } 8 protected override int GetBodyLengthFromHeader(IBufferStream bufferStream, int length) 9 { 10 ArraySegment<byte> buffers = bufferStream.Buffers[0]; 11 byte[] array = buffers.ToArray(); 12 13 DataHead head = ByteHelper.BytesToStruct<DataHead>(array); 18 return (int)head.Length; 19 } 20 public override MyPackageInfo ResolvePackage(IBufferStream bufferStream) 21 { 22 //第三个参数用0,1都可以 23 byte[] header = bufferStream.Buffers[0].ToArray(); 24 byte[] bodyBuffer = bufferStream.Buffers[1].ToArray(); 25 byte[] allBuffer = bufferStream.Buffers[0].Array.CloneRange(0, (int)bufferStream.Length); 26 return new MyPackageInfo(header, bodyBuffer); 27 } 28 } 29 30 public class MyReceiveFilter1 : IReceiveFilter<MyPackageInfo1> 31 { 32 MyPackageInfo1 IReceiveFilter<MyPackageInfo1>.Filter(BufferList data, out int rest) 33 { 34 rest = 0; 35 if (data.Total > 0) 36 return new MyPackageInfo1(data.Last.CloneRange(data.Last.Offset, data.Last.Count)); 37 38 return default(MyPackageInfo1); 39 } 40 41 public void Reset() 42 { 43 NextReceiveFilter = null; 44 State = FilterState.Normal; 45 } 46 47 public IReceiveFilter<MyPackageInfo1> NextReceiveFilter { get; protected set; } 48 public FilterState State { get; protected set; } 49 }
1 /// <summary> 2 /// 有协议头 3 /// </summary> 4 public class MyPackageInfo : IPackageInfo 5 { 6 public MyPackageInfo(byte[] header, byte[] bodyBuffer) 7 { 8 Header = header; 9 Data = bodyBuffer; 10 } 11 /// <summary> 12 /// 服务器返回的字节数据头部 13 /// </summary> 14 public byte[] Header { get; set; } 15 16 /// <summary> 17 /// 服务器返回的字节数据 18 /// </summary> 19 public byte[] Data { get; set; } 20 21 22 /// <summary> 23 /// 服务器返回的字符串数据 24 /// </summary> 25 public string Body 26 { 27 get 28 { 29 return Encoding.UTF8.GetString(Data); 30 } 31 } 32 } 33 34 35 /// <summary> 36 /// 无协议头 37 /// </summary> 38 public class MyPackageInfo1 : IPackageInfo 39 { 40 public MyPackageInfo1( byte[] bodyBuffer) 41 { 42 Data = bodyBuffer; 43 } 44 45 /// <summary> 46 /// 服务器返回的字节数据 47 /// </summary> 48 public byte[] Data { get; set; } 49 /// <summary> 50 /// 服务器返回的字符串数据 51 /// </summary> 52 public string Body 53 { 54 get 55 { 56 return Encoding.UTF8.GetString(Data); 57 } 58 } 59 }