1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| public class Connect { public static Socket socket; int BUFFER_SIZE = NetCloudClientManager.instance.BUFFER_SIZE; byte[] readBuff ; private int buffCount = 0;
private Int32 msgLength = 0; private byte[] lenByte = new byte[sizeof(Int32)];
public ProtocolByte assistProtolByte;
public MsgHandle msgHandle = new MsgHandle();
public enum Status { None,Connect }; public Status status = Status.None; public static int width = 256;
public Connect() { readBuff = new byte[BUFFER_SIZE]; assistProtolByte = new ProtocolByte(); }
public bool Connetion(string host, int port) {
try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (host.Length == 0) { host = "192.168.50.142"; port = 1234; }
socket.Connect(host, port); status = Status.Connect; NetCloudClientManager.instance.debug.text += ("\n" + "客户端地址" + socket.LocalEndPoint.ToString()); socket.BeginReceive(readBuff, buffCount, BUFFER_SIZE - buffCount, SocketFlags.None, ReceiveCb, readBuff); return true; } catch(Exception e) { NetCloudClientManager.instance.debug.text += ("\n" + "客户端失败"+e.Message ); return false;
}
} private void ReceiveCb(IAsyncResult ar) { try { int count = socket.EndReceive(ar); buffCount += count; ProcessData(); socket.BeginReceive(readBuff,buffCount , BUFFER_SIZE-buffCount, SocketFlags.None, ReceiveCb, readBuff); } catch (Exception e) { socket.Close(); status = Status.None; }
}
private void ProcessData() { if (buffCount < sizeof(Int32)) return; Array.Copy(readBuff, lenByte, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenByte, 0); if (buffCount < msgLength + sizeof(Int32)) return; ProtocolBase protocol = assistProtolByte.Decode(readBuff, sizeof(Int32), msgLength); lock(msgHandle.msgList) { msgHandle.msgList.Add(protocol); } int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, sizeof(Int32) + msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) ProcessData(); }
public bool Close() { try { socket.Close(); return true; } catch (Exception e) { Debug.Log("关闭失败:" + e.Message); return false; } }
public bool SendMsg( ProtocolBase protol) { string proName = protol.GetName(); ; return SendMsg(protol, proName);
} public bool SendMsg(ProtocolBase protol,string protolName) { if (status != Status.Connect) { Debug.Log("404 Not Found"); return false; } ProtocolByte protocolByte1 = new ProtocolByte(); protocolByte1.AddString(protolName); byte[] protolNameByte = protocolByte1.bytes; byte[] b = protol.Encode(); byte[] len1 = BitConverter.GetBytes(protolNameByte.Length + b.Length); byte[] sendByte = len1.Concat(protolNameByte).Concat(b).ToArray(); socket.Send(sendByte); Debug.Log(GetStr(sendByte)); return true; } public string GetStr(byte[] bytes) { string str = ""; if (bytes == null) return str; for (int i = 0; i < bytes.Length; i++) { int b = (int)bytes[i]; str += b.ToString() + " "; } return str; } #region 地址辅助方法 public string GetClientAddress() { return socket.LocalEndPoint.ToString(); } public string GetLocalIp() { string AddressIP = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) {
if (_IPAddress.AddressFamily.ToString() == "InterNetwork") {
AddressIP = _IPAddress.ToString(); } } return AddressIP; } #endregion }
|