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);
 }
Ejemplo n.º 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();
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        /// <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;
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        /// <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);
        }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
        /// <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);
        }
Ejemplo n.º 10
0
        /// <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);
 }
Ejemplo n.º 12
0
 /// <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;
 }
Ejemplo n.º 15
0
 /// <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;
 }
Ejemplo n.º 16
0
        /// <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);
        }
Ejemplo n.º 17
0
 ///<summary>
 /// Resets the connection without sending any disconnect events
 /// </summary>
 public void Reset()
 {
     ENet.ResetPeer(Peer);
 }
Ejemplo n.º 18
0
        /// <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);
            }
        }
Ejemplo n.º 19
0
        /// <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);
        }
Ejemplo n.º 20
0
        /// <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;
        }
Ejemplo n.º 21
0
 ///<summary>
 /// Disconnects from a remote connection
 /// </summary>
 public void Disconnect()
 {
     ENet.DisconnectPeer(Peer, 1);
 }
Ejemplo n.º 22
0
        /// <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);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Force an additional ping to a remote connection
 /// </summary>
 public void Ping()
 {
     ENet.PingPeer(Peer);
 }
Ejemplo n.º 24
0
        /// <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);
            }
        }
Ejemplo n.º 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);
 }