//============================================================ // <T>从字节流中读取指定长度的数据保存在指定数组中。</T> // // @param bytes 保存数据的数组 // @param lenth 读取的字节数 //============================================================ public void ReadLength(FBytes bytes, int length) { int read = 0; int remain = length; int block = RInt.SIZE_4K; byte[] memory = new byte[block]; while (remain > 0) { block = (remain > block) ? block : remain; read = _socket.NativeSocket.Receive(memory, 0, block, SocketFlags.None); bytes.Append(memory, 0, read); remain -= read; } }
//============================================================ // <T>读取所有数据。</T> // // @return 保存所有数据的数组 //============================================================ public FBytes ReadAll() { FBytes bytes = new FBytes(); byte[] buffer = new byte[_readLimit]; while (true) { int length = _socket.NativeSocket.Receive(buffer, 0, _readLimit, SocketFlags.None); if (length > 0) { bytes.Append(buffer, 0, length); } // 检查是否完成 if (length != _readLimit) { break; } } return(bytes); }
public FAsmCommands Parse(uint address, byte[] memory, int offset, int length) { FBytes bytes = new FBytes(); bytes.Append(memory, offset, length); FAsmCommands cmds = new FAsmCommands(); int end = offset + length; for (int n = offset; n < end; n++) { FAsmCommand cmd = new FAsmCommand(); cmd.Address = address; if (_codes.Parse(cmd, bytes, n, 0)) { address += (uint)cmd.Size; n += cmd.Size - 1; cmds.Push(cmd); } } return(cmds); }
//============================================================ // <T>执行处理。</T> //============================================================ public override void OnProcess() { FBytes data = new FBytes(); // 异步处理 while (!IsStop) { // 接收数据 int length = _socket.Input.ReadAll(data); if (length == -1) { break; } if (length > 0) { lock (_socketData) { _socketData.Append(data); } } data.Clear(); } // 移除端口线程 _service.SocketThreadRemove(this); }
//============================================================ // <T>从字节流中读取所有数据。</T> // // @param data 输出 // @return 读取长度 //============================================================ public int ReadAll(FBytes data) { int total = 0; byte[] buffer = new byte[_readLimit]; try { while (true) { // 接收数据 int length = _socket.NativeSocket.Receive(buffer, 0, _readLimit, SocketFlags.None); data.Append(buffer, 0, length); total += length; // 检查是否完成 if (length != _readLimit) { break; } } } catch (SocketException exception) { Console.WriteLine(exception.Message); total = -1; } return(total); }
protected FBytes Build() { FBytes bytes = new FBytes(); FString line = new FString(); // Protocol if (RString.IsEmpty(_method)) { if (_values.IsEmpty) { line.Append("GET "); } else { line.Append("POST "); } } else { line.Append(_method); line.Append(" "); } if (_connection.UseProxy) { line.Append(_connection.Url.ToString()); } else { if (RString.IsEmpty(_connection.Url.Path)) { line.Append("/"); } else { line.Append(_connection.Url.Path); } } line.Append(" HTTP/1.1\r\n"); // Values FString value = new FString(); if (!_values.IsEmpty) { int count = _values.Count; for (int n = 0; n < count; n++) { if (n != 0) { value.Append('&'); } value.Append(_values.Name(n)); value.Append('='); value.Append(_values.Value(n)); } _heads["Content-Length"] = value.Length.ToString(); } bytes.Append(line.ToBytes(Encoding.ASCII)); // Heads if (!_heads.IsEmpty) { int count = _heads.Count; for (int n = 0; n < count; n++) { line.Clear(); line.Append(_heads.Name(n)); line.Append(": "); line.Append(_heads.Value(n)); line.Append("\r\n"); bytes.Append(line.ToBytes(Encoding.ASCII)); } } /*// Host Fix * if (!_heads.Contains("Host")) { * line.Clear(); * line.Append("Host: "); * line.Append(_connection.Url.Host); * line.Append("\r\n"); * bytes.Append(line.ToBytes(Encoding.ASCII)); * }*/ // Make line.Clear(); line.Append("\r\n"); bytes.Append(line.ToBytes(Encoding.ASCII)); if (!_values.IsEmpty) { bytes.Append(value.ToBytes(Encoding.ASCII)); } line.Append("\r\n"); return(bytes); }