public void Stop()
        {
            BmUpdateRunner.Instance.Remove(this);

#if !UNITY_WEBGL || UNITY_EDITOR
            NetworkTransport.RemoveHost(_socketId);
#endif
        }
        public void Listen(int port)
        {
#if !UNITY_WEBGL || UNITY_EDITOR
            NetworkTransport.Init();
            _socketId = NetworkTransport.AddHost(_topology, port);
            BmUpdateRunner.Instance.Add(this);
#endif
        }
        public void Disconnect()
        {
            byte error;

            NetworkTransport.Disconnect(_socketId, _connectionId, out error);
            NetworkTransport.RemoveHost(_socketId);

            // When we disconnect ourselves, we dont get NetworkEventType.DisconnectEvent
            // Not sure if that's the expected behaviour, but oh well...
            // TODO Make sure there's no other way
            HandleDisconnect(_connectionId, error);
        }
        /// <summary>
        /// Starts connecting to another socket
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="timeoutMillis"></param>
        /// <returns></returns>
        public IClientSocket Connect(string ip, int port, int timeoutMillis)
        {
            ConnectionIp   = ip;
            ConnectionPort = port;
            NetworkTransport.Init();
            _stopConnectingTick = Environment.TickCount + timeoutMillis;
            _ip          = ip;
            _port        = port;
            IsConnecting = true;

            _socketId = NetworkTransport.AddHost(_topology, 0);

            BmUpdateRunner.Instance.Add(this);
            return(this);
        }
Esempio n. 5
0
        /// <summary>
        ///     Sends a message to peer
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <param name="deliveryMethod">Delivery method</param>
        /// <returns></returns>
        public override void SendMessage(IMessage message, DeliveryMethod deliveryMethod)
        {
            if (!IsConnected)
            {
                return;
            }

            // TODO update this monstrosity
            var channelId = deliveryMethod == DeliveryMethod.Reliable
                ? _reliableChannel
                : deliveryMethod == DeliveryMethod.Unreliable ? _unreliableChannel : _reliableSequenced;

            var  bytes = message.ToBytes();
            byte error;

            NetworkTransport.Send(_socketId, _connectionId, channelId, bytes, bytes.Length, out error);
        }
        public void Update()
        {
            if (_socketId == -1)
            {
                return;
            }

            NetworkEventType networkEvent;

            do
            {
                int  connectionId;
                int  channelId;
                int  receivedSize;
                byte error;

                networkEvent = NetworkTransport.ReceiveFromHost(_socketId, out connectionId, out channelId, _msgBuffer,
                                                                _msgBuffer.Length, out receivedSize, out error);

                switch (networkEvent)
                {
                case NetworkEventType.ConnectEvent:
                    HandleConnect(connectionId, error);
                    break;

                case NetworkEventType.DataEvent:
                    HandleData(connectionId, channelId, receivedSize, error);
                    break;

                case NetworkEventType.DisconnectEvent:
                    HandleDisconnect(connectionId, error);
                    break;

                case NetworkEventType.Nothing:
                    break;

                default:
                    Logs.Error("Unknown network message type received: " + networkEvent);
                    break;
                }
            } while (networkEvent != NetworkEventType.Nothing);
        }
        public void Update()
        {
            if (_socketId == -1)
            {
                return;
            }

            byte error;

            if (IsConnecting && !IsConnected)
            {
                // Try connecting

                if (Environment.TickCount > _stopConnectingTick)
                {
                    // Timeout reached
                    StopConnecting();
                    return;
                }

                Status = ConnectionStatus.Connecting;

                if (!_isConnectionPending)
                {
                    // TODO Finish implementing multiple connects
                    _connectionId = NetworkTransport.Connect(_socketId, _ip, _port, 0, out error);

                    _isConnectionPending = true;

                    if (error != (int)UnityEngine.Networking.NetworkError.Ok)
                    {
                        StopConnecting();
                        return;
                    }
                }
            }

            NetworkEventType networkEvent;

            do
            {
                int connectionId;
                int channelId;
                int receivedSize;

                if (_socketId == -1)// EMIL Fix
                {
                    break;
                }

                networkEvent = NetworkTransport.ReceiveFromHost(_socketId, out connectionId, out channelId, _msgBuffer,
                                                                _msgBuffer.Length, out receivedSize, out error);

                switch (networkEvent)
                {
                case NetworkEventType.ConnectEvent:
                    HandleConnect(connectionId, error);
                    break;

                case NetworkEventType.DataEvent:
                    HandleData(connectionId, channelId, receivedSize, error);
                    break;

                case NetworkEventType.DisconnectEvent:
                    Logs.Debug("Disconnect event!");

                    HandleDisconnect(connectionId, error);
                    break;

                case NetworkEventType.Nothing:
                    break;

                default:
                    Logs.Error("Unknown network message type received: " + networkEvent);
                    break;
                }
            } while (networkEvent != NetworkEventType.Nothing);
        }
Esempio n. 8
0
        /// <summary>
        ///     Force disconnect
        /// </summary>
        /// <param name="reason"></param>
        public override void Disconnect(string reason)
        {
            byte error;

            NetworkTransport.Disconnect(_socketId, _connectionId, out error);
        }