Пример #1
0
        private void HandleSocketServerNotification(ServiceNotification notification, ServiceStatus status,
                                                    SocketServer.ServerReplyContext serverReplyContext, String msg)
        {
            String s = "";

            switch (notification)
            {
            case ServiceNotification.ReceivedData:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = String.Format("Server: Received from Client #{0} at {1}: {2}",
                                  serverReplyContext.ClientNumber, serverReplyContext.Socket.RemoteEndPoint, msg);
                AddLogEntry(s);
                ReceivedData(serverReplyContext, msg);
                return;

            case ServiceNotification.Write:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = String.Format("Wrote to Client #{0} at {1}: {2}",
                                  serverReplyContext.ClientNumber, serverReplyContext.Socket.RemoteEndPoint, msg);
                break;

            case ServiceNotification.WriteFailed:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = String.Format("Write failed to Client #{0} at {1}: {2}",
                                  serverReplyContext.ClientNumber, serverReplyContext.Socket.RemoteEndPoint, msg);
                break;

            case ServiceNotification.ClientConnected:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = String.Format("Client #{0} at {1} connected.",
                                  serverReplyContext.ClientNumber, serverReplyContext.Socket.RemoteEndPoint);
                break;

            case ServiceNotification.ClientDisconnected:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = String.Format("Client #{0} at {1} has disconnected.",
                                  serverReplyContext.ClientNumber, serverReplyContext.Socket.RemoteEndPoint);
                break;

            case ServiceNotification.Wakeup:
                s = "Wakeup: " + (string)msg;
                break;

            case ServiceNotification.Error:
                switch (status)
                {
                case ServiceStatus.Waiting:
                case ServiceStatus.Stopped:
                case ServiceStatus.Sleeping:
                    s = String.Format("{0}: {1}", status, msg);
                    break;

                case ServiceStatus.Connected:
                    if (serverReplyContext != null)
                    {
                        Debug.Assert(serverReplyContext.Socket != null);
                        Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null);
                        s = String.Format("(Client #{0} at {1}): {2}",
                                          serverReplyContext.ClientNumber,
                                          serverReplyContext.Socket == null
                                        ? "n/a"
                                        : serverReplyContext.Socket.RemoteEndPoint.ToString(),
                                          msg);
                    }
                    else
                    {
                        s = msg;
                    }
                    break;
                }
                s = "Error " + s;
                break;

            default:
                s = "Unknown notification: " + notification;
                break;
            }
            AddLogEntry("Server: " + s);
        }
Пример #2
0
        private void HandleSocketServerNotification(ServiceNotification notification, ServiceStatus status,
                                                    SocketServer.ServerReplyContext serverReplyContext, String msg)
        {
            var s = "";

            switch (notification)
            {
            case ServiceNotification.ReceivedData:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = $"SocketServer: Received from Client #{serverReplyContext.ClientNumber} at {serverReplyContext.Socket.RemoteEndPoint}: {msg}";
                Logger.Instance.Log4.Info(s);
                ReceivedData(serverReplyContext, msg);
                return;

            case ServiceNotification.Write:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = $"Wrote to Client #{serverReplyContext.ClientNumber} at {serverReplyContext.Socket.RemoteEndPoint}: {msg}";
                break;

            case ServiceNotification.WriteFailed:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = $"Write failed to Client #{serverReplyContext.ClientNumber} at {serverReplyContext.Socket.RemoteEndPoint}: {msg}";
                break;

            case ServiceNotification.ClientConnected:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = $"Client #{serverReplyContext.ClientNumber} at {serverReplyContext.Socket.RemoteEndPoint} connected";
                break;

            case ServiceNotification.ClientDisconnected:
                Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null, notification.ToString());
                s = $"Client #{serverReplyContext.ClientNumber} at {serverReplyContext.Socket.RemoteEndPoint} has disconnected";
                break;

            case ServiceNotification.Wakeup:
                s = $"Wakeup: {(string)msg}";
                break;

            case ServiceNotification.Error:
                switch (status)
                {
                case ServiceStatus.Waiting:
                case ServiceStatus.Stopped:
                case ServiceStatus.Sleeping:
                    s = $"({status}): {msg}";
                    break;

                case ServiceStatus.Connected:
                    if (serverReplyContext != null)
                    {
                        Debug.Assert(serverReplyContext.Socket != null);
                        Debug.Assert(serverReplyContext.Socket.RemoteEndPoint != null);
                        s = $"(Client #{serverReplyContext.ClientNumber} at {(serverReplyContext.Socket == null ? "n/a" : serverReplyContext.Socket.RemoteEndPoint.ToString())}): {msg}";
                    }
                    else
                    {
                        s = msg;
                    }

                    break;
                }
                s = "Error " + s;
                break;

            default:
                s = "Unknown notification: " + notification;
                break;
            }
            Logger.Instance.Log4.Info($"SocketServer: {s}");
        }