commFPGA.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef myUDP_H
  2. #define myUDP_H
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <mutex>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include <chrono>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #include <sys/socket.h>
  13. #include <stdlib.h>
  14. #include <netdb.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/in.h>
  17. #include <thread>
  18. #include <future>
  19. #include <string.h>
  20. #include "job.hpp"
  21. #include "jobList.hpp"
  22. #include "modules.hpp"
  23. #define UDP_LEN (1500-28-448) // size of sent UDP packets in bytes
  24. #define UDP_MTU (1500) // size of recv UDP buffer in bytes
  25. #define JOB_COUNT (1024 * 4)
  26. #define DEBUG_JOB_RESP
  27. typedef std::chrono::high_resolution_clock Clock;
  28. typedef std::chrono::milliseconds milliseconds;
  29. typedef std::chrono::microseconds microseconds;
  30. enum class RecvState {
  31. checkPreamble,
  32. checkJobId,
  33. checkModuleId,
  34. writePayload
  35. };
  36. //using jobCb_t = void(*)(commFPGA *, jobResponse *);
  37. class commFPGA {
  38. public:
  39. commFPGA(const char *host, uint _port = 1234, bool bindSelf = false);
  40. ~commFPGA();
  41. char ip[16];
  42. uint port;
  43. int sock;
  44. //called by worker thread
  45. int assignJob(std::shared_ptr<Job> &job);
  46. int unassignJob(std::shared_ptr<Job> &job);
  47. size_t jobCount();
  48. //called by send thread
  49. int sendRaw(uint8_t *buf, uint bufLen);
  50. int sendFromBuffer();
  51. void start();
  52. //called by recv thread
  53. void recvUDP();
  54. int parseRaw(uint32_t *buf, size_t bufLen);
  55. std::unordered_map<uint32_t,std::shared_ptr<Job>>::iterator currentJob;
  56. RecvState recvState = RecvState::checkPreamble;
  57. size_t recvPayloadIndex = 0;
  58. uint_least64_t successCounter = 0;
  59. uint_least64_t failedCounter = 0;
  60. float latency = 0;
  61. private:
  62. //tx buffer for buffered send function
  63. uint32_t sendBuffer[MAX_JOB_LEN];
  64. uint_least32_t sendBufferReadIndex = 0;
  65. uint_least32_t sendBufferWriteIndex = 0;
  66. //list of pending responses
  67. std::unordered_map<uint32_t,std::shared_ptr<Job>> jobList;
  68. std::mutex jobLock;
  69. //listener for a single FPGA
  70. sockaddr_storage addrDest = {};
  71. std::future<void> recvResult;
  72. bool running = true;
  73. };
  74. #endif