private void IncomingConnectedAccepted(AsyncResultEventArgs <ServerChildTcpSocket> args) { if (args.Error != null) { string dumpPath = CoreManager.ServerCore.DumpException(args.Error); CoreManager.ServerCore.ConsoleManager.Error("Game Socket Manager", "Incoming connection failed!!"); CoreManager.ServerCore.ConsoleManager.Error("Game Socket Manager", " An exception dump has been saved to " + dumpPath); _listeningSocket.AcceptAsync(); return; } ServerChildTcpSocket internalSocket = args.Result; GameSocket socket = new GameSocket(internalSocket, this); GameSocketEventArgs eventArgs = new GameSocketEventArgs(socket); CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection:before", eventArgs); if (eventArgs.IsCancelled) { socket.Disconnect("Connection rejected from " + internalSocket.RemoteEndPoint + "(" + eventArgs.CancelReason + ")"); return; } socket.Start(); CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection:after", eventArgs); CoreManager.ServerCore.ConsoleManager.Notice("Game Socket Manager", "Incoming connection accepted: " + internalSocket.RemoteEndPoint); _listeningSocket.AcceptAsync(); }
private void IncomingConnectedAccepted(AsyncResultEventArgs <ServerChildTcpSocket> args) { if (args.Error != null) { // TODO: Die safely? CoreManager.ServerCore.StandardOut.Error("Game Socket Manager", "Incoming connection failed!!"); // TODO: Pretty exception reporting Console.WriteLine(); Console.WriteLine(args.Error.Message); Console.WriteLine(args.Error.StackTrace); _listeningSocket.AcceptAsync(); return; } ServerChildTcpSocket internalSocket = args.Result; GameSocket socket = new GameSocket(internalSocket, Protocol); CancelReasonEventArgs connectionEventArgs = new CancelReasonEventArgs(); CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection", EventPriority.Before, socket, connectionEventArgs); if (connectionEventArgs.Cancel) { socket.Disconnect("Connection rejected from " + internalSocket.RemoteEndPoint + "(" + connectionEventArgs.CancelReason + ")"); return; } socket.Start(); CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection", EventPriority.After, socket, connectionEventArgs); CoreManager.ServerCore.StandardOut.Notice("Game Socket Manager", "Incoming connection accepted: " + internalSocket.RemoteEndPoint); _listeningSocket.AcceptAsync(); }
private void buttonStart_Click(object sender, EventArgs e) { // Read the port number int port; if (!int.TryParse(textBoxPort.Text, out port)) { MessageBox.Show("Invalid port number: " + textBoxPort.Text); textBoxPort.Focus(); return; } try { // Define the socket, bind to the port, and start accepting connections ListeningSocket = new ServerTcpSocket(); ListeningSocket.AcceptCompleted += ListeningSocket_AcceptCompleted; ListeningSocket.Bind(port); ListeningSocket.AcceptAsync(); textBoxLog.AppendText("Listening on port " + port.ToString() + Environment.NewLine); } catch (Exception ex) { ResetListeningSocket(); textBoxLog.AppendText("Error creating listening socket on port " + port.ToString() + ": [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine); } RefreshDisplay(); }
private void ListeningSocket_AcceptCompleted(AsyncResultEventArgs <ServerChildTcpSocket> 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; } // Always continue listening for other connections ListeningSocket.AcceptAsync(); ServerChildTcpSocket socket = e.Result; try { // Save the new child socket connection, and create a packetizer for it SocketPacketProtocol protocol = new SocketPacketProtocol(socket); ChildSocketContext context = new ChildSocketContext(); context.Protocol = protocol; context.State = ChildSocketState.Connected; ChildSockets.Add(socket, context); protocol.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); // Start reading data from the connection protocol.Start(); } catch (Exception ex) { ResetChildSocket(socket); textBoxLog.AppendText("Socket error accepting connection: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine); } finally { RefreshDisplay(); } }
public GameSocketManager Start() { _actionThread = new ActionThread { Name = "IHI-GameSocketThread" }; _actionThread.Start(); _actionThread.Do(() => { _listeningSocket = new ServerTcpSocket(); _listeningSocket.AcceptCompleted += IncomingConnectedAccepted; _listeningSocket.Bind(Address, Port); _listeningSocket.AcceptAsync(); }); return(this); }