commFPGA.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 fillBuffer(JobData *sendBuf);
  47. int unassignJob(std::shared_ptr<Job> &job);
  48. uint_least32_t jobCount();
  49. //called by send thread
  50. int sendRaw(uint8_t *buf, uint bufLen);
  51. int sendFromBuffer();
  52. void start();
  53. //called by recv thread
  54. void recvUDP();
  55. int parseRaw(uint32_t *buf, size_t bufLen);
  56. std::unordered_map<uint32_t,std::shared_ptr<Job>>::iterator currentJob;
  57. RecvState recvState = RecvState::checkPreamble;
  58. size_t recvPayloadIndex = 0;
  59. uint_least64_t successCounter = 0;
  60. uint_least64_t failedCounter = 0;
  61. float latency = 0;
  62. private:
  63. //tx buffer for buffered send function
  64. uint32_t sendBuffer[MAX_JOB_LEN];
  65. uint_least32_t sendBufferReadIndex = 0;
  66. uint_least32_t sendBufferWriteIndex = 0;
  67. //list of pending responses
  68. std::unordered_map<uint32_t,std::shared_ptr<Job>> jobList;
  69. uint_least32_t jobsActive = 0;
  70. std::mutex jobLock;
  71. //listener for a single FPGA
  72. sockaddr_storage addrDest = {};
  73. std::future<void> recvResult;
  74. bool running = true;
  75. };
  76. #endif