protected override bool HandleMessage(object message, SimpleServerChildTcpSocket socket)
        {
            ChildSocketState socketState = ChildSocketState.Disconnecting;
            Server.ChildSockets.TryGetValue(socket, out socketState);
            KeyValuePair<SimpleServerChildTcpSocket, ChildSocketState> childSocket = new KeyValuePair<SimpleServerChildTcpSocket,ChildSocketState>(socket, socketState);

            PositionRequest positionRequest = message as PositionRequest;
            if (positionRequest != null)
            {
                string consoleString;
                Float3 position = Float3.Zero;

                if (positionRequest.Type != PositionType.Bundled)
                {
                    consoleString = String.Format("{0} requests {1} position of controller {2} from camera {3}.",
                        socket.RemoteEndPoint,
                        Enum.GetName(typeof(PositionType), positionRequest.Type),
                        positionRequest.ControllerIndex,
                        positionRequest.CameraIndex);
                }
                else
                {
                    consoleString = String.Format("{0} requests {1} position of controller {2}.",
                        socket.RemoteEndPoint,
                        Enum.GetName(typeof(PositionType), positionRequest.Type),
                        positionRequest.ControllerIndex);
                }

                switch (positionRequest.Type)
                {
                    case PositionType.Bundled:
                        position = GetBundledPosition(positionRequest.ControllerIndex);
                        break;
                    case PositionType.Camera:
                        break;
                    case PositionType.Fusion:
                        position = GetFusionPosition(positionRequest.CameraIndex, positionRequest.ControllerIndex);
                        break;
                    case PositionType.Raw:
                        break;
                    case PositionType.World:
                        position = GetWorldPosition(positionRequest.CameraIndex, positionRequest.ControllerIndex);
                        break;
                }

                ConsoleService.Write(consoleString);

                SendPositionMessage(childSocket, new PositionMessage()
                {
                    Position = position,
                    Type = positionRequest.Type,
                    StartTick = positionRequest.StartTick,
                    CameraIndex = positionRequest.CameraIndex,
                    ControllerIndex = positionRequest.CameraIndex
                });
                return true;
            }

            return base.HandleMessage(message, socket);
        }
Example #2
0
 public void Disconnect(ClientModel client)
 {
     SimpleServerChildTcpSocket[] children = new SimpleServerChildTcpSocket[Server.ChildSockets.Keys.Count];
     Server.ChildSockets.Keys.CopyTo(children, 0);
     foreach (SimpleServerChildTcpSocket child in children)
     {
         if (child.RemoteEndPoint.ToString().Equals(client.RemoteEndPoint))
         {
             try
             {
                 child.ShutdownAsync();
                 Server.ChildSockets[child] = ChildSocketState.Disconnecting;
             }
             catch (Exception ex)
             {
                 ConsoleService.Write("Child Socket error disconnecting from " + child.RemoteEndPoint + ": [" + ex.GetType().Name + "] " + ex.Message);
                 ResetChildSocket(child);
             }
         }
     }
 }
Example #3
0
        /// <summary>
        /// Closes and clears a child socket (established connection), without causing exceptions.
        /// </summary>
        /// <param name="childSocket">The child socket to close. May be null.</param>
        private void ResetChildSocket(SimpleServerChildTcpSocket childSocket)
        {
            // Close the child socket if possible
            if (childSocket != null)
                childSocket.Close();

            // Remove it from the list of child sockets
            Server.ChildSockets.Remove(childSocket);
        }
Example #4
0
        private void ChildSocket_WriteCompleted(SimpleServerChildTcpSocket socket, AsyncCompletedEventArgs e)
        {
            // Check for errors
            if (e.Error != null)
            {
                // Note: WriteCompleted may be called as the result of a normal write (SocketPacketizer.WritePacketAsync),
                //  or as the result of a call to SocketPacketizer.WriteKeepaliveAsync. However, WriteKeepaliveAsync
                //  will never invoke WriteCompleted if the write was successful; it will only invoke WriteCompleted if
                //  the keepalive packet failed (indicating a loss of connection).

                // If you want to get fancy, you can tell if the error is the result of a write failure or a keepalive
                //  failure by testing e.UserState, which is set by normal writes.
                if (e.UserState is string)
                    ConsoleService.Write("Socket error during Write to " + socket.RemoteEndPoint + ": [" + e.Error.GetType().Name + "] " + e.Error.Message);
                else
                    ConsoleService.Write("Socket error detected by keepalive to " + socket.RemoteEndPoint + ": [" + e.Error.GetType().Name + "] " + e.Error.Message);

                ResetChildSocket(socket);
            }
            else
            {
                string description = (string)e.UserState;
                ConsoleService.Write("Socket write completed to " + socket.RemoteEndPoint + " for message " + description);
            }

            RefreshDisplay();
        }
Example #5
0
        private void ChildSocket_PacketArrived(SimpleServerChildTcpSocket socket, AsyncResultEventArgs<byte[]> e)
        {
            try
            {
                // Check for errors
                if (e.Error != null)
                {
                    ConsoleService.Write("Client socket error during Read from " + socket.RemoteEndPoint + ": [" + e.Error.GetType().Name + "] " + e.Error.Message);
                    ResetChildSocket(socket);
                }
                else if (e.Result == null)
                {
                    // PacketArrived completes with a null packet when the other side gracefully closes the connection
                    ConsoleService.Write("Socket graceful close detected from " + socket.RemoteEndPoint);

                    // Close the socket and remove it from the list
                    ResetChildSocket(socket);
                }
                else
                {
                    // At this point, we know we actually got a message.

                    // Deserialize the message
                    object message = SerializationHelper.Deserialize(e.Result);

                    // Handle the message
                    if(HandleMessage(message, socket) == false)
                    {
                        ConsoleService.Write("Socket read got an unknown message from " + socket.RemoteEndPoint + " of type " + message.GetType().Name);
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleService.Write("Error reading from socket " + socket.RemoteEndPoint + ": [" + ex.GetType().Name + "] " + ex.Message);
                ResetChildSocket(socket);
            }
            finally
            {
                RefreshDisplay();
            }
        }
Example #6
0
        protected virtual bool HandleMessage(object message, SimpleServerChildTcpSocket socket)
        {
            StringMessage stringMessage = message as StringMessage;
            if (stringMessage != null)
            {
                ConsoleService.Write("Socket read got a string message from " + socket.RemoteEndPoint + ": " + stringMessage.Message);
                return true;
            }

            ComplexMessage complexMessage = message as ComplexMessage;
            if (complexMessage != null)
            {
                ConsoleService.Write("Socket read got a complex message from " + socket.RemoteEndPoint + ": (UniqueID = " + complexMessage.UniqueID +
                    ", Time = " + complexMessage.Time + ", Message = " + complexMessage.Message + ")");
                return true;
            }

            return false;
        }
Example #7
0
        public void Disconnect()
        {
            // Initiate a graceful disconnect for all clients
            SimpleServerChildTcpSocket[] children = new SimpleServerChildTcpSocket[Server.ChildSockets.Keys.Count];
            Server.ChildSockets.Keys.CopyTo(children, 0);
            foreach (SimpleServerChildTcpSocket child in children)
            {
                try
                {
                    child.ShutdownAsync();
                    Server.ChildSockets[child] = ChildSocketState.Disconnecting;
                }
                catch (Exception ex)
                {
                    ConsoleService.Write("Child Socket error disconnecting from " + child.RemoteEndPoint + ": [" + ex.GetType().Name + "] " + ex.Message);
                    ResetChildSocket(child);
                }
            }

            // In case there were any errors, the display may need to be updated
            RefreshDisplay();
        }