-
Notifications
You must be signed in to change notification settings - Fork 55
Add network output, based on MAME's network output system #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
njz3
wants to merge
2
commits into
trzy:master
Choose a base branch
from
njz3:dev/net_output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #ifndef _UDP_H_ | ||
| #define _UDP_H_ | ||
| #include <WinDef.h> | ||
|
|
||
| namespace SMUDP | ||
| { | ||
| struct Packet | ||
| { | ||
|
|
||
| static const UINT32 BUFFER_SIZE_UDP = 1460; | ||
|
|
||
| UINT32 crc; | ||
| UINT16 currentID; | ||
| UINT16 totalIDs; | ||
| UINT16 flags; | ||
| UINT16 length; | ||
| UCHAR data[BUFFER_SIZE_UDP]; | ||
|
|
||
| Packet() { | ||
| Init(); | ||
| } | ||
|
|
||
| enum class PacketFlags { | ||
| newConnection = (1 << 0), | ||
| resend = (1 << 1), | ||
| ping = (1 << 2) | ||
| }; | ||
|
|
||
| void Init() { | ||
| crc = 0; | ||
| currentID = 0; | ||
| totalIDs = 0; | ||
| flags = 0; | ||
| length = 0; | ||
| } | ||
|
|
||
| UINT32 CalcCRC() { | ||
| crc = CalcCRCVal(); | ||
| } | ||
|
|
||
| UINT32 CalcCRCVal() { | ||
|
|
||
| UINT32 val = 0; | ||
|
|
||
| for (int i = 0; i < _countof(data); i++) { | ||
| val += data[i]; // crude but will catch the odd off by one error | ||
| } | ||
|
|
||
| return val; | ||
| } | ||
|
|
||
| bool ValidateCRC() { | ||
| return CalcCRCVal() == crc; | ||
| } | ||
|
|
||
| void CreatePacket(PacketFlags p) { | ||
| flags |= (UINT16)p; | ||
| } | ||
|
|
||
| void CalcTotalIDs(int bytes) { | ||
| totalIDs = bytes / sizeof(data); | ||
|
|
||
| if (bytes % sizeof(data)) { | ||
| totalIDs++; | ||
| } | ||
| } | ||
|
|
||
| int HeaderSize() { | ||
| return 12; | ||
| } | ||
|
|
||
| int Size() { | ||
| return HeaderSize() + length; | ||
| } | ||
|
|
||
| operator char*() { return (char*)this; } | ||
| operator const char*() { return (char*)this; } | ||
| }; | ||
|
|
||
| typedef char PacketReply; | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #include <winsock2.h> | ||
| #include <windows.h> | ||
| #include "UDPReceive.h" | ||
| #include "UDPPacket.h" | ||
|
|
||
| namespace SMUDP | ||
| { | ||
|
|
||
| UDPReceive::UDPReceive() | ||
| { | ||
| m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // create the socket | ||
| m_readEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
|
|
||
| WSAEventSelect(m_socket, m_readEvent, FD_READ); | ||
| } | ||
|
|
||
| UDPReceive::~UDPReceive() | ||
| { | ||
| closesocket(m_socket); | ||
| CloseHandle(m_readEvent); | ||
| } | ||
|
|
||
| bool UDPReceive::Bind(UINT16 port) | ||
| { | ||
| //=========================== | ||
| int err; | ||
| SOCKADDR_IN serverInfo = {}; | ||
| //=========================== | ||
|
|
||
| serverInfo.sin_family = AF_INET; // address family Internet | ||
| serverInfo.sin_port = htons(port); // set server�s port number | ||
| serverInfo.sin_addr.s_addr = INADDR_ANY; // set server�s IP | ||
|
|
||
| err = bind(m_socket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); | ||
|
|
||
| return (err == 0); | ||
| } | ||
|
|
||
| std::vector<UINT8>& UDPReceive::ReadData(int timeout) | ||
| { | ||
| m_data.clear(); | ||
|
|
||
| while (true) { | ||
|
|
||
| //======== | ||
| DWORD res; | ||
| //======== | ||
|
|
||
| res = WaitForSingleObject(m_readEvent, timeout); | ||
|
|
||
| if (res == WAIT_OBJECT_0) { | ||
|
|
||
| //========================= | ||
| int result; | ||
| int slen; | ||
| sockaddr_in si_other; | ||
| Packet packet; | ||
| //========================= | ||
|
|
||
| slen = sizeof(sockaddr_in); | ||
|
|
||
| result = recvfrom(m_socket, packet, sizeof(packet), 0, (struct sockaddr *) &si_other, &slen); | ||
|
|
||
| if (result == SOCKET_ERROR) { | ||
| auto error = WSAGetLastError(); // clear error code | ||
| m_data.clear(); | ||
| break; | ||
| } | ||
|
|
||
| // copy data to array | ||
| m_data.insert(m_data.end(), packet.data, packet.data + packet.length); | ||
|
|
||
| PacketReply r = 1; | ||
| result = sendto(m_socket, &r, sizeof(r), 0, (struct sockaddr *)&si_other, sizeof(SOCKADDR_IN)); | ||
|
|
||
| if (packet.currentID + 1 == packet.totalIDs) { | ||
| break; // reached the end | ||
| } | ||
| } | ||
| else { | ||
| m_data.clear(); // reset any memory because we have failed | ||
| break; // timeout | ||
| } | ||
| } | ||
|
|
||
| return m_data; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #ifndef _UDP_RECEIVE_H_ | ||
| #define _UDP_RECEIVE_H_ | ||
|
|
||
| #include "UDPPacket.h" | ||
| #include "WinSockWrap.h" | ||
| #include <vector> | ||
|
|
||
| namespace SMUDP | ||
| { | ||
| class UDPReceive | ||
| { | ||
| public: | ||
| UDPReceive(); | ||
| ~UDPReceive(); | ||
|
|
||
| bool Bind(UINT16 port); | ||
|
|
||
| std::vector<UINT8>& ReadData(int timeout); | ||
|
|
||
| private: | ||
|
|
||
| std::vector<UINT8> m_data; | ||
| SOCKET m_socket; | ||
| HANDLE m_readEvent; | ||
| WinSockWrap m_winSockWrap; | ||
| }; | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| #include <winsock2.h> | ||
| #include <windows.h> | ||
| #include "UDPSend.h" | ||
| #include <WS2tcpip.h> | ||
| #include <stdio.h> | ||
|
|
||
| #ifdef __GNUC__ | ||
| extern "C" { | ||
| WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf); | ||
| } | ||
| #endif | ||
|
|
||
| namespace SMUDP | ||
| { | ||
|
|
||
| UDPSend::UDPSend() | ||
| { | ||
| m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // create the socket | ||
| m_event = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
| m_exitEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
| m_dataReady = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
| m_sendComplete = CreateEvent(NULL, FALSE, TRUE, NULL); // start off ready | ||
|
|
||
| m_sendThread = std::thread(&UDPSend::SendThread, this); | ||
|
|
||
| WSAEventSelect(m_socket, m_event, FD_READ | FD_WRITE); | ||
| } | ||
|
|
||
| UDPSend::~UDPSend() | ||
| { | ||
| SetEvent(m_exitEvent); // trigger thread to exit | ||
| m_sendThread.join(); // block until thread has exited | ||
| CloseHandle(m_exitEvent); // clean up the rest of the resources | ||
| CloseHandle(m_dataReady); | ||
| CloseHandle(m_sendComplete); | ||
|
|
||
| closesocket(m_socket); | ||
| CloseHandle(m_event); | ||
| } | ||
|
|
||
| bool UDPSend::SendAsync(const char* address, int port, int length, const void *data, int timeout) | ||
| { | ||
| WaitForSingleObject(m_sendComplete, INFINITE); // block until previous sends have completed, don't want overlapping | ||
|
|
||
| m_data.clear(); | ||
| m_data.insert(m_data.end(), (UINT8*)data, (UINT8*)data + length); | ||
|
|
||
| m_address = address; | ||
| m_port = port; | ||
| m_timeout = timeout; | ||
|
|
||
| SetEvent(m_dataReady); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void UDPSend::SendThread() | ||
| { | ||
| HANDLE events[2]; | ||
|
|
||
| events[0] = m_exitEvent; | ||
| events[1] = m_dataReady; | ||
|
|
||
| while (true) { | ||
|
|
||
| auto result = WaitForMultipleObjects(_countof(events), events, FALSE, INFINITE); | ||
|
|
||
| if (result == WAIT_OBJECT_0) { // exit event triggered | ||
| break; | ||
| } | ||
| else if (result == (WAIT_OBJECT_0 + 1)) { // data ready | ||
| Send(m_address.c_str(), m_port, (int)m_data.size(), m_data.data(), m_timeout); | ||
| SetEvent(m_sendComplete); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool UDPSend::Send(const char* ip, int port, int length, const void *data, int timeout) | ||
| { | ||
| UINT8* pData = (UINT8*)data; | ||
|
|
||
| Packet packet; | ||
| packet.CalcTotalIDs(length); | ||
|
|
||
| SOCKADDR_IN address = {}; | ||
| address.sin_family = AF_INET; // address family Internet | ||
| address.sin_port = htons(port); // set server�s port number | ||
| inet_pton(AF_INET, ip, &address.sin_addr); | ||
|
|
||
| while (length > 0) { | ||
|
|
||
| packet.flags = 0; // reset the flags (not used currently) | ||
|
|
||
| if (length > packet.BUFFER_SIZE_UDP) { | ||
| packet.length = packet.BUFFER_SIZE_UDP; | ||
| } | ||
| else { | ||
| packet.length = length; | ||
| } | ||
|
|
||
| memcpy(packet.data, pData, packet.length); | ||
|
|
||
| int sent = SendDataPacket(packet, m_socket, address, m_event); | ||
|
|
||
| if (sent == SOCKET_ERROR) { | ||
| return false; // send failure | ||
| } | ||
|
|
||
| if (!ProcessReply(m_socket, m_event, timeout)) { | ||
| return false; // reply failure | ||
| } | ||
|
|
||
| length -= packet.length; | ||
| pData += packet.length; | ||
|
|
||
| packet.currentID++; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool UDPSend::WaitForEvent(SOCKET s, HANDLE hEvent, long nEvents, int timeout) | ||
| { | ||
| //======== | ||
| DWORD res; | ||
| //======== | ||
|
|
||
| res = WaitForSingleObject(hEvent, timeout); | ||
|
|
||
| if (res == WAIT_OBJECT_0) { | ||
|
|
||
| WSANETWORKEVENTS events = {}; | ||
| WSAEnumNetworkEvents(s, hEvent, &events); | ||
|
|
||
| if (events.lNetworkEvents & nEvents) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| int UDPSend::SendDataPacket(Packet &p, SOCKET s, SOCKADDR_IN& address, HANDLE events) | ||
| { | ||
| while (true) { | ||
|
|
||
| int sent = sendto(s, p, p.Size(), 0, (struct sockaddr *)&address, sizeof(SOCKADDR_IN)); | ||
|
|
||
| if (sent == SOCKET_ERROR) { | ||
|
|
||
| int error = WSAGetLastError(); // clear error code | ||
|
|
||
| if (error == WSAEWOULDBLOCK) { | ||
| WaitForEvent(s, events, FD_WRITE, INFINITE); // wait until write event is triggered | ||
| continue; | ||
| } | ||
|
|
||
| return sent; // send failure | ||
| } | ||
|
|
||
| return p.Size(); | ||
| } | ||
| } | ||
|
|
||
| bool UDPSend::ProcessReply(SOCKET s, HANDLE event, int timeout) | ||
| { | ||
| //================= | ||
| int result; | ||
| PacketReply rp; | ||
| //================= | ||
|
|
||
| if (WaitForEvent(s, event, FD_READ, timeout)) { | ||
|
|
||
| result = recv(s, &rp, sizeof(rp), 0); | ||
|
|
||
| if (result == SOCKET_ERROR) { | ||
| auto error = WSAGetLastError(); // clear error code | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we convert tabs to spaces everywhere? I think we are using a 4-character tab stop.