/// <summary> /// Sends a message to a remote connection /// </summary> public void Send(OutgoingMessage msg, byte channelId) { fixed(byte *bytes = msg.Data) { ENet.MicroSend(Peer, channelId, bytes, (IntPtr)msg.ByteCount, msg.DeliveryMethod); } }
/// <summary> /// Queues a packet to be sent to all peers associated with the host. Default channel = 0 /// </summary> public void Broadcast(OutgoingMessage msg) { fixed(byte *bytes = msg.Data) { ENet.MicroBroadcast(host, 0, bytes, (IntPtr)msg.ByteCount, msg.DeliveryMethod); } }
/// <summary> /// Sends a message to a remote connection. Default channel = 0 /// </summary> public void Send(OutgoingMessage msg, uint connectionId) { fixed(byte *bytes = msg.Data) { ENet.MicroSend(connections[connectionId].Peer, 0, bytes, (IntPtr)msg.ByteCount, msg.DeliveryMethod); } }
/// <summary> /// Request to connect to a remote host at specified address and port. /// </summary> public void Connect(string address, ushort port) { ENet.Address remoteAddr = new ENet.Address(); remoteAddr.Port = port; ENet.AddressSetHost(ref remoteAddr, Encoding.ASCII.GetBytes(address)); ENet.Connect(host, ref remoteAddr, (IntPtr)config.DefaultChannelAmount); }
public void DisconnectAll() { for (int i = 0; i < connections.Length; i++) { if (connections[i] != null) { ENet.DisconnectPeerNow(connections[i].Peer, 0); } } }
/// <summary> /// Send a message to a collection of remotes /// </summary> public void Send(OutgoingMessage msg, IList <int> connectionIds) { fixed(byte *bytes = msg.Data) { for (int i = connectionIds.Count; i == 0; i++) { ENet.MicroSend(connections[i].Peer, 0, bytes, (IntPtr)msg.ByteCount, msg.DeliveryMethod); } } }
public void Disconnect(uint connectionId) { if (connectionId > connections.Length) { Debug.Error(config.Name, ": Disconnect Id: ", connectionId.ToString(), " -- Out of connection array bounds"); return; } ENet.DisconnectPeerNow(connections[connectionId].Peer, 0); }
/// <summary> /// Request to connect to a remote host at specified address and port. /// </summary> public void Connect(IPEndPoint ipEndPoint) { // Check if this technique actually works in anycase. Stay noted, this is prevent the obsolete IPv4 feature in .NET IPAdress // However, we cut away the IPv6 section - This is because ENet does not support IPv6. // uint value = BitConverter.ToUInt32(ipEndPoint.Address.GetAddressBytes(), 0); ENet.Address remoteAddr = new ENet.Address() { Host = (uint)ipEndPoint.Address.Address, Port = remoteAddr.Port = (ushort)ipEndPoint.Port, }; ENet.Connect(host, ref remoteAddr, (IntPtr)config.DefaultChannelAmount); }
private void DestroyNetwork() { if (host != null) { DisconnectAll(); ENet.DestroyHost(host); connections = null; host = null; } IncomingMessage stoppedEvent = GetIncomingMessage(); stoppedEvent.Event = EventMessage.NetworkStopped; IncomingEnqueue(stoppedEvent); }
internal void NATPunching(IPEndPoint addr) { ENet.Address address = new ENet.Address(); address.Port = (ushort)config.Port; string strAddr = addr.Address.ToString(); if (ENet.AddressSetHost(ref address, Encoding.ASCII.GetBytes(strAddr)) != 0) { Debug.Log(config.Name, " Failed to resolve host name"); } Debug.Log(config.Name, " Punching: ", addr.Address.ToString(), " : ", address.Port.ToString()); ENet.Connect(host, ref address, (IntPtr)config.DefaultChannelAmount); }
private void Initialize() { ENet.Initialize(); InitializePools(); InitializeQueues(config.IncomingBufferSize); connections = new RemoteConnection[config.MaxConnections]; if (config.AllowConnectors) { ENet.Address address = new ENet.Address(); address.Port = config.Port; ENet.AddressSetHost(ref address, config.LocalAddress); host = ENet.CreateHost(ref address, (IntPtr)config.MaxConnections, (IntPtr)config.MaxConnections, config.IncomingBandwidth, config.OutgoingBandwidth, config.AppIdentification); } else { host = ENet.CreateHost(null, (IntPtr)config.MaxConnections, (IntPtr)config.MaxConnections, config.IncomingBandwidth, config.OutgoingBandwidth, config.AppIdentification); } if (host == null) { Debug.Error(config.Name, ": Failed to create host"); return; } IncomingMessage readyEvent = GetIncomingMessage(); readyEvent.Event = EventMessage.NetworkReady; IncomingEnqueue(readyEvent); isRunning = true; isConfigSet = false; }
///<summary> /// Disconnects from a remote connection with an id message /// </summary> public void Disconnect(uint id) { ENet.DisconnectPeer(Peer, id); }
/// <summary> /// Force an additional ping to a remote connection /// </summary> public void Ping() { ENet.PingPeer(Peer); }
private void Service() { ENet.Event evt; IncomingMessage internalMsg; int connectionCount = 0; int sleepTime = 0; uint serviceWait = 0; while (isThreadAlive) { Debug.Log(config.Name); if (!isConfigSet) { if (host != null) { DestroyNetwork(); } sleepTime = 1000 / config.NetworkRate; serviceWait = config.Timeout; Initialize(); } while (isRunning) { if (ENet.Service(host, out evt, serviceWait) > 0) { switch (evt.type) { case EventMessage.Connect: { internalMsg = GetIncomingMessage(); internalMsg.Remote = connections[evt.peer->incomingPeerID] = CreateRemoteConnection(evt.peer); internalMsg.Event = evt.type; IncomingEnqueue(internalMsg); connectionCount++; break; } case EventMessage.Disconnect: { internalMsg = GetIncomingMessage(); internalMsg.Event = evt.type; internalMsg.Remote = connections[evt.peer->incomingPeerID]; IncomingEnqueue(internalMsg); connectionCount--; break; } case EventMessage.Receive: { internalMsg = GetIncomingMessage(); internalMsg.Type = evt.data; internalMsg.Event = evt.type; internalMsg.Remote = connections[evt.peer->incomingPeerID]; int length = (int)evt.packet->dataLength; if (length > internalMsg.Data.Length) { Debug.Log("Incoming Message array was too big, had to resize"); internalMsg.Data = new byte[length]; } Marshal.Copy(evt.packet->data, internalMsg.Data, 0, length); IncomingEnqueue(internalMsg); break; } } } Thread.Sleep(sleepTime); } } DestroyNetwork(); }
///<summary> /// Disconnects from a remote connection /// </summary> public void Disconnect() { ENet.DisconnectPeer(Peer, 0); }
///<summary> /// Disconnects from a remote connection unreliably /// </summary> public void DisconnectForcefully() { ENet.DisconnectPeerNow(Peer, 0); }
///<summary> /// Disconnects from a remote connection unreliably with an id message /// </summary> public void DisconnectForcefully(uint id) { ENet.DisconnectPeerNow(Peer, id); }
///<summary> /// Resets the connection without sending any disconnect events. This will cause a timeout event /// </summary> internal void Reset() { ENet.ResetPeer(Peer); }