public static int SendMessage(string msg) { if (msg.ToLower() == "q") { client.Close(); System.Environment.Exit(0); // end life } PacketIO.Send(client, new Message(msg)); return(0); }
public static int BroadcastMsg(Message m) { clientlist.RemoveAll(ClientAintValid); // collect dead clients foreach (Client c in clientlist) { if (ClientAintValid(c)) { continue; } PacketIO.Send(c.socket, m); } return(0); }
private void ClientMain() { Console.WriteLine("Client with ID " + clientID + " thread started"); callback(new Message(Commands.Connection, username + " has joined", "", 0)); try { while (true) { Message message = PacketIO.Receive(socket); if (message.command != (byte)Commands.Message) { continue; } string msg = message.message; Console.WriteLine("\t(" + clientID + ") " + username + ": " + msg); if (msg.Length > 1 && msg[0] == '/') { string[] argv = msg.Substring(1).Split(' '); int argc = argv.Count(); string command = argv[0].ToLower(); if (command == "setusername" && argc > 1) { this.username = argv[1]; PacketIO.Send(socket, new Message(Commands.System, "Username updated", "", 0)); } else { PacketIO.Send(socket, new Message(Commands.System, "Command error!", "", 0)); } } else { // Broadcast D O N G callback(new Message(msg, username)); } } } catch (Exception e) { Console.Beep(); Console.WriteLine("Thread " + clientID + ": " + e.Message); } Console.WriteLine("Client with ID " + clientID + " thread ended"); callback(new Message(Commands.Connection, username + " has left", "", 0)); }
//void Recieved(IAsyncResult ar) { // var recieved = (ar.AsyncState as Result); // var stream = recieved.Stream; // var buffer = recieved.Buffer; // var bytesRead = stream.EndRead(ar); // _recieveBuffer.Add(buffer); // _recieveLock.Set(); //} public void Send(byte[] data) { try { if (!_client.Connected) { _connected = false; } else { var stream = _client.GetStream(); _packetIO.Send(data); //Console.WriteLine("Sending " + data.Length + " bytes"); //for (int i = 0; i < data.Length; i += CHUNK_SIZE) { // // If the size of a chunk is anything other than // // CHUNK_SIZE the reciever will know it's the end // // of the message. // var currentChunkSize = data.Length - i; // var writeSize = Math.Min(CHUNK_SIZE, currentChunkSize); // if (currentChunkSize == CHUNK_SIZE + 1) // writeSize = currentChunkSize; // var chunk = Slice(data, i, writeSize); // Console.WriteLine("Sending CHUNK of " + chunk.Length + " bytes"); // var lengthByte = BitConverter.GetBytes((short)chunk.Length)[0]; // stream.WriteByte(lengthByte); // Console.WriteLine("Bytes " + chunk.Length); // stream.WriteAsync(chunk); //} } } catch (IOException) { _connected = false; } }
static void StartServer(Config cfg) { TcpListener listener = new TcpListener(System.Net.IPAddress.Any, cfg.port); listener.Start(); int clients = 0; for (; ;) { Console.WriteLine("Waiting for new connection..."); TcpClient client = listener.AcceptTcpClient(); // wait until a client appears Client connection = new Client(); clientlist.Add(connection); connection.Start(client, clients++, BroadcastMsg); // Send MOTD foreach (string str in cfg.MOTD.Split('\n')) { PacketIO.Send(connection.socket, new Message(Commands.System, str, "MOTD", 0)); } } }