|
7 | 7 | // <time> #2018年7月26日 23点25分# </time>
|
8 | 8 | //-----------------------------------------------------------------------
|
9 | 9 |
|
| 10 | +using System; |
10 | 11 | using System.Collections;
|
11 | 12 | using System.Collections.Generic;
|
| 13 | +using System.Net; |
| 14 | +using System.Net.Sockets; |
| 15 | +using System.Threading; |
12 | 16 |
|
13 | 17 | namespace GameFramework.Taurus
|
14 | 18 | {
|
15 | 19 | public class KcpChannel
|
16 | 20 | {
|
| 21 | + private static readonly DateTime utc_time = new DateTime(1970, 1, 1); |
| 22 | + |
| 23 | + public static UInt32 iclock() |
| 24 | + { |
| 25 | + return (UInt32)(Convert.ToInt64(DateTime.UtcNow.Subtract(utc_time).TotalMilliseconds) & 0xffffffff); |
| 26 | + } |
| 27 | + |
| 28 | + #region 属性 |
| 29 | + |
| 30 | + private KCP _kcp; |
| 31 | + |
| 32 | + private UdpClient _udpClient; |
| 33 | + |
| 34 | + private IPEndPoint _targetEndPoinnt; |
| 35 | + |
| 36 | + private IPEndPoint _allEndPoint; |
| 37 | + |
| 38 | + private IPEndPoint _currentEndPoint; |
| 39 | + |
| 40 | + private IPEndPoint _recEndPoint; |
| 41 | + |
| 42 | + private Queue<byte[]> _receiveMeesages; |
| 43 | + |
| 44 | + private Action<byte[]> _reveiveHandler; |
| 45 | + #endregion |
| 46 | + |
| 47 | + |
| 48 | + public KcpChannel(string ip,int port,Action<byte[]> reveiveHandler) |
| 49 | + { |
| 50 | + _allEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), port); |
| 51 | + _recEndPoint = new IPEndPoint(IPAddress.Any, 0); |
| 52 | + |
| 53 | + _reveiveHandler = reveiveHandler; |
| 54 | + |
| 55 | + _udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(ip), port)); |
| 56 | + _kcp = new KCP((UInt32) new Random((int) DateTime.Now.Ticks).Next(1, Int32.MaxValue), |
| 57 | + UdpSendData); |
| 58 | + |
| 59 | + //kcp fast mode. |
| 60 | + _kcp.NoDelay(1, 10, 2, 1); |
| 61 | + _kcp.WndSize(128, 128); |
| 62 | + |
| 63 | + _receiveMeesages = new Queue<byte[]>(); |
| 64 | + |
| 65 | + Thread revThread = new Thread(ReceiveMessage); |
| 66 | + revThread.Start(); |
| 67 | + Thread updataThread = new Thread(Update); |
| 68 | + updataThread.Start(); |
| 69 | + } |
| 70 | + |
| 71 | + public void Connect(string ip, int port) |
| 72 | + { |
| 73 | + _targetEndPoinnt = new IPEndPoint(IPAddress.Parse(ip), port); |
| 74 | + _currentEndPoint = _targetEndPoinnt; |
| 75 | + } |
| 76 | + |
| 77 | + private void UdpSendData(byte[] datas, int length) |
| 78 | + { |
| 79 | + if(_currentEndPoint!=null) |
| 80 | + _udpClient?.Send(datas, length, _currentEndPoint); |
| 81 | + } |
| 82 | + |
| 83 | + public void Send(byte[] datas,bool toAll=false) |
| 84 | + { |
| 85 | + if (toAll) |
| 86 | + _currentEndPoint = _allEndPoint; |
| 87 | + else |
| 88 | + _currentEndPoint = _targetEndPoinnt; |
| 89 | + _kcp?.Send(datas); |
| 90 | + } |
| 91 | + |
| 92 | + private void ReceiveMessage() |
| 93 | + { |
| 94 | + while (true) |
| 95 | + { |
| 96 | + byte[] datas = _udpClient.Receive(ref _recEndPoint); |
| 97 | + if (datas != null) |
| 98 | + { |
| 99 | + _receiveMeesages.Enqueue(datas); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void Update() |
| 105 | + { |
| 106 | + while (_receiveMeesages.Count > 0) |
| 107 | + { |
| 108 | + var buf = _receiveMeesages.Dequeue(); |
| 109 | + |
| 110 | + _kcp.Input(buf); |
| 111 | + // mNeedUpdateFlag = true; |
| 112 | + |
| 113 | + for (var size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize()) |
| 114 | + { |
| 115 | + var buffer = new byte[size]; |
| 116 | + if (_kcp.Recv(buffer) > 0) |
| 117 | + { |
| 118 | + _reveiveHandler(buffer); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + _kcp.Update(iclock()); |
| 124 | + Thread.Sleep(10); |
| 125 | + } |
| 126 | + |
17 | 127 |
|
18 | 128 | }
|
19 | 129 |
|
|
0 commit comments