public void DisConnect() { m_Socket.CloseConnection(m_SystemAddress, false); m_Socket.Shutdown(300); RakPeerInterface.DestroyInstance(m_Socket); m_SystemAddress = null; m_Socket = null; OnDisconnected(); }
/// <summary> /// 停止 /// </summary> /// <param name="beforeAction"></param> public void Stop(Action beforeAction = null) { beforeAction?.Invoke(); string myAddress = GetMyAddress().ToString(); isThreadRunning = false; rakPeer.Shutdown(10); RakPeerInterface.DestroyInstance(rakPeer); Console.WriteLine("UdpNATPTServer停止了:{0}", myAddress); }
/// <summary> /// 停止 /// </summary> /// <param name="beforeAction"></param> internal void Stop(Action beforeAction = null) { beforeAction?.Invoke(); string myAddress = GetMyAddress().ToString(); isThreadRunning = false; rakPeer.Shutdown(10); RakPeerInterface.DestroyInstance(rakPeer); RaknetExtension.WriteWarning(string.Format("coordinator停止了:{0}", myAddress)); }
// <summary> /// 停止 /// </summary> /// <param name="beforeAction"></param> internal void Stop(Action beforeAction = null) { beforeAction?.Invoke(); string myAddress = GetMyAddress().ToString(); rakPeer.CloseConnection(new AddressOrGUID(new SystemAddress(_coordinatorAddress.Address, _coordinatorAddress.Port)), true); isThreadRunning = false; rakPeer.Shutdown(10); RakPeerInterface.DestroyInstance(rakPeer); RaknetExtension.WriteWarning(string.Format("UdpProxyServer停止了:{0}", myAddress)); }
public void Dispose() { if (_scheduledTransportLoop != null) { _scheduledTransportLoop.Dispose(); if (_peer != null) { if (_peer.IsActive()) { _peer.Shutdown(1000); } RakPeerInterface.DestroyInstance(_peer); } IsRunning = false; _logger.Info("transports.raknet", "Stopped raknet server."); } }
/// <summary> /// 彻底关掉raknet /// </summary> public void ShutDown() { Debug.LogFormat("=====Client ShutDown 1: {0} IP:{1}", ClientName, ServerIP); if (mClient != null && mClient.IsActive() && !string.IsNullOrEmpty(ServerIP) && !(m_serverAddress == null)) { Debug.LogFormat("DisConnect Server[{0}]===2=>[{1}].", this.ClientName, this.ServerIP); //Debug.Log("mClient.CloseConnection 被调用"); mClient.CloseConnection(m_serverAddress, true); } Debug.LogFormat("=====Client ShutDown 2: {0} IP:{1}", ClientName, ServerIP); if (mClient != null) { //Debug.Log("mClient.Shutdown 被调用"); mClient.Shutdown(300); RakPeerInterface.DestroyInstance(mClient); mClient = null; } IsStartUp = false; IsConnectedServer = false; IsConnecting = false; Debug.LogFormat("=====Client ShutDown 3: {0} IP:{1}", ClientName, ServerIP); }
public bool Connect(string ip, int port, string pwd) { m_Socket = RakPeerInterface.GetInstance(); m_Socket.AllowConnectionResponseIPMigration(false); SocketDescriptor socketDescriptor = new SocketDescriptor(0, "0"); socketDescriptor.socketFamily = 2; m_Socket.Startup(8, socketDescriptor, 1); m_Socket.SetOccasionalPing(true); ConnectionAttemptResult car = m_Socket.Connect(ip, (ushort)port, pwd, pwd.Length); if (car == ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED) { return(true); } else { m_Socket.Shutdown(300); RakPeerInterface.DestroyInstance(m_Socket); return(false); } }
private void Run(CancellationToken token, ushort?serverPort, ushort maxConnections, TaskCompletionSource <bool> startupTcs) { IsRunning = true; logger.Info("Starting raknet transport " + _type); RakPeerInterface server; try { server = RakPeerInterface.GetInstance(); } catch (Exception ex) { logger.Error(ex); throw; } var socketDescriptorList = new RakNetListSocketDescriptor(); //var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); if (Socket.SupportsIPv4) { logger.Info("A valid ipv4 address was found."); var socketDescriptorIpv4 = new SocketDescriptor(serverPort.GetValueOrDefault(), null); socketDescriptorList.Push(socketDescriptorIpv4, null, 0); } else { logger.Info("No valid ipv4 address was found."); } #if (!UNITY_ANDROID) if (Socket.OSSupportsIPv6) { logger.Info("A valid ipv6 address was found."); var socketDescriptorIpv6 = new SocketDescriptor(serverPort.GetValueOrDefault(), null); socketDescriptorIpv6.socketFamily = 23; // AF_INET6 socketDescriptorList.Push(socketDescriptorIpv6, null, 0); } else { logger.Info("No valid ipv6 address was found."); } #endif var startupResult = server.Startup(maxConnections, socketDescriptorList, 1); if (startupResult != StartupResult.RAKNET_STARTED) { logger.Error("Couldn't start raknet peer :" + startupResult); throw new InvalidOperationException("Couldn't start raknet peer :" + startupResult); } server.SetMaximumIncomingConnections(maxConnections); _peer = server; startupTcs.SetResult(true); logger.Info("Raknet transport " + _type + " started"); while (!token.IsCancellationRequested) { for (var packet = server.Receive(); packet != null; packet = server.Receive()) { switch (packet.data[0]) { case (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED: IConnection c; try { c = OnConnection(packet, server); } catch (Exception e) { UnityEngine.Debug.LogException(e); throw; } logger.Debug("Connection request to {0} accepted.", packet.systemAddress.ToString()); var pendingConnection = _pendingConnections.Dequeue(); if (pendingConnection != null) { pendingConnection.Tcs.SetResult(c); logger.Trace("Task for the connection request to {0} completed.", packet.systemAddress.ToString()); } else { logger.Log(Diagnostics.LogLevel.Warn, "RaknetTransport", "Unknown pending connection accepted: '" + packet.systemAddress.ToString() + "'"); } break; case (byte)DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION: logger.Trace("Incoming connection from {0}.", packet.systemAddress.ToString()); OnConnection(packet, server); break; case (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION: logger.Trace("{0} disconnected.", packet.systemAddress.ToString()); OnDisconnection(packet, server, "CLIENT_DISCONNECTED"); break; case (byte)DefaultMessageIDTypes.ID_CONNECTION_LOST: logger.Trace("{0} lost the connection.", packet.systemAddress.ToString()); OnDisconnection(packet, server, "CONNECTION_LOST"); break; case (byte)MessageIDTypes.ID_CONNECTION_RESULT: OnConnectionIdReceived(BitConverter.ToInt64(packet.data, 1)); break; case (byte)DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED: pendingConnection = _pendingConnections.Dequeue(); if (pendingConnection != null) { pendingConnection.Tcs.SetException(new InvalidOperationException("Connection attempt for endpoint " + pendingConnection.Endpoint + " failed.")); } else { logger.Log(Diagnostics.LogLevel.Warn, "RaknetTransport", "Unknown pending connection failed: '" + packet.systemAddress.ToString() + "'"); } break; default: OnMessageReceived(packet); break; } } Thread.Sleep(5); } server.Shutdown(1000); RakPeerInterface.DestroyInstance(server); IsRunning = false; logger.Info("Stopped raknet server."); }
static void Main(string[] args) { int BIG_PACKET_SIZE = 103296250; bool quit; bool sentPacket = false; RakPeerInterface client, server; byte[] text; string message; client = server = null; string ip = string.Empty; text = new byte[BIG_PACKET_SIZE]; quit = false; char ch; Console.WriteLine("Enter 's' to run as server, 'c' to run as client, space to run local."); ch = ' '; message = Console.ReadLine(); ch = message.ToCharArray()[0]; if (ch == 'c') { client = RakPeerInterface.GetInstance(); Console.WriteLine("Working as client"); Console.WriteLine("Enter remote IP: "); ip = Console.ReadLine(); if (ip.Length == 0) { ip = "127.0.0.1"; } } else if (ch == 's') { server = RakPeerInterface.GetInstance(); Console.WriteLine("Working as server"); } else { client = RakPeerInterface.GetInstance(); server = RakPeerInterface.GetInstance(); ip = "127.0.0.1"; } short socketFamily; socketFamily = AF_INET; if (server != null) { server.SetTimeoutTime(5000, RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS); SocketDescriptor socketDescriptor = new SocketDescriptor(3000, "0"); socketDescriptor.socketFamily = socketFamily; server.SetMaximumIncomingConnections(4); StartupResult sr = new StartupResult(); sr = server.Startup(4, socketDescriptor, 1); if (sr != StartupResult.RAKNET_STARTED) { Console.WriteLine("Error: Server failed to start: {0} ", sr.ToString()); return; } // server.SetPerConnectionOutgoingBandwidthLimit(50000); Console.WriteLine("Server started on {0}", server.GetMyBoundAddress().ToString()); } if (client != null) { client.SetTimeoutTime(5000, RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS); SocketDescriptor socketDescriptor = new SocketDescriptor(0, "0"); socketDescriptor.socketFamily = socketFamily; client.SetMaximumIncomingConnections(4); StartupResult sr = new StartupResult(); sr = client.Startup(4, socketDescriptor, 1); if (sr != StartupResult.RAKNET_STARTED) { Console.WriteLine("Error: Server failed to start: " + sr.ToString()); return; } client.SetSplitMessageProgressInterval(10000); // Get ID_DOWNLOAD_PROGRESS notifications client.SetPerConnectionOutgoingBandwidthLimit(10000); Console.WriteLine("Client started on {0}", client.GetMyBoundAddress().ToString()); client.Connect(ip, 3000, null, 0); } System.Threading.Thread.Sleep(500); Console.WriteLine("My IP addresses: "); RakPeerInterface rakPeer; if (server != null) { rakPeer = server; } else { rakPeer = client; } for (uint i = 0; i < rakPeer.GetNumberOfAddresses(); i++) { Console.WriteLine("{0}. {1}", (i + 1).ToString(), rakPeer.GetLocalIP(i).ToString()); } uint start, stop = 0; uint nextStatTime = RakNet.RakNet.GetTimeMS() + 1000; Packet packet = new Packet(); start = RakNet.RakNet.GetTimeMS(); while (!quit) { if (server != null) { for (packet = server.Receive(); packet != null; server.DeallocatePacket(packet), packet = server.Receive()) { if ((DefaultMessageIDTypes)packet.data[0] == DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION || packet.data[0] == (int)253) { Console.WriteLine("Starting send"); start = RakNet.RakNet.GetTimeMS(); if (BIG_PACKET_SIZE < 100000) { for (int i = 0; i < BIG_PACKET_SIZE; i++) { text[i] = (byte)(255 - (i & 255)); } } else { text[0] = (byte)255; } DefaultMessageIDTypes idtype = (DefaultMessageIDTypes)packet.data[0]; if (idtype == DefaultMessageIDTypes.ID_CONNECTION_LOST) { Console.WriteLine("ID_CONNECTION_LOST from {0}", packet.systemAddress.ToString()); } else if (idtype == DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION) { Console.WriteLine("ID_DISCONNECTION_NOTIFICATION from {0}", packet.systemAddress.ToString()); } else if (idtype == DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION) { Console.WriteLine("ID_NEW_INCOMING_CONNECTION from {0}", packet.systemAddress.ToString()); } else if (idtype == DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED) { Console.WriteLine("ID_CONNECTION_REQUEST_ACCEPTED from {0}", packet.systemAddress.ToString()); } server.Send(text, BIG_PACKET_SIZE, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED_WITH_ACK_RECEIPT, (char)0, packet.systemAddress, false); } } if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.Spacebar: Console.WriteLine("Sending medium priority message"); byte[] t = new byte[1]; t[0] = 254; server.Send(t, 1, PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)1, RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS, true); break; case ConsoleKey.Q: quit = true; break; default: break; } } } if (client != null) { packet = client.Receive(); while (packet != null) { DefaultMessageIDTypes idtype = (DefaultMessageIDTypes)packet.data[0]; if (idtype == DefaultMessageIDTypes.ID_DOWNLOAD_PROGRESS) { BitStream progressBS = new BitStream(packet.data, packet.length, false); progressBS.IgnoreBits(8); byte[] progress = new byte[4], total = new byte[4], partlength = new byte[4]; progressBS.ReadBits(progress, sizeof(uint) << 3, true); progressBS.ReadBits(total, sizeof(uint) << 3, true); progressBS.ReadBits(partlength, sizeof(uint) << 3, true); Console.WriteLine("Progress: msgID= {0}, Progress: {1} / {2}, Partsize: {3}", packet.data[0].ToString(), BitConverter.ToUInt32(progress, 0).ToString(), BitConverter.ToUInt32(total, 0).ToString(), BitConverter.ToUInt32(partlength, 0).ToString()); } else if (packet.data[0] == 255) { if (packet.length != BIG_PACKET_SIZE) { Console.WriteLine("Test failed. {0} bytes (wrong number of bytes.", packet.length); quit = true; break; } if (BIG_PACKET_SIZE <= 100000) { for (int i = 0; i < BIG_PACKET_SIZE; i++) { if (packet.data[i] != 255 - (i & 255)) { Console.WriteLine("Test failed. {0} bytes (bad data).", packet.length); quit = true; break; } } } if (quit == false) { Console.WriteLine("Test Succeeded. {0} bytes.", packet.length); bool repeat = false; if (repeat) { Console.WriteLine("Rerequesting send."); byte[] ch2 = new byte[1]; ch2[0] = (byte)253; client.Send(ch2, 1, PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)1, RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS, true); GC.Collect(); } else { quit = true; break; } } } else if ((int)packet.data[0] == 254) { Console.WriteLine("Got high priority message."); } else if ((DefaultMessageIDTypes)packet.data[0] == DefaultMessageIDTypes.ID_CONNECTION_LOST) { Console.WriteLine("ID_CONNECTION_LOST from {0}", packet.systemAddress.ToString()); } else if ((DefaultMessageIDTypes)packet.data[0] == DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION) { Console.WriteLine("ID_NEW_INCOMING_CONNECTION from {0}", packet.systemAddress.ToString()); } else if ((DefaultMessageIDTypes)packet.data[0] == DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED) { start = RakNet.RakNet.GetTimeMS(); Console.WriteLine("ID_CONNECTION_REQUEST_ACCEPTED from {0}", packet.systemAddress.ToString()); } else if ((DefaultMessageIDTypes)packet.data[0] == DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED) { Console.WriteLine("ID_CONNECTION_ATTEMPT_FAILED from {0}", packet.systemAddress.ToString()); } client.DeallocatePacket(packet); packet = client.Receive(); } } uint currenttime = RakNet.RakNet.GetTimeMS(); if (currenttime > nextStatTime) { nextStatTime = RakNet.RakNet.GetTimeMS() + 1000; RakNetStatistics rssSender = new RakNetStatistics(); RakNetStatistics rssReceiver = new RakNetStatistics(); string StatText; if (server != null) { ushort i; ushort numSystems = 1; server.GetConnectionList(null, ref numSystems); if (numSystems > 0) { for (i = 0; i < numSystems; i++) { server.GetStatistics(server.GetSystemAddressFromIndex(i), rssSender); RakNet.RakNet.StatisticsToString(rssSender, out StatText, 2); Console.WriteLine("==== System {0} ====", (i + 1).ToString()); Console.WriteLine("{0}", StatText); } } } if (client != null && server == null && client.GetGUIDFromIndex(0) != RakNet.RakNet.UNASSIGNED_RAKNET_GUID) { client.GetStatistics(client.GetSystemAddressFromIndex(0), rssReceiver); RakNet.RakNet.StatisticsToString(rssReceiver, out StatText, 2); Console.WriteLine("{0}", StatText); } } System.Threading.Thread.Sleep(100); } string StatTextEnd = ""; stop = RakNet.RakNet.GetTimeMS(); double seconds = (double)(stop - start) / 1000.0; if (server != null) { RakNetStatistics rssSender2 = server.GetStatistics(server.GetSystemAddressFromIndex(0)); RakNet.RakNet.StatisticsToString(rssSender2, out StatTextEnd, 2); Console.WriteLine("{0}", StatTextEnd); } Console.WriteLine("{0} bytes per second ({1} seconds). Press enter to quit", (int)((double)(BIG_PACKET_SIZE) / seconds), seconds); RakPeerInterface.DestroyInstance(server); RakPeerInterface.DestroyInstance(client); Console.Read(); }