示例#1
0
        }             //Main

        void TCPListen()
        {
            try {
                TCP.Start();

                while (true)
                {
                    TcpClient Client = TCP.AcceptTcpClient();

                    //Program.ConsoleWriteLine(Client.Client.RemoteEndPoint.ToString() + " Connecting", Color.GreenYellow, Color.Black);

                    Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                    var ClientStream = Client.GetStream();

                    if (this.EnableAudio == false)
                    {
                        SCMessage message = new SCMessage {
                            MessageType = SCMessageType.Send_Client_Connection_Info, UDP_Video_Port = 1
                        };

                        var bytes = SCMessage.SerializeJSON_ToBytes(message);

                        ClientStream.Write(bytes, 0, bytes.Length);
                    }//if
                    else
                    {
                        SCMessage message = new SCMessage {
                            MessageType = SCMessageType.Send_Client_Connection_Info, UDP_Video_Port = 1, UDP_Audio_Port = 1
                        };

                        var bytes = SCMessage.SerializeJSON_ToBytes(message);

                        ClientStream.Write(bytes, 0, bytes.Length);
                    }//else

                    int BytesRead = 0;

                    try {
                        BytesRead = ClientStream.Read(this.MessageBuffer, 0, this.MessageBuffer.Length);

                        var message = SCMessage.DeserializeJSON_FromBytes(this.MessageBuffer, 0, BytesRead);

                        if (message.MessageType == SCMessageType.Send_Client_Connection_Info)
                        {
                            string Password = message.Message;

                            if (this.Password != Password)
                            {
                                SCMessage scmessage = new SCMessage {
                                    MessageType = SCMessageType.Invalid_Password
                                };

                                var bytes = SCMessage.SerializeJSON_ToBytes(scmessage);

                                ClientStream.Write(bytes, 0, bytes.Length);

                                Program.ConsoleWriteLine(Client.Client.RemoteEndPoint.ToString() + " (Invalid Password)", Color.Red, Color.Black);

                                Client.Close();

                                continue;
                            }//if

                            string NickName = message.From;

                            var checkNick = this.Clients.FirstOrDefault(c => c.NickName == NickName);

                            if (checkNick != null)
                            {
                                SCMessage scmessage = new SCMessage {
                                    MessageType = SCMessageType.Invalid_NickName
                                };

                                var bytes = SCMessage.SerializeJSON_ToBytes(scmessage);

                                ClientStream.Write(bytes, 0, bytes.Length);

                                Program.ConsoleWriteLine(Client.Client.RemoteEndPoint.ToString() + " (Invalid NickName)", Color.Red, Color.Black);

                                Client.Close();

                                continue;
                            }//if

                            SCMessage scmessageOK = new SCMessage {
                                MessageType = SCMessageType.Connection_OK, X = Width_X, Y = Height_Y
                            };

                            var bytesOK = SCMessage.SerializeJSON_ToBytes(scmessageOK);

                            ClientStream.Write(bytesOK, 0, bytesOK.Length);

                            BytesRead = ClientStream.Read(this.MessageBuffer, 0, this.MessageBuffer.Length);

                            scmessageOK = SCMessage.DeserializeJSON_FromBytes(this.MessageBuffer, 0, BytesRead);

                            if (scmessageOK.MessageType == SCMessageType.Connection_OK)
                            {
                                Program.ConsoleWriteLine(Client.Client.RemoteEndPoint.ToString() + $" Connected ({NickName})", Color.GreenYellow, Color.Black);

                                int Video_Udp_Port = message.UDP_Video_Port;

                                int Audio_Udp_Port = -1;

                                if (this.EnableAudio)
                                {
                                    Audio_Udp_Port = message.UDP_Audio_Port;
                                }//if

                                this.SendAll_Clients(SCMessage.SerializeJSON_ToBytes(new SCMessage {
                                    Message = NickName, MessageType = SCMessageType.ClientConnected_Message
                                }));

                                AddClientInfo(Client, Video_Udp_Port, Audio_Udp_Port, NickName);
                            }//if
                            else
                            {
                                continue;
                            }
                        }//if
                        else
                        {
                            Disconnect(Client);

                            continue;
                        } //else
                    }     //try
                    catch {
                        Disconnect(Client);

                        continue;
                    }//catch

                    Task.Run(() => {
                        try {
                            this.SendAllClientsOnline(Client);

                            int countRead = 0;

                            SCMessage scmessage = null;

                            byte[] clientMessage = new byte[MessageBufferSIZE];

                            while (true)
                            {
                                countRead = ClientStream.Read(clientMessage, 0, clientMessage.Length);

                                scmessage = SCMessage.DeserializeJSON_FromBytes(clientMessage, 0, countRead);

                                if (scmessage.MessageType == SCMessageType.Mouse_Click)
                                {
                                    MouseClick mouseclick = new MouseClick();

                                    mouseclick.Message = scmessage.From;

                                    mouseclick.X = scmessage.X;

                                    mouseclick.Y = scmessage.Y;

                                    MouseClick.DrawMouseClick(mouseclick);
                                }//if
                                else if (scmessage.MessageType == SCMessageType.PublicMessage)
                                {
                                    Program.ConsoleWriteLine($"{scmessage.From} : {scmessage.Message}", Program.Orange, Color.Black);

                                    var sender = this.Clients.FirstOrDefault(c => c.NickName == scmessage.From);

                                    if (sender != null)
                                    {
                                        this.SendAll_Clients(clientMessage, 0, countRead, sender);
                                    }
                                }//else if
                                else if (scmessage.MessageType == SCMessageType.PrivateMessage)
                                {
                                    var toClient = this.Clients.FirstOrDefault(c => c.NickName == scmessage.To);

                                    if (toClient != null)
                                    {
                                        this.SendTCP(clientMessage, 0, countRead, toClient.Client);
                                    }
                                }//else if
                                else if (scmessage.MessageType == SCMessageType.DeleteFileServer)
                                {
                                    var fname = scmessage.Message;

                                    var NickName_ = scmessage.From;

                                    Task.Run(() => {
                                        while (true)
                                        {
                                            try {
                                                if (File.Exists($"{Program.SV_Directory}/{fname}"))
                                                {
                                                    File.Delete($"{Program.SV_Directory}/{fname}");

                                                    return;
                                                } //if
                                            }     //try
                                            catch {
                                            }     //catch

                                            Thread.Sleep(1000);
                                        }//while
                                    });

                                    scmessage = new SCMessage {
                                        MessageType = SCMessageType.FileDeleted, Message = fname
                                    };

                                    var bytes_ = SCMessage.SerializeJSON_ToBytes(scmessage);

                                    var sender = this.Clients.FirstOrDefault(c => c.NickName == NickName_);

                                    if (sender != null)
                                    {
                                        this.SendAll_Clients(bytes_, 0, bytes_.Length, sender);
                                    }
                                }//else if
                                else if (scmessage.MessageType == SCMessageType.SendFile)
                                {
                                    string NickName_ = scmessage.From;

                                    SCMessage cMessage = new SCMessage {
                                        MessageType = SCMessageType.FileTCP_Port, UDP_Video_Port = this.FileSendTCP_Port
                                    };

                                    var bytes__ = SCMessage.SerializeJSON_ToBytes(cMessage);

                                    ClientStream.Write(bytes__, 0, bytes__.Length);

                                    Task.Run(() => {
                                        TcpClient client = null;

                                        try {
                                            if (!Directory.Exists(Program.SV_Directory))
                                            {
                                                Directory.CreateDirectory(Program.SV_Directory);
                                            }//if

                                            client = this.FileSendTCP.AcceptTcpClient();

                                            var NS = client.GetStream();

                                            var fname = $"{DateTime.Now.Ticks}_{scmessage.Message}";

                                            FileStream fileStream = new FileStream($"{Program.SV_Directory}/{fname}", FileMode.Create, FileAccess.Write);

                                            const long SendSize = 999;

                                            byte[] buffer = new byte[SendSize];

                                            byte[] messagebyffer = new byte[4] {
                                                7, 7, 7, 7
                                            };

                                            NS.Write(messagebyffer, 0, messagebyffer.Length);

                                            var fnameBytes = Encoding.UTF8.GetBytes(fname);

                                            NS.Write(fnameBytes, 0, fnameBytes.Length);

                                            while (true)
                                            {
                                                countRead = NS.Read(buffer, 0, buffer.Length);

                                                if (countRead == 9)
                                                {
                                                    bool error = true;

                                                    for (int j = 0; j < 9; j++)
                                                    {
                                                        if (buffer[j] != 7)
                                                        {
                                                            error = false;

                                                            return;
                                                        } //if
                                                    }     //for

                                                    if (error)
                                                    {
                                                        break;
                                                    }

                                                    fileStream.Write(buffer, 0, countRead);

                                                    NS.Write(messagebyffer, 0, messagebyffer.Length);

                                                    fileStream.Close();

                                                    break;
                                                }//if
                                                else if (countRead < SendSize)
                                                {
                                                    fileStream.Write(buffer, 0, countRead);

                                                    NS.Write(messagebyffer, 0, messagebyffer.Length);

                                                    fileStream.Close();

                                                    break;
                                                }//else if
                                                else
                                                {
                                                    fileStream.Write(buffer, 0, countRead);

                                                    NS.Write(messagebyffer, 0, messagebyffer.Length);
                                                } //else
                                            }     //while

                                            SCMessage sCMessage = new SCMessage {
                                                MessageType = SCMessageType.SendFileName, Message = fname
                                            };

                                            var bytes___ = SCMessage.SerializeJSON_ToBytes(sCMessage);

                                            var sender = this.Clients.FirstOrDefault(c => c.NickName == NickName_);

                                            if (sender != null)
                                            {
                                                this.SendAll_Clients(bytes___, 0, bytes___.Length, sender);
                                            }
                                        }//try
                                        catch (Exception ex) {
                                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }//catch

                                        if (client != null)
                                        {
                                            client.Close();
                                        }
                                    });
                                }//else if
                                else if (scmessage.MessageType == SCMessageType.SendFileName)
                                {
                                    if (File.Exists($"{Program.SV_Directory}/{scmessage.Message}") == false)
                                    {
                                        scmessage = new SCMessage {
                                            MessageType = SCMessageType.FileNotFound, Message = scmessage.Message
                                        };

                                        var bytes = SCMessage.SerializeJSON_ToBytes(scmessage);

                                        ClientStream.Write(bytes, 0, bytes.Length);

                                        return;
                                    }//if
                                    else
                                    {
                                        SCMessage cMessage = new SCMessage {
                                            MessageType = SCMessageType.SendFile, UDP_Video_Port = this.FileSendTCP_Port
                                        };

                                        var bytes = SCMessage.SerializeJSON_ToBytes(cMessage);

                                        ClientStream.Write(bytes, 0, bytes.Length);
                                    }//else

                                    Task.Run(() => {
                                        TcpClient client = null;

                                        try {
                                            client = this.FileSendTCP.AcceptTcpClient();

                                            var NS = client.GetStream();

                                            var fname = scmessage.Message;

                                            try {
                                                const long SendSize = 999;

                                                byte[] read = new byte[4];

                                                NS.Read(read, 0, read.Length);

                                                FileStream fileStream = new FileStream($"{Program.SV_Directory}/{fname}", FileMode.Open, FileAccess.Read);

                                                FileInfo fileInfo = new FileInfo($"{Program.SV_Directory}/{fname}");

                                                var countFullSend = (int)(fileInfo.Length / SendSize);

                                                var lastBytes = (int)(fileInfo.Length - (countFullSend * SendSize));

                                                int i = 0;

                                                byte[] buffer = new byte[SendSize];

                                                while (i < countFullSend)
                                                {
                                                    fileStream.Read(buffer, 0, buffer.Length);

                                                    NS.Write(buffer, 0, buffer.Length);

                                                    NS.Read(read, 0, read.Length);

                                                    i++;
                                                }//while

                                                if (lastBytes == 0)
                                                {
                                                    ClientStream.Write(new byte[9] {
                                                        7, 7, 7, 7, 7, 7, 7, 7, 7
                                                    }, 0, 9);
                                                }//if
                                                else
                                                {
                                                    fileStream.Read(buffer, 0, lastBytes);

                                                    NS.Write(buffer, 0, lastBytes);

                                                    NS.Read(read, 0, read.Length);

                                                    fileStream.Close();
                                                } //else
                                            }     //try
                                            catch (Exception ex) {
                                                ClientStream.Write(new byte[9] {
                                                    7, 7, 7, 7, 7, 7, 7, 7, 7
                                                }, 0, 9);

                                                MessageBox.Show(ex.Message, "Send File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            } //catch
                                        }     //try
                                        catch (Exception ex) {
                                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }//catch

                                        if (client != null)
                                        {
                                            client.Close();
                                        }
                                    });
                                } //else if
                            }     //while
                        }         //try
                        catch {
                            Disconnect(Client);

                            return;
                        } //catch
                    });
                }         //while
            }             //try
            catch (Exception ex) {
                //Program.ConsoleWriteLine($"TCP Error: {ex.Message}", Program.Red, Color.Black);
            } //catch
        }     //TCPListen