Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from kerryjiang/SuperSocket
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (1)
master
v1.6.4
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
Tags (1)
master
v1.6.4
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (1)
master
v1.6.4
Program.cs 12.21 KB
Copy Edit Raw Blame History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.ServiceProcess;
using System.Text;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketEngine.Configuration;
namespace SuperSocket.SocketService
{
static partial class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
//If this application run in Mono/Linux, change the control script to be executable
if(Platform.IsMono && Path.DirectorySeparatorChar == '/')
ChangeScriptExecutable();
if ((!Platform.IsMono && !Environment.UserInteractive)//Windows Service
|| (Platform.IsMono && !AppDomain.CurrentDomain.FriendlyName.Equals(Path.GetFileName(Assembly.GetEntryAssembly().CodeBase))))//MonoService
{
RunAsService();
return;
}
string exeArg = string.Empty;
if (args == null || args.Length < 1)
{
Console.WriteLine("Welcome to SuperSocket SocketService!");
Console.WriteLine("Please press a key to continue...");
Console.WriteLine("-[r]: Run this application as a console application;");
Console.WriteLine("-[i]: Install this application as a Windows Service;");
Console.WriteLine("-[u]: Uninstall this Windows Service application;");
while (true)
{
exeArg = Console.ReadKey().KeyChar.ToString();
Console.WriteLine();
if (Run(exeArg, null))
break;
}
}
else
{
exeArg = args[0];
if (!string.IsNullOrEmpty(exeArg))
exeArg = exeArg.TrimStart('-');
Run(exeArg, args);
}
}
static void ChangeScriptExecutable()
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "supersocket.sh");
try
{
if (!File.Exists(filePath))
return;
File.SetAttributes(filePath, (FileAttributes)((uint)File.GetAttributes(filePath) | 0x80000000));
}
catch
{
}
}
private static bool Run(string exeArg, string[] startArgs)
{
switch (exeArg.ToLower())
{
case ("i"):
SelfInstaller.InstallMe();
return true;
case ("u"):
SelfInstaller.UninstallMe();
return true;
case ("r"):
RunAsConsole();
return true;
case ("c"):
RunAsController(startArgs);
return true;
default:
Console.WriteLine("Invalid argument!");
return false;
}
}
private static bool setConsoleColor;
static void CheckCanSetConsoleColor()
{
try
{
Console.ForegroundColor = ConsoleColor.Green;
Console.ResetColor();
setConsoleColor = true;
}
catch
{
setConsoleColor = false;
}
}
private static void SetConsoleColor(ConsoleColor color)
{
if (setConsoleColor)
Console.ForegroundColor = color;
}
private static Dictionary<string, ControlCommand> m_CommandHandlers = new Dictionary<string, ControlCommand>(StringComparer.OrdinalIgnoreCase);
private static void AddCommand(string name, string description, Func<IBootstrap, string[], bool> handler)
{
var command = new ControlCommand
{
Name = name,
Description = description,
Handler = handler
};
m_CommandHandlers.Add(command.Name, command);
}
static void RunAsConsole()
{
Console.WriteLine("Welcome to SuperSocket SocketService!");
CheckCanSetConsoleColor();
Console.WriteLine("Initializing...");
IBootstrap bootstrap = BootstrapFactory.CreateBootstrap();
if (!bootstrap.Initialize())
{
SetConsoleColor(ConsoleColor.Red);
Console.WriteLine("Failed to initialize SuperSocket ServiceEngine! Please check error log for more information!");
Console.ReadKey();
return;
}
Console.WriteLine("Starting...");
var result = bootstrap.Start();
Console.WriteLine("-------------------------------------------------------------------");
foreach (var server in bootstrap.AppServers)
{
if (server.State == ServerState.Running)
{
SetConsoleColor(ConsoleColor.Green);
Console.WriteLine("- {0} has been started", server.Name);
}
else
{
SetConsoleColor(ConsoleColor.Red);
Console.WriteLine("- {0} failed to start", server.Name);
}
}
Console.ResetColor();
Console.WriteLine("-------------------------------------------------------------------");
switch(result)
{
case(StartResult.None):
SetConsoleColor(ConsoleColor.Red);
Console.WriteLine("No server is configured, please check you configuration!");
Console.ReadKey();
return;
case(StartResult.Success):
Console.WriteLine("The SuperSocket ServiceEngine has been started!");
break;
case (StartResult.Failed):
SetConsoleColor(ConsoleColor.Red);
Console.WriteLine("Failed to start the SuperSocket ServiceEngine! Please check error log for more information!");
Console.ReadKey();
return;
case (StartResult.PartialSuccess):
SetConsoleColor(ConsoleColor.Red);
Console.WriteLine("Some server instances were started successfully, but the others failed! Please check error log for more information!");
break;
}
Console.ResetColor();
Console.WriteLine("Enter key 'quit' to stop the ServiceEngine.");
RegisterCommands();
ReadConsoleCommand(bootstrap);
bootstrap.Stop();
Console.WriteLine("The SuperSocket ServiceEngine has been stopped!");
}
private static void RegisterCommands()
{
AddCommand("List", "List all server instances", ListCommand);
AddCommand("Start", "Start a server instance: Start {ServerName}", StartCommand);
AddCommand("Stop", "Stop a server instance: Stop {ServerName}", StopCommand);
}
private static void RunAsController(string[] arguments)
{
if (arguments == null || arguments.Length < 2)
{
Console.WriteLine("Invalid arguments!");
return;
}
var config = ConfigurationManager.GetSection("superSocket") as IConfigurationSource;
if (config == null)
{
Console.WriteLine("SuperSocket configiration is required!");
return;
}
var clientChannel = new IpcClientChannel();
ChannelServices.RegisterChannel(clientChannel, false);
IBootstrap bootstrap = null;
try
{
var remoteBootstrapUri = string.Format("ipc://SuperSocket.Bootstrap[{0}]/Bootstrap.rem", Math.Abs(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar).GetHashCode()));
bootstrap = (IBootstrap)Activator.GetObject(typeof(IBootstrap), remoteBootstrapUri);
}
catch (RemotingException)
{
if (config.Isolation != IsolationMode.Process)
{
Console.WriteLine("Error: the SuperSocket engine has not been started!");
return;
}
}
RegisterCommands();
var cmdName = arguments[1];
ControlCommand cmd;
if (!m_CommandHandlers.TryGetValue(cmdName, out cmd))
{
Console.WriteLine("Unknown command");
return;
}
try
{
if (cmd.Handler(bootstrap, arguments.Skip(1).ToArray()))
Console.WriteLine("Ok");
}
catch (Exception e)
{
Console.WriteLine("Failed. " + e.Message);
}
}
static bool ListCommand(IBootstrap bootstrap, string[] arguments)
{
foreach (var s in bootstrap.AppServers)
{
var processInfo = s as IProcessServer;
if (processInfo != null && processInfo.ProcessId > 0)
Console.WriteLine("{0}[PID:{1}] - {2}", s.Name, processInfo.ProcessId, s.State);
else
Console.WriteLine("{0} - {1}", s.Name, s.State);
}
return false;
}
static bool StopCommand(IBootstrap bootstrap, string[] arguments)
{
var name = arguments[1];
if (string.IsNullOrEmpty(name))
{
Console.WriteLine("Server name is required!");
return false;
}
var server = bootstrap.AppServers.FirstOrDefault(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (server == null)
{
Console.WriteLine("The server was not found!");
return false;
}
server.Stop();
return true;
}
static bool StartCommand(IBootstrap bootstrap, string[] arguments)
{
var name = arguments[1];
if (string.IsNullOrEmpty(name))
{
Console.WriteLine("Server name is required!");
return false;
}
var server = bootstrap.AppServers.FirstOrDefault(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (server == null)
{
Console.WriteLine("The server was not found!");
return false;
}
server.Start();
return true;
}
static void ReadConsoleCommand(IBootstrap bootstrap)
{
var line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
{
ReadConsoleCommand(bootstrap);
return;
}
if ("quit".Equals(line, StringComparison.OrdinalIgnoreCase))
return;
var cmdArray = line.Split(' ');
ControlCommand cmd;
if (!m_CommandHandlers.TryGetValue(cmdArray[0], out cmd))
{
Console.WriteLine("Unknown command");
ReadConsoleCommand(bootstrap);
return;
}
try
{
if(cmd.Handler(bootstrap, cmdArray))
Console.WriteLine("Ok");
}
catch (Exception e)
{
Console.WriteLine("Failed. " + e.Message + Environment.NewLine + e.StackTrace);
}
ReadConsoleCommand(bootstrap);
}
static void RunAsService()
{
ServiceBase[] servicesToRun;
servicesToRun = new ServiceBase[] { new MainService() };
ServiceBase.Run(servicesToRun);
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

SuperSocket 是一个轻量级的可扩展的 Socket 开发框架,可用来构建一个服务器端 Socket 程序,而无需了解如何使用 Socket,如何维护Socket连接,Socket是如何工作的。该项目使用纯 C# 开发,易于扩展和集成到已有的项目。只要你的已有系统是使用.NET开发的,你都能够使用 SuperSocket来轻易的开发出你需要的Socket应用程序来集成到你的现有系统之中。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/program4fun/supersocket.git
git@gitee.com:program4fun/supersocket.git
program4fun
supersocket
SuperSocket
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /