private void Awake() { //UDP Client shutdownNetwork();//just incase there was some sort of error last time Networking.initNetwork(); endp = createIPEndpointData("10.150.32.203", 8888); sock = initSocketData(); if (createSocket(ref sock, SocketType.UDP) == P_GenericError) { Debug.LogError(getLastNetworkError()); } if (connectEndpoint(ref endp, ref sock) == P_GenericError) { Debug.LogError(getLastNetworkError()); } jobClient = new NetworkClientJob() { sock = this.sock, endp = this.endp }; hnd = jobClient.Schedule();//schedules the job to start asynchronously like std::detach in c++ }
///<summary> ///Send packet over UDP server. Not guaranteed to send all bytes. ///</summary> public static PResult sendToPacket(ref SocketData soc, ref string data, ref IPEndpointData ip) { IntPtr tmp = Marshal.StringToHGlobalAnsi(data); PResult res = sendToPacketData(ref soc, tmp, data.Length + 1, ref ip); return(res); }
public void connectToInternet() { string serverAddr = ipText.text; string userName = nameText.text; thisUser._name = userName; // Create UDP Client shutdownNetwork(); //just incase there was some sort of error last time Networking.initNetwork(); endp = createIPEndpointData(serverAddr, 8888); sock = initSocketData(); if (createSocket(ref sock, SocketType.UDP) == P_GenericError) { Debug.LogError(getLastNetworkError()); } if (connectEndpoint(ref endp, ref sock) == P_GenericError) { Debug.LogError(getLastNetworkError()); } jobClient = new NetworkClientJob() { sock = sock, endp = endp }; hnd = jobClient.Schedule(); //schedules the job to start asynchronously like std::detach in c++ string tmp = "@" + userName; int dump = 0; if (sendToPacket(ref sock, ref tmp, ref dump, ref endp) == P_GenericError) { Debug.LogError(getLastNetworkError()); } }
///<summary> ///Send packet over UDP server. Not guaranteed to send all bytes. ///</summary> public static PResult sendToPacket <T>(ref SocketData soc, ref T data, int numberOfBytes, ref IPEndpointData ip) { IntPtr tmp = Marshal.AllocHGlobal(Marshal.SizeOf <T>(data)); Marshal.StructureToPtr(data, tmp, true); PResult res = sendToPacketData(ref soc, tmp, numberOfBytes, ref ip); Marshal.FreeHGlobal(tmp); return(res); }
[DllImport(DLL)] private static extern PResult sendToPacketData(ref SocketData soc, IntPtr data, int numberOfBytes, ref IPEndpointData ip);
///<summary> ///Receive packet over UDP server. Not guaranteed to recieve all bytes. ///</summary> public static PResult recvFromPacket(ref SocketData soc, ref string data, int numberOfBytes, ref IPEndpointData ip) { IntPtr tmp = Marshal.AllocHGlobal(numberOfBytes); PResult res = recvFromPacketData(ref soc, tmp, numberOfBytes, ref ip); data = Marshal.PtrToStringAnsi(tmp); Marshal.FreeHGlobal(tmp); return(res); }
///<summary> ///Receive packet over UDP server. Not guaranteed to recieve all bytes. ///</summary> public static PResult recvFromPacket <T>(ref SocketData soc, ref T data, int numberOfBytes, ref IPEndpointData ip) { IntPtr tmp = Marshal.AllocHGlobal(Marshal.SizeOf <T>(data)); Marshal.StructureToPtr(data, tmp, true); PResult res = recvFromPacketData(ref soc, tmp, numberOfBytes, ref ip); if (res == PResult.P_GenericError) { data = Marshal.PtrToStructure <T>(tmp); } Marshal.FreeHGlobal(tmp); return(res); }
///<summary> ///Receive packet over UDP server. Not guaranteed to recieve all bytes. ///</summary> public static PResult recvFromPacket(ref SocketData soc, ref string data, int numberOfBytes, ref int bytesSent, ref IPEndpointData ip) { IntPtr tmp = Marshal.AllocHGlobal(numberOfBytes);//allocates to unmanaged memory PResult res = recvFromPacketDataEx(ref soc, tmp, numberOfBytes, ref bytesSent, ref ip); data = Marshal.PtrToStringAnsi(tmp, numberOfBytes); Marshal.FreeHGlobal(tmp); return(res); }
//UDP [DllImport(DLL)] private static extern PResult recvFromPacketDataEx(ref SocketData soc, IntPtr data, int numberOfBytes, ref int bytesSent, ref IPEndpointData ip);
///<summary> ///Connects endpoint to socket ///</summary> [DllImport(DLL)] public static extern PResult connectEndpoint(ref IPEndpointData ip, ref SocketData soc);
///<summary> ///Listens for endpoint connection to the socket. It will block until a new connection is found. ///</summary> [DllImport(DLL)] public static extern PResult listenEndpointToSocket(ref IPEndpointData ip, ref SocketData soc, int backlog = 5);
///<summary> ///Bind Endpoint to socket. ///</summary> [DllImport(DLL)] public static extern PResult bindEndpointToSocket(ref IPEndpointData ip, ref SocketData soc);
///<summary> ///Send packet over UDP server. Not guaranteed to send all bytes. ///</summary> public static PResult sendToPacket(ref SocketData soc, ref string data, int numberOfBytes, ref int bytesSent, ref IPEndpointData ip) { IntPtr tmp = Marshal.StringToHGlobalAnsi(data); PResult res = sendToPacketDataEx(ref soc, tmp, numberOfBytes, ref bytesSent, ref ip); return(res); }