Exemple #1
0
    // worker thread
    void OnProxyConnected(networking.Chan chan)
    {
        chan.recvHandler += HandleProxyRecv;
#if UNITY_EDITOR
        chan.recvHandler += OnStatProxyRecv;
        chan.onSend      += OnStatProxySend;
#endif
        utils.TaskManager.PerformOnMainThread(
            (obj) =>
        {
            onProxyConnected.Invoke(chan);
        });
    }
Exemple #2
0
    // called in worker thread
    void OnConnected(networking.Chan chan)
    {
        var sess = new Session();

        chan.recvHandler += sess.HandleRecv;
#if UNITY_EDITOR
        chan.recvHandler += OnStatRecv;
        chan.onSend      += OnStatSend;
#endif
        utils.TaskManager.PerformOnMainThread(
            (obj) =>
        {
            var id  = (long)onConnected.Invoke1(chan);
            sess.id = (int)id;
            sessions.Add(sess);
        });
    }
Exemple #3
0
    // called in worker thread
    void OnConnected(networking.Chan chan)
    {
        chan.recvHandler += HandleRecv;
#if UNITY_EDITOR
        chan.recvHandler += OnStatRecv;
        chan.onSend      += OnStatSend;
#endif
        utils.TaskManager.PerformOnMainThread(
            (obj) => {
            Debug.Assert(onConnected != null);
            onConnected.Invoke(chan);

#if UNITY_EDITOR
            bool left = false;
            if (rect.x < Screen.width * 0.5f)
            {
                left = true;
            }
            float x;
            var width = 200;
            if (left)
            {
                x = rect.width + 10;
            }
            else
            {
                x = rect.x - 10 - width;
            }
            var y      = rect.y;
            var height = 80;
            Editor_AddGraph_Native(
                "client_send", "kbps", GetSendBandwidth, 10, 0.5f,
                x, y, width, height, Color.red);

            y += height + 5;
            Editor_AddGraph_Native(
                "client_recv", "kbps", GetRecvBandwidth, 10, 0.5f,
                x, y, width, height, Color.blue);
#endif
        });
    }
Exemple #4
0
        static void ConnectCallback(IAsyncResult ar)
        {
            var s = (ConnStateObject)ar.AsyncState;

            try
            {
                s.workSocket.EndConnect(ar);
                if (s.workSocket.Connected)
                {
                    var chan = new Chan(
                        (data) => {
                        SocketSendingAndReceiving.Send(s.workSocket, data);
                    });
                    s.onConnected(chan);
                    SocketSendingAndReceiving.BeginReceive(
                        s.workSocket,
                        (data, bytesReceived) =>
                    {
                        if (bytesReceived > 0)
                        {
                            chan.HandleRecv(data, bytesReceived);
                        }
                        else
                        {
                            chan.Break(Chan.BreakReason.ReceiveZero);
                        }
                    });
                }
                else
                {
                    Debug.Log("Client not connected!");
                    s.onConnected(null);
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Exception on connecting callback " + e.Message);
                s.onConnected(null);
            }
        }
Exemple #5
0
        static void AcceptCallback(IAsyncResult ar)
        {
            allDone.Set();
            var    s       = (AcceptStateObject)ar.AsyncState;
            Socket handler = null;

            lock (socketLock)
            {
                if (socket != null)
                {
                    handler = socket.EndAccept(ar);
                }
            }
            if (handler == null)
            {
                s.onConnected(null);
                return;
            }
            Debug.Log("Client connectioned");
            var chan = new Chan(
                (data) => {
                SocketSendingAndReceiving.Send(handler, data);
            });

            s.onConnected(chan);
            SocketSendingAndReceiving.BeginReceive(
                handler,
                (data, bytesReceived) =>
            {
                if (bytesReceived > 0)
                {
                    chan.HandleRecv(data, bytesReceived);
                }
                else
                {
                    chan.Break(Chan.BreakReason.ReceiveZero);
                }
            });
        }