public override void Send(MessageType msgType, NetworkStream stream, object options) { BinaryWriter writer = new BinaryWriter(stream); switch (msgType) { case MessageType.ConnectionAsk: { byte[] userTable = SimpleFormatters.ObjectToBytes(options); int len = userTable.Length; writer.Write(len); writer.Write(userTable); break; } case MessageType.ConnectionUserAdded: { byte[] userBytes = SimpleFormatters.ObjectToBytes(options); int len = userBytes.Length; writer.Write(len); writer.Write(userBytes); break; } case MessageType.ConnectionAccepted: case MessageType.LoggedIn: case MessageType.LoggedOut: case MessageType.IsAlive: { StreamWriter nickWriter = new StreamWriter(stream); nickWriter.WriteLine((string)options); nickWriter.Close(); break; } default: writer.Close(); break; } }
public override void Receive(MessageType msgType, NetworkStream stream, IPAddress owner) { BinaryReader reader = new BinaryReader(stream); StreamReader nickReader = new StreamReader(stream); switch (msgType) { case MessageType.ConnectionAsk: { int len = reader.ReadInt32(); byte[] userTable = reader.ReadBytes(len); User[] table = (User[])SimpleFormatters.BytesToObject(userTable); AskedConnection(this, new ReceivedMessageEventArgs(msgType, table, owner)); break; } case MessageType.ConnectionUserAdded: { int len = reader.ReadInt32(); byte[] userBytes = reader.ReadBytes(len); User newUser = (User)SimpleFormatters.BytesToObject(userBytes); NewUserAdded(this, new ReceivedMessageEventArgs(msgType, newUser, owner)); break; } case MessageType.ConnectionAccepted: { string nickname = nickReader.ReadLine(); AcceptedConnection(this, new ReceivedMessageEventArgs(msgType, nickname, owner)); break; } case MessageType.ConnectionCanceled: { string nickname = nickReader.ReadLine(); CanceledConnection(this, new ReceivedMessageEventArgs(msgType, nickname, owner)); break; } case MessageType.ConnectionClosed: { ClosedConnection(this, new ReceivedMessageEventArgs(msgType, owner)); break; } case MessageType.LoggedIn: { string nickname = nickReader.ReadLine(); UserLoggedIn(this, new ReceivedMessageEventArgs(msgType, nickname, owner)); break; } case MessageType.LoggedOut: { string nickname = nickReader.ReadLine(); UserLoggedOut(this, new ReceivedMessageEventArgs(msgType, nickname, owner)); break; } case MessageType.IsAlive: { string nickname = nickReader.ReadLine(); AliveUser(this, new ReceivedMessageEventArgs(msgType, nickname, owner)); break; } case MessageType.Echo: { Send(MessageType.IsAlive, stream, Program.localUser.Nickname); break; } default: break; } reader.Close(); nickReader.Close(); }
public override void Send(MessageType msgType, NetworkStream stream, object options) { BinaryWriter writer = new BinaryWriter(stream); switch (msgType) { case MessageType.FilesTableAvailable: { AvailableFiles.Clear(); AvailableFiles.AddRange((string[])options); List <string> temp = new List <string>(); string[] files = (string[])options; foreach (string file in files) { FileInfo f = new FileInfo(file); long size = f.Length; temp.Add(String.Concat(file, " ", size.ToString())); } byte[] buffer = SimpleFormatters.ObjectToBytes(temp); byte[] count = BitConverter.GetBytes(buffer.Length); writer.Write(count, 0, 4); writer.Write(buffer, 0, buffer.Length); break; } case MessageType.FilesAsk: { byte[] buffer = SimpleFormatters.ObjectToBytes(options); byte[] count = BitConverter.GetBytes(buffer.Length); writer.Write(count, 0, 4); writer.Write(buffer, 0, buffer.Length); break; } case MessageType.FileDataFirst: { char[] symbols = ((string)options).ToCharArray(); byte[] buffer = System.Text.Encoding.Unicode.GetBytes(symbols); byte[] count = BitConverter.GetBytes(buffer.Length); writer.Write(count, 0, 4); writer.Write(buffer, 0, buffer.Length); break; } case MessageType.FileData: { byte[] buffer = (byte[])options; byte[] count = BitConverter.GetBytes(buffer.Length); writer.Write(count, 0, 4); writer.Write(buffer, 0, buffer.Length); break; } case MessageType.FileDataLast: { break; } default: { break; } } }
public override void Receive(MessageType msgType, NetworkStream stream, IPAddress owner) { switch (msgType) { case MessageType.FilesTableAvailable: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int size = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); object filesData = SimpleFormatters.BytesToObject(buffer); RecievedFileTable(this, new ReceivedMessageEventArgs(MessageType.FilesTableAvailable, filesData, owner)); break; } case MessageType.FilesAsk: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int size = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); string file = (string)SimpleFormatters.BytesToObject(buffer); FileServerULMessage msg = new FileServerULMessage(file, owner); UploadQueue.Enqueue(msg); if (Sending == false) { this.AfterConnectionClosed += new Action(StartUploadFile); } break; } case MessageType.FileDataFirst: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int len = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[len]; stream.Read(buffer, 0, len); char[] symbols = new char[len]; symbols = System.Text.Encoding.Unicode.GetChars(buffer); string[] words = new string(symbols).Split(); string name = String.Join(" ", words, 0, words.Length - 1); long lenght = Convert.ToInt64(words[words.Length - 1]); FileServerDLMessage thisFIle = new FileServerDLMessage(name, SaveDirs[name], owner, lenght); if (Writing == true) { DownloadQueue.Enqueue(thisFIle); } else { StartWriteFile(thisFIle); } break; } case MessageType.FileData: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int len = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[len]; int readed = 0, offset = 0, toRead = len; while (true) { readed += stream.Read(buffer, offset, toRead); if (readed < len) { offset = readed; toRead = len - readed; } else { break; } } AppendToFile(buffer); break; } case MessageType.FileDataLast: { CloseFile(); break; } default: { break; } } }