1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CONTRIB_IGNITE_KERNELS_CLIENT_IGNITE_CLIENT_H_ 17 #define TENSORFLOW_CONTRIB_IGNITE_KERNELS_CLIENT_IGNITE_CLIENT_H_ 18 19 #include "tensorflow/contrib/ignite/kernels/client/ignite_byte_swapper.h" 20 #include "tensorflow/core/lib/core/errors.h" 21 #include "tensorflow/core/lib/core/status.h" 22 23 namespace tensorflow { 24 25 class Client { 26 public: Client(bool big_endian)27 Client(bool big_endian) : byte_swapper_(ByteSwapper(big_endian)) {} 28 virtual Status Connect() = 0; 29 virtual Status Disconnect() = 0; 30 virtual bool IsConnected() = 0; 31 virtual int GetSocketDescriptor() = 0; 32 virtual Status ReadData(uint8_t *buf, const int32_t length) = 0; 33 virtual Status WriteData(const uint8_t *buf, const int32_t length) = 0; 34 ReadByte(uint8_t * data)35 Status ReadByte(uint8_t *data) { return ReadData(data, 1); } 36 ReadShort(int16_t * data)37 Status ReadShort(int16_t *data) { 38 TF_RETURN_IF_ERROR(ReadData((uint8_t *)data, 2)); 39 byte_swapper_.SwapIfRequiredInt16(data); 40 41 return Status::OK(); 42 } 43 ReadInt(int32_t * data)44 Status ReadInt(int32_t *data) { 45 TF_RETURN_IF_ERROR(ReadData((uint8_t *)data, 4)); 46 byte_swapper_.SwapIfRequiredInt32(data); 47 48 return Status::OK(); 49 } 50 ReadLong(int64_t * data)51 Status ReadLong(int64_t *data) { 52 TF_RETURN_IF_ERROR(ReadData((uint8_t *)data, 8)); 53 byte_swapper_.SwapIfRequiredInt64(data); 54 55 return Status::OK(); 56 } 57 WriteByte(const uint8_t data)58 Status WriteByte(const uint8_t data) { return WriteData(&data, 1); } 59 WriteShort(const int16_t data)60 Status WriteShort(const int16_t data) { 61 int16_t tmp = data; 62 byte_swapper_.SwapIfRequiredInt16(&tmp); 63 return WriteData((uint8_t *)&tmp, 2); 64 } 65 WriteInt(const int32_t data)66 Status WriteInt(const int32_t data) { 67 int32_t tmp = data; 68 byte_swapper_.SwapIfRequiredInt32(&tmp); 69 return WriteData((uint8_t *)&tmp, 4); 70 } 71 WriteLong(const int64_t data)72 Status WriteLong(const int64_t data) { 73 int64_t tmp = data; 74 byte_swapper_.SwapIfRequiredInt64(&tmp); 75 return WriteData((uint8_t *)&tmp, 8); 76 } 77 78 private: 79 const ByteSwapper byte_swapper_; 80 }; 81 82 } // namespace tensorflow 83 84 #endif // TENSORFLOW_CONTRIB_IGNITE_KERNELS_CLIENT_IGNITE_CLIENT_H_ 85