/// <summary> /// /// </summary> /// <param name="peer"></param> internal void Initialize(Peer peer) { lock(NextClientIdLock) ClientId = ++NextClientId; m_peer = peer; m_peer.SetTimeouts(5, 2000, 4000); }
/// <summary> /// Enable to connect to a server /// </summary> /// <param name="server">Server.</param> public static void connectTo(string server, string user) { userName = user; int port = 20336; string ipServer = server; // parse to have the server IP and Port if (server.Contains (":")) { string[] serverSplited = server.Split (':'); ipServer = serverSplited [0]; port = int.Parse (serverSplited [1]); } // initialize the connection, after 2s to wait, it is concidered // that the server does not respond ConnexionUtils.host.InitializeClient (1); ConnexionUtils.peer = ConnexionUtils.host.Connect (ipServer, port, 0, 2); if (ConnexionUtils.host.Service (2000, out ConnexionUtils.@event)) { if (ENet.EventType.Connect == [email protected]) { ConnexionUtils.connected = true; Debug.Log ("Connected to server at IP/port " + peer.GetRemoteAddress ()); } } else { Debug.LogError ("Problème de connexion, met plus de 2s à répondre"); peer.Disconnect (0); host.Dispose (); } }
public Peer Connect(Address address, int channelLimit) { CheckCreated(); CheckChannelLimit(channelLimit); var nativeAddress = address.NativeData; var peer = new Peer(Native.enet_host_connect(_host, ref nativeAddress, (IntPtr) channelLimit)); if (peer.NativeData == null) { throw new ENetException(0, "Host connect call failed."); } return peer; }
public bool Connect(IPEndPoint serverAddress) { client.Connect(serverAddress, 0, 2); ENet.Event e = new ENet.Event(); if (client.Service(CONNECTIONTIMEOUT, out e) && e.Type == ENet.EventType.Connect) { Console.Out.WriteLine("Successful connection to " + serverAddress.Address.ToString() + ":" + serverAddress.Port); server = e.Peer; listeningThread = new Thread(this.listen); listeningThread.Start(); return true; } else { Console.Out.WriteLine("Connection failure to " + serverAddress.Address.ToString() + ":" + serverAddress.Port); return false; } }
private static void Server() { using (var host = new Host()) { host.Create(5000, 1); var peer = new Peer(); while (host.Service(1) >= 0) { Event @event; while (host.CheckEvents(out @event) > 0) { //Console.WriteLine("Server: " + @event.Type.ToString()); switch (@event.Type) { case EventType.Connect: peer = @event.Peer; for (var i = 0; i < 200; i++) { peer.Send((byte) i, new byte[] {0, 0}); } break; case EventType.Receive: var data = @event.Packet.GetBytes(); var value = BitConverter.ToUInt16(data, 0); if (value%1000 == 1) { Console.WriteLine(" Server: Ch={0} Recv={1}", @event.ChannelID, value); } value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value)); @event.Packet.Dispose(); break; } } } } }
protected unsafe void listen() { ENet.Event e = new ENet.Event(); while (!end) { client.Service(TIMEOUT, out e); //Console.Out.WriteLine("Message received. Type : " + e.Type); switch (e.Type) { // needs more error handling case ENet.EventType.Connect: break; case ENet.EventType.Disconnect: server = new ENet.Peer(); end = true; // no thread join in this case, fix ? Console.Out.WriteLine("Connection closed by server"); break; case ENet.EventType.Receive: //String message = new String((sbyte*)e.Packet.Data.ToPointer(), 0, e.Packet.Length); byte[] bytes = new byte[e.Packet.Length]; for (int i = 0; i < e.Packet.Length; i++) { bytes[i] = *((byte*)(e.Packet.Data.ToPointer())+i); } dispatchMessage(bytes); break; case ENet.EventType.None: break; } } }
private void dispatchMessage(byte[] bytes, Peer peer) { MemoryStream stream = new MemoryStream(bytes); BinaryFormatter formater = new BinaryFormatter(); NetFrame frame = (NetFrame)formater.Deserialize(stream); switch (frame.type) { case NetFrame.FrameType.player: Player newPlayer = engine.AddPlayer((LightPlayer)frame.content); clientMap.Add(peer, newPlayer); playerMap.Add(newPlayer, peer); break; case NetFrame.FrameType.position: engine.UpdatePosition(clientMap[peer], (Position)frame.content); break; case NetFrame.FrameType.shootCommand: engine.AddShoot(clientMap[peer], (LightShoot) frame.content); break; } }