public static RakPeerInterface GetInstance()
        {
            IntPtr           cPtr = RakNetPINVOKE.RakPeerInterface_GetInstance();
            RakPeerInterface ret  = (cPtr == IntPtr.Zero) ? null : new RakPeerInterface(cPtr, false);

            return(ret);
        }
        public RakPeerInterface GetRakPeerInterface()
        {
            IntPtr           cPtr = RakNetPINVOKE.PluginInterface2_GetRakPeerInterface(swigCPtr);
            RakPeerInterface ret  = (cPtr == IntPtr.Zero) ? null : new RakPeerInterface(cPtr, false);

            return(ret);
        }
 public void Send(FileList fileList, RakPeerInterface rakPeer, SystemAddress recipient, ushort setID, PacketPriority priority, char orderingChannel)
 {
     RakNetPINVOKE.FileListTransfer_Send__SWIG_2(swigCPtr, FileList.getCPtr(fileList), RakPeerInterface.getCPtr(rakPeer), SystemAddress.getCPtr(recipient), setID, (int)priority, orderingChannel);
     if (RakNetPINVOKE.SWIGPendingException.Pending)
     {
         throw RakNetPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #4
0
 public void Send(FileList fileList, RakPeerInterface rakPeer, SystemAddress recipient, ushort setID, PacketPriority priority, char orderingChannel, IncrementalReadInterface _incrementalReadInterface, uint _chunkSize)
 {
     RakNetPINVOKE.CSharp_FileListTransfer_Send__SWIG_0(swigCPtr, FileList.getCPtr(fileList), RakPeerInterface.getCPtr(rakPeer), SystemAddress.getCPtr(recipient), setID, (int)priority, orderingChannel, IncrementalReadInterface.getCPtr(_incrementalReadInterface), _chunkSize);
     if (RakNetPINVOKE.SWIGPendingException.Pending)
     {
         throw RakNetPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #5
0
    private void sendNewClientID(RakNet.RakPeerInterface rakPeerInterface, ref RakNet.SystemAddress address)
    {
        RakNet.BitStream bitstream;
        bitstream = new RakNet.BitStream();
        bitstream.Write(nextClientID);
        nextClientID++;

        rakPeerInterface.Send(bitstream, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, address, false);
    }
 void sendClientPing(RakNet.RakPeerInterface rakPeerInterface)
 {
     while (true)
     {
         RakNet.BitStream bitStream;
         //bitStream.Write((RakNet.DefaultMessageIDTypes) ENUMGAMEMESSAGES.ID_SERVER_TEXT_MESSAGE);
         //bitStream.Write("Ping!");
     }
 }
    void sendClientPing(RakNet.RakPeerInterface rakPeerInterface)
    {
        while (true)
        {
            //Use unity console instead.
            RakNet.BitStream bitStream;
            bitStream = new RakNet.BitStream();
            bitStream.Write((char)ENUMGAMEMESSAGES.ID_SERVER_TEXT_MESSAGE);
            bitStream.Write("Ping!");

            rakPeerInterface.Send(bitStream, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0,
                                  RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS, true);
            Thread.Sleep(1000);
        }
    }
Beispiel #8
0
        public void InitPeer()
        {
            if (_peer != null)
            {
                return;
            }
            try
            {
                _peer = RakNet.RakPeerInterface.GetInstance();

                _State = RakNetClientState.PeerInitOK;
            }
            catch
            {
                _State = RakNetClientState.PeerInitError;
            }
        }
Beispiel #9
0
    private void handleNetworkMessages(RakNet.RakPeerInterface rakPeerInterface)
    {
        RakNet.Packet packet;
        packet = null;

        while (true)
        {
            for (packet = rakPeerInterface.Receive(); packet != null;
                 rakPeerInterface.DeallocatePacket(packet),
                 packet = rakPeerInterface.Receive())
            {
                switch (packet.data[0])
                {
                case (int)RakNet.DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION:
                    Debug.Log("A connection is incoming.");
                    SystemAddress sa = packet.systemAddress;
                    sendNewClientID(rakPeerInterface, ref sa);
                    packet.systemAddress = sa;
                    break;

                case (int)RakNet.DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                    Debug.Log("A client has disconnected.");
                    break;

                case (int)RakNet.DefaultMessageIDTypes.ID_CONNECTION_LOST:
                    Debug.Log("A client has lost the connection.");
                    break;

                case (int)GameMessages.ENUMGAMEMESSAGES.ID_CLIENT_CLIENT_DATA:
                {
                    RakNet.BitStream bitStream;
                    bitStream = new RakNet.BitStream(packet.data, packet.length, false);
                    rakPeerInterface.Send(bitStream, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED,
                                          (char)0, packet.systemAddress, true);
                    break;
                }

                default:
                    Debug.Log("Recieved a message with unknown id:");
                    Debug.Log(packet.data[0]);
                    break;
                }
            }
        }
    }
Beispiel #10
0
        void handleNetworkMessages(RakNet.RakPeerInterface rakPeerInterface)
        {
            RakNet.Packet packet;
            packet = null;

            while (true)
            {
                for (packet = rakPeerInterface.Receive(); packet != null; rakPeerInterface.DeallocatePacket(packet), packet = rakPeerInterface.Receive())
                {
                    switch (packet.data[0])
                    {
                    case (int)RakNet.DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION:

                        break;
                    }
                }
            }
        }
Beispiel #11
0
 /// <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);
 }
Beispiel #12
0
        /// <summary>
        /// 启动是否成功
        /// </summary>
        /// <returns></returns>
        public bool StartUpRaknet(bool isIPV6)
        {
            if (IsStartUp == false)
            {
                if (mClient == null)
                {
                    mClient = RakPeerInterface.GetInstance();
                }

                SocketDescriptor descriptor = new SocketDescriptor();
                //descriptor.port = 0;
                if (isIPV6 == true)
                {
                    // 这里有尼玛个天坑,AF_INET6 這个宏在windows下的值是23,在 mac osx下的值是30
                    descriptor.socketFamily = 30;
                }
                else
                {
                    descriptor.socketFamily = 2;
                }

                StartupResult result = mClient.Startup(1, descriptor, 1);
                if (result == StartupResult.RAKNET_STARTED)
                {
                    IsStartUp = true;
                    return(true);
                }
                else
                {
                    Debug.LogError(string.Format("初始化raknet失败,result = {0}", result));
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
        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 socketDescriptor = serverPort.HasValue ? new SocketDescriptor(serverPort.Value, null) : new SocketDescriptor();
            var startupResult = server.Startup(maxConnections, socketDescriptor, 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:
                            TaskCompletionSource<IConnection> tcs;
                            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());

                            if (_pendingConnections.TryGetValue(packet.systemAddress.ToString(), out tcs))
                            {
                               
                                tcs.SetResult(c);
                                logger.Trace("Task for the connection request to {0} completed.", 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:
                            if (_pendingConnections.TryGetValue(packet.systemAddress.ToString(), out tcs))
                            {
                                tcs.SetException(new InvalidOperationException("Connection attempt failed."));
                            }
                            break;

                        default:
                            OnMessageReceived(packet);
                            break;
                    }
                }
                Thread.Sleep(5);
            }
            server.Shutdown(1000);
            IsRunning = false;
            logger.Info("Stopped raknet server.");
        }
        private RakNetConnection CreateNewConnection(RakNetGUID raknetGuid, RakPeerInterface peer)
        {
            var cid = _handler.GenerateNewConnectionId();
            var c = new RakNetConnection(raknetGuid, cid, peer, OnRequestClose);
            _connections.TryAdd(raknetGuid.g, c);
            return c;

        }
        private void OnDisconnection(RakNet.Packet packet, RakPeerInterface server,string reason)
        {
            logger.Trace("Disconnected from endpoint {0}", packet.systemAddress);
            var c = RemoveConnection(packet.guid);
            server.DeallocatePacket(packet);

            _handler.CloseConnection(c,reason);

            var action = ConnectionClosed;
            if (action != null)
            {
                action(c);
            }
            c.ConnectionClosed(reason);
        }
        private IConnection OnConnection(RakNet.Packet packet, RakPeerInterface server)
        {
            logger.Trace("Connected to endpoint {0}", packet.systemAddress);

            IConnection c = CreateNewConnection(packet.guid, server);
            var ctx = new PeerConnectedContext { Connection = c };
            var pconnected = _connectionHandler.PeerConnected;
            if(pconnected !=null)
            {
                pconnected(ctx);
            }
            
            c = ctx.Connection;
            server.DeallocatePacket(packet);
            _handler.NewConnection(c);
            var action = ConnectionOpened;
            if (action != null)
            {
                action(c);
            }

            c.SendSystem((byte)MessageIDTypes.ID_CONNECTION_RESULT, s => s.Write(BitConverter.GetBytes(c.Id), 0, 8));
            return c;
        }
Beispiel #17
0
 public Test()
 {
     _peer = RakPeerInterface.GetInstance();
     _bStop = false;
 }
Beispiel #18
0
 public static void DestroyInstance(RakPeerInterface i) {
   RakNetPINVOKE.RakPeerInterface_DestroyInstance(RakPeerInterface.getCPtr(i));
 }
 internal static HandleRef getCPtr(RakPeerInterface obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }
        static void ProcessMessage(RakPeerInterface peer, RakNet.Packet packet)
        {
            if (packet != null)
            {
                if (packet.data[0] == (byte)(FT_MessageTypes.ID_SERVER_LOGIN))
                {
                    BitStream receiveBitStream = new BitStream();
                    receiveBitStream.Write(packet.data, packet.length);
                    receiveBitStream.IgnoreBytes(2);
                    FT_UnitData data = new FT_UnitData();
                    data.Serialize(false, receiveBitStream);
                    Log.Debug(" data.nGrid_x: " + data.nGrid_x);

                    BitStream writeBitStream = new BitStream();
                    writeBitStream.Write((byte)FT_MessageTypes.ID_SERVER_LOGIN);
                    writeBitStream.Write((byte)FT_MessageTypesNode.NODE_FT_TEST1);
                    data.Serialize(true, writeBitStream);
                    uint sendLength = peer.Send(writeBitStream, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, packet.systemAddress, false);
                    Log.Debug("SendLength = " + sendLength);
                }
            }
        }
 public void SetRakPeerInterface(RakPeerInterface ptr) {
   RakNetPINVOKE.FT_Node_Process_SetRakPeerInterface(swigCPtr, RakPeerInterface.getCPtr(ptr));
 }
 public void Send(FileList fileList, RakPeerInterface rakPeer, SystemAddress recipient, ushort setID, PacketPriority priority, char orderingChannel) {
   RakNetPINVOKE.FileListTransfer_Send__SWIG_2(swigCPtr, FileList.getCPtr(fileList), RakPeerInterface.getCPtr(rakPeer), SystemAddress.getCPtr(recipient), setID, (int)priority, orderingChannel);
   if (RakNetPINVOKE.SWIGPendingException.Pending) throw RakNetPINVOKE.SWIGPendingException.Retrieve();
 }
Beispiel #23
0
 public void SetRakPeerInterface(RakPeerInterface ptr)
 {
     RakNetPINVOKE.FT_Node_Process_SetRakPeerInterface(swigCPtr, RakPeerInterface.getCPtr(ptr));
 }
 public void SetRakPeer(RakPeerInterface ptr)
 {
     RakNetPINVOKE.FT_Node_Plugin_SetRakPeer(swigCPtr, RakPeerInterface.getCPtr(ptr));
 }
Beispiel #25
0
 internal static HandleRef getCPtr(RakPeerInterface obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
 public static void DestroyInstance(RakPeerInterface i)
 {
     RakNetPINVOKE.RakPeerInterface_DestroyInstance(RakPeerInterface.getCPtr(i));
 }
 public void SetRakPeer(RakPeerInterface ptr) {
   RakNetPINVOKE.FT_Node_Plugin_SetRakPeer(swigCPtr, RakPeerInterface.getCPtr(ptr));
 }
        private void OnConnection(RakNet.Packet packet, RakPeerInterface server)
        {
            logger.Trace("Connected to endpoint {0}", packet.systemAddress);

            var c = CreateNewConnection(packet.guid, server);
            server.DeallocatePacket(packet);
            _handler.NewConnection(c);
            var action = ConnectionOpened;
            if (action != null)
            {
                action(c);
            }

            c.SendSystem((byte)MessageIDTypes.ID_CONNECTION_RESULT, s => s.Write(BitConverter.GetBytes(c.Id), 0, 8));
        }