/// <summary> /// Request time from NTP server and calls callback (if success) /// </summary> /// <param name="ntpServerAddress">NTP Server address</param> /// <param name="port">port</param> /// <param name="onRequestComplete">callback (called from other thread!)</param> public static void RequestTimeFromNTP(string ntpServerAddress, int port, Action <DateTime?> onRequestComplete) { NetSocket socket = null; var ntpEndPoint = MakeEndPoint(ntpServerAddress, port); NetManager.OnMessageReceived onReceive = (data, length, code, point) => { if (!point.Equals(ntpEndPoint) || length < 48) { return; } socket.Close(); ulong intPart = (ulong)data[40] << 24 | (ulong)data[41] << 16 | (ulong)data[42] << 8 | (ulong)data[43]; ulong fractPart = (ulong)data[44] << 24 | (ulong)data[45] << 16 | (ulong)data[46] << 8 | (ulong)data[47]; var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L); onRequestComplete(new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)milliseconds)); }; //Create and start socket socket = new NetSocket(onReceive); socket.Bind(IPAddress.Any, IPAddress.IPv6Any, 0, false); //Send request int errorCode = 0; var sendData = new byte[48]; sendData[0] = 0x1B; var sendCount = socket.SendTo(sendData, 0, sendData.Length, ntpEndPoint, ref errorCode); if (errorCode != 0 || sendCount != sendData.Length) { onRequestComplete(null); } }
public void NatIntroduce( IPEndPoint hostInternal, IPEndPoint hostExternal, IPEndPoint clientInternal, IPEndPoint clientExternal, string additionalInfo) { NetDataWriter dw = new NetDataWriter(); //First packet (server) //send to client dw.Put((byte)PacketProperty.NatIntroduction); dw.Put(ClientByte); dw.Put(hostInternal); dw.Put(hostExternal); dw.Put(additionalInfo, MaxTokenLength); int errorCode = 0; _socket.SendTo(dw.Data, 0, dw.Length, clientExternal, ref errorCode); //Second packet (client) //send to server dw.Reset(); dw.Put((byte)PacketProperty.NatIntroduction); dw.Put(HostByte); dw.Put(clientInternal); dw.Put(clientExternal); dw.Put(additionalInfo, MaxTokenLength); _socket.SendTo(dw.Data, 0, dw.Length, hostExternal, ref errorCode); }
internal bool SendRaw(byte[] message, int start, int length, IPEndPoint remoteEndPoint) { if (!IsRunning) { return(false); } int errorCode = 0; if (_socket.SendTo(message, start, length, remoteEndPoint, ref errorCode) <= 0) { return(false); } //10040 message to long... need to check //10065 no route to host if (errorCode == 10040) { NetUtils.DebugWrite(ConsoleColor.Red, "[SRD] 10040, datalen: {0}", length); return(false); } if (errorCode != 0 && errorCode != 10065) { //Send error if (_peers.TryGetValue(remoteEndPoint, out var fromPeer)) { DisconnectPeer(fromPeer, DisconnectReason.SocketSendError, errorCode, true, null, 0, 0); } var netEvent = CreateEvent(NetEventType.Error); netEvent.RemoteEndPoint = remoteEndPoint; netEvent.AdditionalData = errorCode; EnqueueEvent(netEvent); return(false); } #if STATS_ENABLED Statistics.PacketsSent++; Statistics.BytesSent += (uint)length; #endif return(true); }