コード例 #1
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();
        }
コード例 #2
0
        private void ListeningSocket_ConnectionArrived(AsyncResultEventArgs <SimpleServerChildTcpSocket> e)
        {
            // Check for errors
            if (e.Error != null)
            {
                ResetListeningSocket();
                textBoxLog.AppendText("Socket error during Accept: [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                RefreshDisplay();
                return;
            }

            SimpleServerChildTcpSocket socket = e.Result;

            try
            {
                // Save the new child socket connection
                ChildSockets.Add(socket, ChildSocketState.Connected);

                socket.PacketArrived     += (args) => ChildSocket_PacketArrived(socket, args);
                socket.WriteCompleted    += (args) => ChildSocket_WriteCompleted(socket, args);
                socket.ShutdownCompleted += (args) => ChildSocket_ShutdownCompleted(socket, args);

                // Display the connection information
                textBoxLog.AppendText("Connection established to " + socket.RemoteEndPoint.ToString() + Environment.NewLine);
            }
            catch (Exception ex)
            {
                ResetChildSocket(socket);
                textBoxLog.AppendText("Socket error accepting connection: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
            }
            finally
            {
                RefreshDisplay();
            }
        }
コード例 #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);
        }
コード例 #4
0
        private void ChildSocket_PacketArrived(SimpleServerChildTcpSocket socket, AsyncResultEventArgs <byte[]> e)
        {
            try
            {
                // Check for errors
                if (e.Error != null)
                {
                    textBoxLog.AppendText("Client socket error during Read from " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                    ResetChildSocket(socket);
                }
                else if (e.Result == null)
                {
                    // PacketArrived completes with a null packet when the other side gracefully closes the connection
                    textBoxLog.AppendText("Socket graceful close detected from " + socket.RemoteEndPoint.ToString() + Environment.NewLine);

                    // 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 = Messages.Util.Deserialize(e.Result);

                    // Handle the message
                    Messages.StringMessage stringMessage = message as Messages.StringMessage;
                    if (stringMessage != null)
                    {
                        textBoxLog.AppendText("Socket read got a string message from " + socket.RemoteEndPoint.ToString() + ": " + stringMessage.Message + Environment.NewLine);
                        return;
                    }

                    Messages.ComplexMessage complexMessage = message as Messages.ComplexMessage;
                    if (complexMessage != null)
                    {
                        textBoxLog.AppendText("Socket read got a complex message from " + socket.RemoteEndPoint.ToString() + ": (UniqueID = " + complexMessage.UniqueID.ToString() +
                                              ", Time = " + complexMessage.Time.ToString() + ", Message = " + complexMessage.Message + ")" + Environment.NewLine);
                        return;
                    }

                    textBoxLog.AppendText("Socket read got an unknown message from " + socket.RemoteEndPoint.ToString() + " of type " + message.GetType().Name + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                textBoxLog.AppendText("Error reading from socket " + socket.RemoteEndPoint.ToString() + ": [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
                ResetChildSocket(socket);
            }
            finally
            {
                RefreshDisplay();
            }
        }
コード例 #5
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;
        }
コード例 #6
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();
            }
        }
コード例 #7
0
        private void ChildSocket_ShutdownCompleted(object sender, AsyncCompletedEventArgs e)
        {
            SimpleServerChildTcpSocket socket = (SimpleServerChildTcpSocket)sender;

            // Check for errors
            if (e.Error != null)
            {
                ConsoleService.Write("Socket error during Shutdown of " + socket.RemoteEndPoint + ": [" + e.Error.GetType().Name + "] " + e.Error.Message);
                ResetChildSocket(socket);
            }
            else
            {
                ConsoleService.Write("Socket shutdown completed on " + socket.RemoteEndPoint);

                // Close the socket and remove it from the list
                ResetChildSocket(socket);
            }
            if(OnClientRemovedHandler != null) OnClientRemovedHandler(this, new OnClientRemovedEventArgs(Server.Clients[socket]));
            RefreshDisplay();
        }
コード例 #8
0
        private void ChildSocket_ShutdownCompleted(object sender, AsyncCompletedEventArgs e)
        {
            SimpleServerChildTcpSocket socket = (SimpleServerChildTcpSocket)sender;

            // Check for errors
            if (e.Error != null)
            {
                textBoxLog.AppendText("Socket error during Shutdown of " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                ResetChildSocket(socket);
            }
            else
            {
                textBoxLog.AppendText("Socket shutdown completed on " + socket.RemoteEndPoint.ToString() + Environment.NewLine);

                // Close the socket and remove it from the list
                ResetChildSocket(socket);
            }

            RefreshDisplay();
        }
コード例 #9
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);
             }
         }
     }
 }
コード例 #10
0
        private void ListeningSocket_ConnectionArrived(AsyncResultEventArgs<SimpleServerChildTcpSocket> e)
        {
            // Check for errors
            if (e.Error != null)
            {
                ResetListeningSocket();
                ConsoleService.Write("Socket error during Accept: [" + e.Error.GetType().Name + "] " + e.Error.Message);
                RefreshDisplay();
                return;
            }

            SimpleServerChildTcpSocket socket = e.Result;

            try
            {
                // Save the new child socket connection
                Server.ChildSockets.Add(socket, ChildSocketState.Connected);
                Server.Clients.Add(socket, new ClientModel
                {
                    RemoteEndPoint = socket.RemoteEndPoint.ToString()
                });
                if (OnClientAddedHandler != null) OnClientAddedHandler(this, new OnClientAddedEventArgs(Server.Clients[socket]));

                socket.PacketArrived += args => ChildSocket_PacketArrived(socket, args);
                socket.WriteCompleted += args => ChildSocket_WriteCompleted(socket, args);
                socket.ShutdownCompleted += args => ChildSocket_ShutdownCompleted(socket, args);

                // Display the connection information
                ConsoleService.Write("Connection established to " + socket.RemoteEndPoint);
            }
            catch (Exception ex)
            {
                ResetChildSocket(socket);
                ConsoleService.Write("Socket error accepting connection: [" + ex.GetType().Name + "] " + ex.Message);
            }
            finally
            {
                RefreshDisplay();
            }
        }
コード例 #11
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();
        }
コード例 #12
0
        private void buttonDisconnect_Click(object sender, EventArgs e)
        {
            // Initiate a graceful disconnect for all clients
            SimpleServerChildTcpSocket[] children = new SimpleServerChildTcpSocket[ChildSockets.Keys.Count];
            ChildSockets.Keys.CopyTo(children, 0);
            foreach (SimpleServerChildTcpSocket child in children)
            {
                try
                {
                    child.ShutdownAsync();
                    ChildSockets[child] = ChildSocketState.Disconnecting;
                }
                catch (Exception ex)
                {
                    textBoxLog.AppendText("Child Socket error disconnecting from " + child.RemoteEndPoint.ToString() + ": [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
                    ResetChildSocket(child);
                }
            }

            // In case there were any errors, the display may need to be updated
            RefreshDisplay();
        }
コード例 #13
0
        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));
        }