void BeginResponseCallBack(IAsyncResult ar) { UdpPeer peer = (UdpPeer)ar.AsyncState; int rev = peer.serverSocket.EndReceiveFrom(ar, ref peer.remoteEndPoint); if (rev > 0) { if (ResponseCallback != null) { byte[] result = ResponseCallback(peer.remoteEndPoint, peer.buffer); if (result != null && result.Length > 0) { SendTo(peer.remoteEndPoint, result); } peer.ResetBuffer(); } socketHandler.BeginReceiveFrom (peer.buffer, 0, bufferSize, SocketFlags.None, ref peer.remoteEndPoint, new AsyncCallback(BeginResponseCallBack), peer); } }
public void runServer() { if (socketHandler == null) { Logger.LogError("IP Type Error at UDPServer Init"); return; } UdpPeer peerInit = new UdpPeer(socketHandler, bufferSize, IPType); socketHandler.BeginReceiveFrom (peerInit.buffer, 0, bufferSize, SocketFlags.None, ref peerInit.remoteEndPoint, new AsyncCallback(BeginResponseCallBack), peerInit); }
void BeginResponseCallBack(IAsyncResult ar) { UdpPeer peer = (UdpPeer)ar.AsyncState; int rev = peer.serverSocket.EndReceiveFrom(ar, ref peer.remoteEndPoint); if (rev > 0) { if (ResponseCallback != null) { ThreadBridge.Invoke(() => { ResponseCallback(peer.remoteEndPoint, peer.buffer); }); } peer.ResetBuffer(); socketHandler.BeginReceiveFrom (peer.buffer, 0, bufferSize, SocketFlags.None, ref peer.remoteEndPoint, new AsyncCallback(BeginResponseCallBack), peer); } }