commFPGA.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 * 10) // max size of jobList
  26. #define MAX_JOB_LEN (256*256*2) // max word count of job
  27. //#define DEBUG_JOB_RESP
  28. //#define DEBUG_JOB_SEND
  29. typedef std::chrono::high_resolution_clock Clock;
  30. typedef std::chrono::milliseconds milliseconds;
  31. typedef std::chrono::microseconds microseconds;
  32. enum class RecvState {
  33. checkPreamble,
  34. checkJobId,
  35. checkModuleId,
  36. writePayload
  37. };
  38. //using jobCb_t = void(*)(commFPGA *, jobResponse *);
  39. class commFPGA {
  40. public:
  41. commFPGA(const char *host, uint _port = 1234, bool bindSelf = false);
  42. ~commFPGA();
  43. char ip[16];
  44. uint port;
  45. int sock;
  46. //called by worker thread
  47. int assignJob(JobContainer &job);
  48. int unassignJob(JobContainer &job);
  49. size_t jobCount();
  50. //called by send thread
  51. int sendRaw(uint8_t *buf, uint bufLen);
  52. int sendFromBuffer();
  53. void start();
  54. //called by recv thread
  55. void recvUDP();
  56. int parseRaw(uint32_t *buf, int_least32_t bufLen);
  57. std::shared_ptr<Job> currentJob;
  58. RecvState recvState = RecvState::checkPreamble;
  59. size_t recvPayloadIndex = 0;
  60. uint_least64_t successCounter = 0;
  61. uint_least64_t failedCounter = 0;
  62. float latency = 0;
  63. private:
  64. //tx buffer for buffered send function
  65. uint32_t *sendBuffer;
  66. int_least32_t sendBufferReadIndex = 0;
  67. int_least32_t sendBufferAvailable = 0;
  68. std::mutex sendLock;
  69. //list of pending responses
  70. std::unordered_map<uint32_t,std::shared_ptr<Job>> jobList;
  71. std::mutex jobLock;
  72. sockaddr_storage addrDest = {};
  73. std::future<void> recvResult;
  74. bool running = true;
  75. };
  76. #endif