public override SocketTasks StartClient()
        {
            SocketConfig config = GetConfig(false);

            socket = new RuffleSocket(config);

            isConnector = true;

            if (!socket.Start())
            {
                return(SocketTask.Fault.AsTasks());
            }

            serverConnection = socket.Connect(new IPEndPoint(IPAddress.Parse(ConnectAddress), Port));

            if (serverConnection == null)
            {
                return(SocketTask.Fault.AsTasks());
            }
            else
            {
                connectTask = SocketTask.Working;

                return(connectTask.AsTasks());
            }
        }
Example #2
0
        public override bool StartServer()
        {
            SocketConfig config = GetConfig(true);

            socket = new RuffleSocket(config);

            serverConnection = null;
            isConnector      = false;

            return(socket.Start());
        }
        public override SocketTasks StartServer()
        {
            SocketConfig config = GetConfig(true);

            socket = new RuffleSocket(config);

            serverConnection = null;
            isConnector      = false;

            if (socket.Start())
            {
                return(SocketTask.Done.AsTasks());
            }
            else
            {
                return(SocketTask.Fault.AsTasks());
            }
        }
Example #4
0
        public override void Shutdown()
        {
            channelNameToId.Clear();
            connections.Clear();

            // Release to GC
            messageBuffer = null;

            if (socket != null && socket.IsInitialized)
            {
                // Releases memory and other things
                socket.Shutdown();
            }

            // Release to GC
            socket = null;

            // Release server connection to GC
            serverConnection = null;
        }
Example #5
0
        public override bool StartClient()
        {
            SocketConfig config = GetConfig(false);

            socket = new RuffleSocket(config);

            isConnector = true;

            if (!socket.Start())
            {
                return(false);
            }

            serverConnection = socket.Connect(new IPEndPoint(IPAddress.Parse(ConnectAddress), Port));

            if (serverConnection == null)
            {
                return(false);
            }

            return(true);
        }
Example #6
0
        public override NetEventType PollEvent(out ulong clientId, out string channelName, out ArraySegment <byte> payload)
        {
            socket.RunInternalLoop();
            NetworkEvent @event = socket.Poll();

            if (@event.Type != NetworkEventType.Nothing)
            {
                clientId = GetMLAPIClientId(@event.Connection.Id, false);
            }
            else
            {
                clientId = 0;
            }

            byte[] dataBuffer = messageBuffer;

            if (@event.Type == NetworkEventType.Data)
            {
                if (@event.Data.Count > messageBuffer.Length)
                {
                    if (temporaryBufferReference != null && temporaryBufferReference.IsAlive && ((byte[])temporaryBufferReference.Target).Length >= @event.Data.Count)
                    {
                        dataBuffer = (byte[])temporaryBufferReference.Target;
                    }
                    else
                    {
                        dataBuffer = new byte[@event.Data.Count];
                        temporaryBufferReference = new WeakReference(dataBuffer);
                    }
                }

                Buffer.BlockCopy(@event.Data.Array, @event.Data.Offset, dataBuffer, 0, @event.Data.Count);
                payload = new ArraySegment <byte>(dataBuffer, 0, @event.Data.Count);
            }
            else
            {
                payload = new ArraySegment <byte>();
            }

            channelName = channelIdToName[@event.ChannelId];

            // Translate NetworkEventType to NetEventType
            switch (@event.Type)
            {
            case NetworkEventType.Data:
                return(NetEventType.Data);

            case NetworkEventType.Connect:
            {
                connections.Add(@event.Connection.Id, @event.Connection);

                // Set the server connectionId
                if (isConnector)
                {
                    serverConnection = @event.Connection;
                }

                return(NetEventType.Connect);
            }

            case NetworkEventType.Timeout:
            case NetworkEventType.Disconnect:
            {
                if (@event.Connection == serverConnection)
                {
                    serverConnection = null;
                }

                connections.Remove(@event.Connection.Id);

                @event.Connection.Recycle();

                return(NetEventType.Disconnect);
            }

            case NetworkEventType.Nothing:
                return(NetEventType.Nothing);
            }

            return(NetEventType.Nothing);
        }