public void BytesHelperShouldReturnEmptyArrayOnEmptyString() { var input = string.Empty; var expectedBytes = new byte[0]; var actualBytes = BytesHelper.GetBytes(input); Assert.Equal(expectedBytes, actualBytes); }
public void BytesHelperShouldReturnCorrectBytesForString() { var input = "Test"; var expectedBytes = new byte[4] { 84, 101, 115, 116 }; var actualBytes = BytesHelper.GetBytes(input); Assert.Equal(expectedBytes, actualBytes); }
/// <summary> /// 像指定的客户端发消息 /// </summary> /// <param name="c"></param> /// <param name="data"></param> public void SendMessageToClient(TcpClient c, byte[] data) { if (null != c) { try { byte[] size = BytesHelper.GetBytes((ushort)data.Length); //简易封包协议:包长度+包体 var temp = new byte[size.Length + data.Length]; Buffer.BlockCopy(size, 0, temp, 0, size.Length); Buffer.BlockCopy(data, 0, temp, size.Length, data.Length); c.GetStream().Write(temp, 0, temp.Length); c.GetStream().Flush(); } catch (Exception e) { Debug.Log($"{nameof(TCPServer)}: Send Message To Client Failed - {e}"); } } }
void SendNetMessage(string str) { try { if (isRun) { var networkStream = tcpClient.GetStream(); var data = Encoding.UTF8.GetBytes(str); byte[] size = BytesHelper.GetBytes((ushort)data.Length); var temp = new byte[size.Length + data.Length]; Buffer.BlockCopy(size, 0, temp, 0, size.Length); Buffer.BlockCopy(data, 0, temp, size.Length, data.Length); networkStream.Write(temp, 0, temp.Length); networkStream.Flush(); Debug.Log($"[控制器] 发送到播放器消息 {str}!"); UnitySynchronizationContext.Post(() => { if (message) { message.text = $"[控制器] 发送到播放器消息 {str}!"; } }); } else { Debug.LogWarning($"{nameof(PlayerController)}: 已经与服务器断开连接!"); } } catch (Exception e) { UnitySynchronizationContext.Post(() => { if (message) { message.text = $"[控制器] 发送消息到播放器错误 {e}!"; } }); throw; } }
public IAndDistributedCacheTestBuilder ContainingEntry(string key, string value) { this.ContainingEntry(key, BytesHelper.GetBytes(value)); return(this); }
public IAndDistributedCacheEntryBuilder WithValue(string value) { this.WithValue(BytesHelper.GetBytes(value)); return(this); }