Пример #1
0
        public ClientConnection(int ID, Socket socket, VmServer server)
        {
            this.ID     = ID;
            this.socket = socket;
            this.server = server;
            if (debugClientConnection)
            {
                Debug.Log("ClientConnection.ClientConnection (" + Thread.CurrentThread.ManagedThreadId + "): "
                          + "Client " + ID + " has connected");
            }

            VmSocketState socketState = new VmSocketState(this);

            socket.BeginReceive(socketState.buffer, 0, VmNetworking.BUFFER_LENGTH, SocketFlags.None, OnReceiveFromClient, socketState);
        }
Пример #2
0
        private void OnConnect(IAsyncResult ar)
        {
            try
            {
                if (clientSocket == null || !clientSocket.Connected)
                {
                    Debug.Log("VmClient.OnConnect (" + Thread.CurrentThread.ManagedThreadId + "): "
                              + "server connection rejected because connection was shutdown or not started");
                    return;
                }

                clientSocket.EndConnect(ar);
                connected = true;

                VmSocketState socketState = new VmSocketState(this);
                clientSocket.BeginReceive(socketState.buffer, 0, VmNetworking.BUFFER_LENGTH, SocketFlags.None, OnReceiveFromServer, socketState);
            }
            catch (Exception ex)
            {
                Debug.LogError(ex);
            }
        }
Пример #3
0
        private void OnReceiveFromClient(IAsyncResult ar)
        {
            try
            {
                if (socket == null || !socket.Connected)
                {
                    Debug.Log("ClientConnection.OnReceiveFromClient (" + Thread.CurrentThread.ManagedThreadId + "): "
                              + "client message rejected because connection was shutdown or not started");
                    return;
                }

                int received = socket.EndReceive(ar);
                if (received == 0)
                {
                    Disconnect();
                    return;
                }

                if (debugClientConnection)
                {
                    Debug.Log("ClientConnection.OnReceiveFromClient (" + Thread.CurrentThread.ManagedThreadId + "): " + ID);
                }

                VmSocketState socketState = ar.AsyncState as VmSocketState;
                socketState.Receive(received, 0);

                if (socket != null && socket.Connected)
                {
                    // Should be able to use a mutex but unity doesn't seem to like it
                    socket.BeginReceive(socketState.buffer, 0, VmNetworking.BUFFER_LENGTH, SocketFlags.None, OnReceiveFromClient, socketState);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex);
            }
        }