/// <summary> /// Writes the provided message to the console window /// </summary> /// <param name="header">The packet header associated with the incoming message</param> /// <param name="connection">The connection used by the incoming message</param> /// <param name="message">The message to be printed to the console</param> private void PrintIncomingMessage(PacketHeader header, Connection connection, string message) { System.Windows.Forms.MessageBox.Show("\nA message was received from " + connection.ToString() + " which said '" + message + "'."); //richTextBox1.AppendText("\nA message was received from " + connection.ToString() + " which said '" + message + "'."); }
/// <summary> /// Handle the message receive /// </summary> /// <param name="packetheader">The packet header associated with the incoming message</param> /// <param name="connection">The connection used by the incoming message</param> /// <param name="incomingobject">The message received</param> private static void GetConnect(PacketHeader packetheader, Connection connection, int incomingobject) { //as soon as players are connected, they are assignd with their player number GetPlayers(connection); }
private static void HandleGuess(PacketHeader packetheader, Connection connection, string[] guess) { //the server checks the guess from client and send correct positions to client (player 1) int[] cp = _game.CheckGuess(guess, _secretCombination, _numColumns); NetworkComms.SendObject("CheckGuess", _destinationIpAddress1, _destinationPort1, cp); }
private void AddFileData(PacketHeader header, Connection connection, byte[] data) { try { SendInfo info = null; ReceivedFile file = null; lock (syncRoot) { long sequenceNumber = header.GetOption(PacketHeaderLongItems.PacketSequenceNumber); if (incomingDataInfoCache.ContainsKey(connection.ConnectionInfo) && incomingDataInfoCache[connection.ConnectionInfo].ContainsKey(sequenceNumber)) { info = incomingDataInfoCache[connection.ConnectionInfo][sequenceNumber]; incomingDataInfoCache[connection.ConnectionInfo].Remove(sequenceNumber); if (!receivedFilesDict.ContainsKey(connection.ConnectionInfo)) { receivedFilesDict.Add(connection.ConnectionInfo, new Dictionary <string, ReceivedFile>()); } if (!receivedFilesDict[connection.ConnectionInfo].ContainsKey(info.Filename)) { receivedFilesDict[connection.ConnectionInfo].Add(info.Filename, new ReceivedFile(info.Filename, connection.ConnectionInfo, info.TotalBytes)); AddNewReceivedItem(receivedFilesDict[connection.ConnectionInfo][info.Filename]); } file = receivedFilesDict[connection.ConnectionInfo][info.Filename]; Console.WriteLine("File was received " + info.Filename); } else { if (!incomingDataCache.ContainsKey(connection.ConnectionInfo)) { incomingDataCache.Add(connection.ConnectionInfo, new Dictionary <long, byte[]>()); } incomingDataCache[connection.ConnectionInfo].Add(sequenceNumber, data); } } //If we have everything we need we can add data to the ReceivedFile if (info != null && file != null && !file.IsCompleted) { file.AddData(info.BytesStart, 0, data.Length, data); Console.WriteLine("Received: " + file.CompletedPercent + "%"); //Perform a little clean-up file = null; data = null; GC.Collect(); } else if (info == null ^ file == null) { throw new Exception("Either both are null or both are set. Info is " + (info == null ? "null." : "set.") + " File is " + (file == null ? "null." : "set.") + " File is " + (file.IsCompleted ? "completed." : "not completed.")); } } catch (Exception ex) { //If an exception occurs we write to the log window and also create an error file LogTools.LogException(ex, "IncomingPartialFileDataError"); } }
private static void GetMode(PacketHeader header, Connection connection, int modeClient) { _mode = modeClient; }
private void IncomingPartialFileData(PacketHeader header, Connection connection, byte[] data) { AddFileData(header, connection, data); SaveFile(); }
private void IncomingPartialFileDataInfo(PacketHeader header, Connection connection, SendInfo info) { AddFileInfo(header, connection, info); SaveFile(); }
public LogOutEventArgs(PacketHeader header, LogOutPacket body) : base(header.UserId, header.SequenceNumber) { }
/// <summary> /// Writes the provided message to the console window /// </summary> /// <param name="header">The packet header associated with the incoming message</param> /// <param name="connection">The connection used by the incoming message</param> /// <param name="message">The message to be printed to the console</param> private static void PrintIncomingMessage(PacketHeader header, Connection connection, string message) { Console.WriteLine("\nA message was received from " + connection.ToString() + " which said '" + message + "'."); }
public LogOnEventArgs(PacketHeader header, LogOnPacket body, TcpClient client) : base(header.UserId, header.SequenceNumber) { Client = client; }
public HeartBeatEventArgs(PacketHeader header, HeartBeatPacket body) : base(header.UserId, header.SequenceNumber) { }
public ChatEventArgs(PacketHeader header, ChatPacket body) : base(header.UserId, header.SequenceNumber) { DestinationUserId = body.DestinationId; ChatContent = body.ChatContent; }
private void ReadIncommingCommand(PacketHeader header, Connection connection, string message) { Console.WriteLine("\nA message was received from " + connection.ToString() + " which said '" + message + "'."); CreateSendObject(message); connection.SendObject("Message", toSendObject); }
/** * Triggered when the client asked to connect. * @param header Infos about the header. * @param connection Infos about the client connection. * @param message The name of the user. */ public void Connection(PacketHeader header, Connection connection, string message) { bool connect = false; var ip = connection.ConnectionInfo.RemoteEndPoint.ToString().Split(':')[0]; var port = int.Parse(connection.ConnectionInfo.RemoteEndPoint.ToString().Split(':')[1]); int id = 0; Server.Instance.PrintOnDebug("A new player try to connect " + ip + " " + port + " " + id + " " + message); if (Server.Instance.players.list.Count() < 4) { id = Server.Instance.players.list.Count(); Profile newDeck = new Profile(message, id, ip, port); Server.Instance.players.list.Add(newDeck); connect = true; } else { foreach (var entry in Server.Instance.players.list) { if (entry.ip == ip && entry.port == port && entry.status == PLAYER_STATUS.OFFLINE) { id = entry.id; Server.Instance.players.list[entry.id].status = PLAYER_STATUS.ONLINE; connect = true; break; } } foreach (var entry in Server.Instance.players.list) { if (entry.status == PLAYER_STATUS.OFFLINE) { id = entry.id; Server.Instance.players.list[entry.id].ip = ip; Server.Instance.players.list[entry.id].port = port; Server.Instance.players.list[entry.id].status = PLAYER_STATUS.ONLINE; Server.Instance.WriteToOther("031", ip, port, Server.Instance.serializer.ObjectToString(new PlayerRename(entry.id, Server.Instance.players.list[entry.id].owner, message))); Server.Instance.players.list[entry.id].owner = message; connect = true; break; } } } if (connect == true) { Server.Instance.PrintOnDebug("A new player just connect " + ip + " " + port + " " + id); Server.Instance.WriteTo("230", ip, port, id.ToString() + ":" + message); Server.Instance.WriteTo("011", ip, port, "Waiting for players"); List <string> list = new List <string>(); foreach (var it in Server.Instance.players.list) { if (it.id != id) { list.Add(it.id + ":" + it.owner); } } Server.Instance.WriteTo("030", ip, port, Server.Instance.serializer.ObjectToString(list)); list.Clear(); list.Add(id + ":" + message); Server.Instance.WriteToOther("030", ip, port, Server.Instance.serializer.ObjectToString(list)); } else { Server.Instance.WriteTo("330", ip, port, "Sorry, there are too many client which are already connected"); } }