public SendTo ( byte data, int offset, int size, |
||
data | byte | |
offset | int | |
size | int | |
remoteEndPoint | ||
errorCode | int | |
return | int |
public void NatIntroduce( NetEndPoint hostInternal, NetEndPoint hostExternal, NetEndPoint clientInternal, NetEndPoint clientExternal, string additionalInfo) { NetDataWriter dw = new NetDataWriter(); //First packet (server) //send to client dw.Put(ClientByte); dw.Put(hostInternal); dw.Put(hostExternal); dw.Put(additionalInfo, MaxTokenLength); _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatIntroduction, dw), clientExternal); //Second packet (client) //send to server dw.Reset(); dw.Put(HostByte); dw.Put(clientInternal); dw.Put(clientExternal); dw.Put(additionalInfo, MaxTokenLength); _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatIntroduction, dw), hostExternal); }
private void Send <T>(T packet, IPEndPoint target) where T : class, new() { SocketError errorCode = 0; _cacheWriter.Reset(); _cacheWriter.Put((byte)PacketProperty.NatMessage); _netPacketProcessor.Write(_cacheWriter, packet); _socket.SendTo(_cacheWriter.Data, 0, _cacheWriter.Length, target, ref errorCode); }
public void NatIntroduce( IPEndPoint hostInternal, IPEndPoint hostExternal, IPEndPoint hostInternet, IPEndPoint clientInternal, IPEndPoint clientExternal, IPEndPoint clientInternet, 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); dw.Put(hostInternet); SocketError 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); dw.Put(clientInternet); _socket.SendTo(dw.Data, 0, dw.Length, hostExternal, ref errorCode); }
/// <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 = new NetEndPoint(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(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); } }
internal bool SendRaw(byte[] message, int start, int length, NetEndPoint remoteEndPoint) { if (!_running) { return(false); } int errorCode = 0; bool result = _socket.SendTo(message, start, length, remoteEndPoint, ref errorCode) > 0; //10040 message to long... need to check //10065 no route to host if (errorCode != 0 && errorCode != 10040 && errorCode != 10065) { ProcessSendError(remoteEndPoint, errorCode); return(false); } if (errorCode == 10040) { NetUtils.DebugWrite(ConsoleColor.Red, "[SRD] 10040, datalen: {0}", length); return(false); } #if STATS_ENABLED PacketsSent++; BytesSent += (uint)length; #endif return(result); }
public void GetNetworkTime() { if (SyncedTime != null) { return; } var ntpData = new byte[48]; //LeapIndicator = 0 (no warning) //VersionNum = 3 //Mode = 3 (Client Mode) ntpData[0] = 0x1B; //send _socket.SendTo(ntpData, _ntpEndPoint); //receive NetEndPoint endPoint = new NetEndPoint(ConnectionAddressType.IPv4, 0); int errorCode = 0; if (_socket.ReceiveFrom(ref ntpData, ref endPoint, ref errorCode) > 0 && endPoint.Equals(_ntpEndPoint)) { ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | (ulong)ntpData[43]; ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | (ulong)ntpData[47]; var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L); SyncedTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds); } }
/// <summary> /// Send message without connection /// </summary> /// <param name="message">Raw data</param> /// <param name="start">data start</param> /// <param name="length">data length</param> /// <param name="remoteEndPoint">Packet destination</param> /// <returns>Operation result</returns> public bool SendUnconnectedMessage(byte[] message, int start, int length, NetEndPoint remoteEndPoint) { if (!_running) { return(false); } NetPacket p = new NetPacket(); p.Init(PacketProperty.UnconnectedMessage, length); p.PutData(message, start, length); return(_socket.SendTo(p.RawData, remoteEndPoint) > 0); }
internal bool SendRawData(byte[] data) { int errorCode = 0; if (_socket.SendTo(data, _remoteEndPoint, ref errorCode) == -1) { if (errorCode != 0) { _peerListener.ProcessSendError(_remoteEndPoint, errorCode.ToString()); } return(false); } return(true); }
internal bool SendRaw(byte[] message, int start, int length, IPEndPoint remoteEndPoint) { if (!IsRunning) { return(false); } SocketError errorCode = 0; if (_socket.SendTo(message, start, length, remoteEndPoint, ref errorCode) <= 0) { return(false); } NetPeer fromPeer; switch (errorCode) { case SocketError.MessageSize: NetUtils.DebugWrite(ConsoleColor.Red, "[SRD] 10040, datalen: {0}", length); return(false); case SocketError.HostUnreachable: if (_peers.TryGetValue(remoteEndPoint, out fromPeer)) { DisconnectPeer(fromPeer, DisconnectReason.SocketSendError, errorCode, true, null, 0, 0); } CreateEvent(NetEvent.EType.Error, remoteEndPoint: remoteEndPoint, errorCode: errorCode); return(false); case SocketError.ConnectionReset: //connection reset (connection closed) if (_peers.TryGetValue(remoteEndPoint, out fromPeer)) { DisconnectPeer(fromPeer, DisconnectReason.RemoteConnectionClose, errorCode, true, null, 0, 0); } return(false); } #if STATS_ENABLED Statistics.PacketsSent++; Statistics.BytesSent += (uint)length; #endif return(true); }
internal bool SendRaw(byte[] message, int start, int length, NetEndPoint 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 NetPeer fromPeer; if (_peers.TryGetValue(remoteEndPoint, out 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); }
private NtpRequest(IPEndPoint endPoint, Action <DateTime?> onRequestComplete) { _ntpEndPoint = endPoint; _onRequestComplete = onRequestComplete; //Create and start socket _socket = new NetSocket(this); _socket.Bind(IPAddress.Any, IPAddress.IPv6Any, 0, false); //Send request SocketError 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 GetNetworkTime() { if (SyncedTime != null) { return; } var ntpData = new byte[48]; //LeapIndicator = 0 (no warning) //VersionNum = 3 //Mode = 3 (Client Mode) ntpData[0] = 0x1B; //send int errorCode = 0; _socket.SendTo(ntpData, 0, ntpData.Length, _ntpEndPoint, ref errorCode); if (errorCode == 0) { _waiter.WaitOne(1000); } }