Beispiel #1
0
        void Client_OnStatusChanged(object sender, NetWrapConnectionStatus e)
        {
            switch (e.Status)
            {
            case NetWrapConnectionStatusEnum.Connected:
                ServerConnectionId = e.ConnectionId;
                ConnectedEndPoint  = e.EndPoint;
                break;

            case NetWrapConnectionStatusEnum.Disconnected:
            case NetWrapConnectionStatusEnum.TimedOut:
                ServerConnectionId = -1;
                ConnectedEndPoint  = null;
                break;
            }

            Console.WriteLine("Client: (" + e.EndPoint.ToString() + ") Status changed to " + e.Status.ToString());
        }
Beispiel #2
0
        public bool Update()
        {
            if (m_valid == false)
            {
                return(false);
            }

            i_checkStartENetThread();

            using (MonitorLock.CreateLock(m_outgoingPacketListLocks[m_currentOutgoingPacketListIndex]))
            {
                using (MonitorLock.CreateLock(m_outgoingPacketListLocks[(m_currentOutgoingPacketListIndex + 1) % 2]))
                {
                    if (m_outgoingPacketLists[(m_currentOutgoingPacketListIndex + 1) % 2].Count > 0 && m_outgoingPacketLists[m_currentOutgoingPacketListIndex].Count == 0)
                    {
                        m_currentOutgoingPacketListIndex = (m_currentOutgoingPacketListIndex + 1) % 2;
                    }
                }
            }

            using (MonitorLock.CreateLock(m_enetEventListLocks[m_currentENetEventListIndex]))
            {
                if (m_enetEventLists[m_currentENetEventListIndex].Count > 0)
                {
                    m_currentENetEventListIndex = (m_currentENetEventListIndex + 1) % 2;
                    m_enetEventLists[m_currentENetEventListIndex].Clear();
                }
            }

            using (MonitorLock.CreateLock(m_enetEventListLocks[m_currentENetEventListIndex]))
            {
                foreach (var enetEvent in m_enetEventLists[(m_currentENetEventListIndex + 1) % 2])
                {
                    if (enetEvent.Type == ENet.EventType.None)
                    {
                        continue;
                    }

                    NetWrapConnection connection;
                    if (enetEvent.Peer.UserData == IntPtr.Zero)
                    {
                        var peer = enetEvent.Peer;
                        connection = new NetWrapConnection
                        {
                            ConnectionId = connectionIDCounter++,
                            Peer         = enetEvent.Peer
                        };
                        m_connections.TryAdd(connection.ConnectionId, connection);
                        peer.UserData = GCHandle.ToIntPtr(GCHandle.Alloc(connection));
                    }
                    else
                    {
                        var gcHandle = GCHandle.FromIntPtr(enetEvent.Peer.UserData);
                        connection = (NetWrapConnection)gcHandle.Target;
                        m_connections.GetOrAdd(connection.ConnectionId, connection);
                    }

                    switch (enetEvent.Type)
                    {
                    case ENet.EventType.Connect:
                    {
                        // set timeouts
                        enetEvent.Peer.SetTimeouts(5, 30000, 90000);

                        NumConnections++;

                        var wrappedStatus = new NetWrapConnectionStatus()
                        {
                            Status       = NetWrapConnectionStatusEnum.Connected,
                            EndPoint     = enetEvent.Peer.GetRemoteAddress(),
                            ConnectionId = connection.ConnectionId
                        };
                        OnStatusChanged(this, wrappedStatus);
                    }
                    break;

                    case ENet.EventType.Disconnect:
                    {
                        NumConnections--;

                        var wrappedStatus = new NetWrapConnectionStatus()
                        {
                            Status       = NetWrapConnectionStatusEnum.Disconnected,
                            EndPoint     = enetEvent.Peer.GetRemoteAddress(),
                            ConnectionId = connection.ConnectionId
                        };
                        OnStatusChanged(this, wrappedStatus);
                    }
                    break;

                    case ENet.EventType.Receive:
                        byte[] data = enetEvent.Packet.GetBytes();
                        if (data.Length >= 5 && data[0] == RPCHeader[0] && data[1] == RPCHeader[1] && data[2] == RPCHeader[2] && data[3] == RPCHeader[3])
                        {
#if RELEASE // Needs to be #if-ed out in debug to let errors fall into the debugger
                            try
                            {
#endif
                            var rpcDataTemplate = new RPCData();
                            var msg             = new NetWrapIncomingMessage();
                            msg.SenderConnectionId          = connection.ConnectionId;
                            rpcDataTemplate.OriginalMessage = msg;

                            int offset = 5;
                            for (int i = 0; i < data[4]; i++)
                            {
                                int blockbytes = BitConverter.ToInt32(data, offset);
                                offset += 4;
                                if (blockbytes < 0)
                                {
                                    // compressed RPC call
                                    blockbytes = -blockbytes;
                                    var buffer = NetCompressByteArray.Unwrap(data, offset);
                                    var stream = new MemoryStream(buffer);
                                    RPCStream.Execute(stream, RpcDispatcher, rpcDataTemplate);
                                }
                                else
                                {
                                    // uncompressed RPC call
                                    var stream = new MemoryStream(data, offset, blockbytes);
                                    RPCStream.Execute(stream, RpcDispatcher, rpcDataTemplate);
                                }
                                offset += blockbytes;
                            }
#if RELEASE
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                using (var stream = System.IO.File.AppendText("ignored-net-errors.txt"))
                                    stream.WriteLine(DateTime.Now.ToString() + " : " + ex.ToString());
                            } catch {}

                            Console.WriteLine(ex.ToString());
                            break;
                        }
#endif
                        }
                        else
                        {
                            NetWrapIncomingMessage msg = new NetWrapIncomingMessage()
                            {
                                Message            = data,
                                MessageCount       = data.Length,
                                MessageOffset      = 0,
                                SenderConnectionId = connection.ConnectionId
                            };
                            OnData(this, msg);
                        }
                        enetEvent.Packet.Dispose();
                        break;

                    default:
                        Console.WriteLine(enetEvent.Type);
                        break;
                    }
                }
                m_enetEventLists[(m_currentENetEventListIndex + 1) % 2].Clear();
            }
            return(true);
        }
Beispiel #3
0
 void Server_OnStatusChanged(object sender, NetWrapConnectionStatus e)
 {
     Console.WriteLine(DateTime.Now + " " + "Server: (" + e.EndPoint.ToString() + ") Status changed to " + e.Status.ToString());
 }