public static devproxy_header_t devproxy_cast(byte[] pcHeader) { global::System.IntPtr cPtr = protocommPINVOKE.devproxy_cast(pcHeader); devproxy_header_t ret = (cPtr == global::System.IntPtr.Zero) ? null : new devproxy_header_t(cPtr, false); return(ret); }
private bool IsHeaderValid(ref devproxy_header_t header) { bool ret = true; if (header.SOF != (uint)protocomm.DEVPROXY_HEADER_MAGIC) { return(false); } switch (header.code) { case devproxy_opcode_t.PROXY_CMD_READ: case devproxy_opcode_t.PROXY_CMD_WRITE: break; default: ret = false; break; } ret = header.datalen > 0; return(ret); }
private void Mainloop() { // Accept connection (blocking) TcpClient client = server.AcceptTcpClient(); // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int ret = 0; byte[] arrHeaderReq = new byte[protocomm.sizeof_devproxy_header_t()]; devproxy_header_t headerReq = protocomm.devproxy_cast(arrHeaderReq); byte[] arrHeaderReply = new byte[protocomm.sizeof_devproxy_header_t()]; devproxy_header_t headerReply = protocomm.devproxy_cast(arrHeaderReply); headerReply.SOF = (uint)protocomm.DEVPROXY_HEADER_MAGIC; while (_bRunTask) { // Wait for a header packet ret = stream.Read(arrHeaderReq, 0, arrHeaderReq.Length); // Decode and execute if ((ret < protocomm.sizeof_devproxy_header_t()) || (!IsHeaderValid(ref headerReq))) { headerReply.code = devproxy_opcode_t.PROXY_REP_NACK; headerReply.datalen = 0; stream.Write(arrHeaderReply, 0, arrHeaderReply.Length); } else { // TODO : add try catch Byte[] data = new Byte[headerReq.datalen]; headerReply.datalen = 0; // Execute the peer CMD switch (headerReq.code) { case devproxy_opcode_t.PROXY_CMD_READ: ret = _iusbManager.ReadFromDevice(data, data.Length); if (ret > 0) { headerReply.code = devproxy_opcode_t.PROXY_REP_DONE; headerReply.datalen = (uint)ret; } else { headerReply.code = devproxy_opcode_t.PROXY_REP_ERR; } break; case devproxy_opcode_t.PROXY_CMD_WRITE: // read the data to pass to device ret = stream.Read(data, 0, data.Length); if (ret == data.Length) { // now pass it to the device ret = _iusbManager.WriteToDevice(data); if (ret == 0) { headerReply.code = devproxy_opcode_t.PROXY_REP_DONE; } else { headerReply.code = devproxy_opcode_t.PROXY_REP_ERR; } } else { // Peer has not sent all expected data : reply NACK // We Expect there is no risk to lose sync here headerReply.code = devproxy_opcode_t.PROXY_REP_NACK; } break; default: // Shall not fall here break; } // Return execution to peer stream.Write(arrHeaderReply, 0, arrHeaderReply.Length); // in case of a successful read we must also return data if ((headerReq.code == devproxy_opcode_t.PROXY_CMD_READ) && (headerReply.code == devproxy_opcode_t.PROXY_REP_DONE)) { // We must not sent all data size, as we allocated the max size expected stream.Write(data, 0, (int)headerReply.datalen); } } } // exiting // Shutdown and end connection client.Close(); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(devproxy_header_t obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }