public void SendReliable(String message, ENet.Peer destination)
 {
     ENet.Packet packet = new ENet.Packet();
     byte[] buffer = Encoding.ASCII.GetBytes(message);
     packet.Initialize(buffer, ENet.PacketFlags.Reliable);
     destination.Send((byte)(clients.IndexOf(destination) * 2 + 1), packet);
 }
Example #2
0
        static void TestAsync()
        {
            var address = new Address("127.0.0.1", 5005);
            var server  = new ENet {
                name = "Server"
            };
            var client = new ENet {
                name = "Client"
            };

            server.host = new Host(LENet.Version.Seasson8_Server, address, 32, 8, 0, 0);

            client.host = new Host(LENet.Version.Seasson8_Client, null, 1, 8, 0, 0);
            client.peer = client.host.Connect(address, 8);

            client.RunLoop();
            server.RunLoop();
            client.RunLoop();
            server.RunLoop();

            client.peer.Send(0, new Packet(42, PacketFlags.RELIABLE));

            client.RunLoop();
            server.RunLoop();

            client.peer.Send(0, new Packet(69, PacketFlags.RELIABLE));


            client.RunOnce();
            server.RunOnce();
        }
        /// <summary>
        /// 添加适配器
        /// </summary>
        /// <param name="net"></param>
        /// <param name="type"></param>
        protected void AddAdapter(ENet net, Type type)
        {
            if (mAdapterDict.ContainsKey(net))
                return;

            mAdapterDict.Add(net, type);
        }
        public void Connect()
        {
            ENet.Address remoteAddr = new ENet.Address()
            {
                Host = (uint)endPoint.Address.Address,
                Port = remoteAddr.Port = (ushort)endPoint.Port
            };

            Peer = ENet.Connect(Host, ref remoteAddr, (IntPtr)1);
        }
        /// <summary>
        /// 创建适配器
        /// </summary>
        /// <param name="net"></param>
        /// <returns></returns>
        public NetAdapter CreateAdapter(ENet net)
        {
            if (!mAdapterDict.ContainsKey(net))
                return null;

            Type type = mAdapterDict[net];
            if (type == null)
                return null;

            NetAdapter adapter = Activator.CreateInstance(type) as NetAdapter;

            return adapter;
        }
        /// <summary>
        /// Sends a message to a remote connection
        /// </summary>
        public void InformPunchthroughSuccess()
        {
            OutgoingMessage request = MessagePool.CreateMessage();

            request.Write(NATMessageType.NAT_PUNCH_SUCCESS);

            fixed(byte *bytes = request.Data)
            {
                ENet.MicroSend(Peer, 0, bytes, (IntPtr)request.ByteCount, DeliveryMethod.Reliable);
            }

            MessagePool.Recycle(request);
        }
        /// <summary>
        /// Sends a message to a remote connection. Default channel = 0
        /// </summary>
        public void RequestHostList()
        {
            OutgoingMessage regMessage = MessagePool.CreateMessage();

            regMessage.Write(NATMessageType.REQUEST_HOST_LIST);


            fixed(byte *bytes = regMessage.Data)
            {
                ENet.MicroSend(Peer, 0, bytes, (IntPtr)regMessage.ByteCount, DeliveryMethod.Reliable);
            }

            MessagePool.Recycle(regMessage);
        }
        /// <summary>
        /// Sends a message to a remote connection. Default channel = 0
        /// </summary>
        public void RegisterHosting()
        {
            OutgoingMessage regMessage = MessagePool.CreateMessage();
            IPAddress       local      = NetUtilities.GetLocalAddress();

            regMessage.Write(NATMessageType.INITIATE_HOST);
            regMessage.WriteString("hello");

            fixed(byte *bytes = regMessage.Data)
            {
                ENet.MicroSend(Peer, 0, bytes, (IntPtr)regMessage.ByteCount, DeliveryMethod.Reliable);
            }

            MessagePool.Recycle(regMessage);
        }
        /// <summary>
        /// Sends a message to a remote connection
        /// </summary>
        public void RequestIntroduction(ulong hostId)
        {
            OutgoingMessage request = MessagePool.CreateMessage();

            //  IPAddress local = NetUtilities.GetLocalAddress();
            request.Write(NATMessageType.REQUEST_INTRODUCTION);
            request.Write(hostId);


            fixed(byte *bytes = request.Data)
            {
                ENet.MicroSend(Peer, 0, bytes, (IntPtr)request.ByteCount, DeliveryMethod.Reliable);
            }

            MessagePool.Recycle(request);
        }
        /// <summary>
        /// 获取适配器
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        protected NetAdapter GetAdapter(ENet net, object session)
        {
            lock(mAllAdapterLock)
            {
                if (session == null)
                    return null;
                Dictionary<object, NetAdapter> adapterDict;
                mAllAdapterDict.TryGetValue(net, out adapterDict);
                if (adapterDict == null)
                    return null;

                NetAdapter adapter;
                adapterDict.TryGetValue(session, out adapter);

                return adapter;
            }
        }
 protected void removeClient(ENet.Peer client)
 {
     clients.Remove(client);
 }
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="AdapterID">唯一值</param>
 /// <param name="net">网络模型</param>
 /// <param name="param">参数值</param>
 public override void Init(long lAdapterID, ENet net, object param)
 {
     base.Init(lAdapterID, net, param);
     mSession = param as BinaryServlet;
 }
 public void SendReliable(Object message, NetFrame.FrameType type, ENet.Peer destination)
 {
     ENet.Packet packet = new ENet.Packet();
     NetFrame f = new NetFrame(message, type);
     MemoryStream stream = new MemoryStream(512); // TODO : buffer size ?
     BinaryFormatter formater = new BinaryFormatter();
     formater.Serialize(stream, f);
     packet.Initialize(stream.GetBuffer(), ENet.PacketFlags.Reliable);
     destination.Send((byte)(clients.IndexOf(destination) * 2 + 1), packet);
     server.Flush();
 }
 protected bool addClient(ENet.Peer newClient)
 {
     Console.Out.WriteLine("New connection from client " + newClient.GetRemoteAddress().Address.ToString() + ":"
         + newClient.GetRemoteAddress().Port);
     if (clients.Contains(newClient))
     {
         return false;
     }
     clients.Add(newClient);
     return true;
 }
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="AdapterID">唯一值</param>
 /// <param name="net">网络模型</param>
 /// <param name="param">参数值</param>
 public override void Init(long lAdapterID, ENet net, object param)
 {
     base.Init(lAdapterID, net, param);
     mSession = param as UDPSession;
 }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        /// <param name="msg"></param>
        /// <param name="Immediate"></param>
        public override void SendMessage(ENet net, object session, Message msg, bool Immediate = true)
        {
            NetAdapter adapter = GetAdapter(net, session);
            if (adapter == null)
                return;

            SendMessage(adapter.AdapterID, msg, Immediate);
        }
Example #17
0
 ///<summary>
 /// Resets the connection without sending any disconnect events
 /// </summary>
 public void Reset()
 {
     ENet.ResetPeer(Peer);
 }
        /// <summary>
        /// 添加适配器
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        /// <param name="adapter"></param>
        protected void AddAdapter(ENet net, object session, NetAdapter adapter)
        {
            lock(mAllAdapterLock)
            {
                Dictionary<object, NetAdapter> adapterDict;
                mAllAdapterDict.TryGetValue(net, out adapterDict);
                if (adapterDict == null)
                {
                    adapterDict = new Dictionary<object,NetAdapter>();
                    mAllAdapterDict.Add(net, adapterDict);
                }

                // 重复了
                if( adapterDict.ContainsKey(session) )
                {
                    return;
                }

                adapterDict.Add(session, adapter);
            }
        }
        /// <summary>
        /// 断开连接处理
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        protected void DisconectionHandle(ENet net, object session)
        {
            NetAdapter adapter = GetAdapter(net, session);
            if (adapter == null)
                return;

            mDisconnectAdapter.Enqueue(adapter.AdapterID);
        }
        /// <summary>
        /// 连接处理
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        protected long ConnectionHandle(ENet net, object session)
        {
            NetAdapter adapter = GetAdapter(net, session);
            // 已经存在这个适配器了
            if (adapter != null)
            {
                if( net == ENet.UdpClient )
                    mConnectAdapter.Enqueue(adapter.AdapterID);
                return adapter.AdapterID;
            }

            // 创建适配器
            adapter = mAdapterFactor.CreateAdapter(net);
            adapter.Init(NextAdapterID(), net, session);

            AddAdapter(adapter.AdapterID, adapter);
            AddAdapter(net, session, adapter);

            mConnectAdapter.Enqueue(adapter.AdapterID);

            return adapter.AdapterID;
        }
Example #21
0
 ///<summary>
 /// Disconnects from a remote connection
 /// </summary>
 public void Disconnect()
 {
     ENet.DisconnectPeer(Peer, 1);
 }
        /// <summary>
        /// 接收处理
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        /// <param name="byData"></param>
        /// <param name="length"></param>
        /// <param name="offset"></param>
        protected void ReceiveHandle(ENet net, object session, byte[] byData, int length, int offset)
        {
            NetAdapter adapter = GetAdapter(net, session);
            if (adapter == null)
                return;

            // 这里处理接收消息,转化成Message
            if (byData == null || byData.Length < (length + offset) || length < sizeof(short) + sizeof(int))
                return;

            MemBlock block = NetAdapter.Alloc(length);
            Buffer.BlockCopy(byData, offset, block.GetBytes(), 0, length);

            MessageCache cache = new MessageCache();
            cache.AdapterID = adapter.AdapterID;
            cache.Block = block;

            PushMessageQueue(cache);
        }
Example #23
0
 /// <summary>
 /// Force an additional ping to a remote connection
 /// </summary>
 public void Ping()
 {
     ENet.PingPeer(Peer);
 }
        /// <summary>
        /// 删除适配器
        /// </summary>
        /// <param name="net"></param>
        /// <param name="session"></param>
        protected void RemoveAdapter(ENet net, object session)
        {
            lock (mAllAdapterLock)
            {
                Dictionary<object, NetAdapter> adapterDict;
                mAllAdapterDict.TryGetValue(net, out adapterDict);
                if (adapterDict == null)
                    return;

                adapterDict.Remove(session);
            }
        }
Example #25
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="AdapterID">唯一值</param>
 /// <param name="net">网络模型</param>
 /// <param name="param">参数值</param>
 public virtual void Init(long lAdapterID, ENet net, object param)
 {
     mAdapterID = lAdapterID;
     mNetType = net;
     mObjSession = param;
 }
 public void SendReliable(byte[] message, ENet.Peer destination)
 {
     ENet.Packet packet = new ENet.Packet();
     packet.Initialize(message, ENet.PacketFlags.Reliable);
     destination.Send((byte)(clients.IndexOf(destination) * 2 + 1), packet);
 }