コード例 #1
0
 /// <summary>
 /// 生成新随机数用于 FileStreamId
 /// </summary>
 /// <returns></returns>
 private int GenerateRandomFileStreamId(int max)
 {
     lock (this.ServerFileSet)
     {
         Random rd = new Random();
         for (int id = rd.Next(0, max - 1); ; id = rd.Next(0, max - 1))
         {
             bool match = false;
             foreach (int setid in ServerFileSet.Keys)
             {
                 if (setid == id)
                 {
                     match = true;
                     break;
                 }
             }
             if (!match)
             {
                 Display.TimeWriteLine("Created file id : " + id.ToString());
                 return(id);
             }
         }
     }
 }
コード例 #2
0
        public void ReceiveData(object acceptSocketObject)
        {
            Socket client = (Socket)acceptSocketObject;

            client.SendTimeout    = Config.SocketSendTimeOut;
            client.ReceiveTimeout = Config.SocketReceiveTimeOut;
            int error_count = 0;

            while (flag_receive & error_count < 5)
            {
                try
                {
                    ReceiveBytes(client, out HB32Header header, out byte[] bytes);
                    //Display.TimeWriteLine(header.Flag.ToString());
                    switch (header.Flag)
                    {
                    case SocketDataFlag.DirectoryRequest:
                        ResponseDirectory(client, bytes);
                        break;

                    case SocketDataFlag.DirectorySizeRequest:
                        ResponseDirectorySize(client, bytes);
                        break;

                    case SocketDataFlag.CreateDirectoryRequest:
                        ResponseCreateDirectory(client, bytes);
                        break;

                        #region download
                    case SocketDataFlag.DownloadRequest:
                        ResponseDownloadSmallFile(client, bytes);
                        break;

                    case SocketDataFlag.DownloadFileStreamIdRequest:
                        ResponseFileStreamId(client, header, bytes, false);
                        break;

                    case SocketDataFlag.DownloadPacketRequest:
                        ResponseTransferPacket(client, header, bytes, false);
                        break;
                        #endregion

                        #region upload
                    case SocketDataFlag.UploadRequest:
                        ResponseUploadSmallFile(client, bytes);
                        break;

                    case SocketDataFlag.UploadFileStreamIdRequest:
                        ResponseFileStreamId(client, header, bytes, true);
                        break;

                    case SocketDataFlag.UploadPacketRequest:
                        ResponseTransferPacket(client, header, bytes, true);
                        break;
                        #endregion

                    case SocketDataFlag.DisconnectRequest:
                        client.Close();
                        return;

                    default:
                        throw new Exception("Invalid socket header in receiving: " + header.Flag.ToString());
                    }
                    error_count = 0;
                }
                catch (SocketException ex)
                {
                    error_count++;
                    switch (ex.ErrorCode)
                    {
                    // 远程 client 主机关闭连接
                    case 10054:
                        client.Close();
                        Display.WriteLine("connection closed (remote closed)");
                        return;

                    // Socket 超时
                    case 10060:
                        Thread.Sleep(200);
                        continue;

                    default:
                        //System.Windows.Forms.MessageBox.Show("Server receive data :" + ex.Message);
                        Display.WriteLine("Server receive data :" + ex.Message);
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    error_count++;
                    if (ex.Message.Contains("Buffer receive error: cannot receive package"))
                    {
                        client.Close();
                        //Display.TimeWriteLine("connection closed (buffer received none)");
                        return;
                    }
                    if (ex.Message.Contains("Invalid socket header"))
                    {
                        client.Close();
                        Display.TimeWriteLine("connection closed : " + ex.Message);
                        return;
                    }
                    Display.TimeWriteLine("Server exception :" + ex.Message);
                    Thread.Sleep(200);
                    continue;
                }
            }
            Display.TimeWriteLine("Connection closed: error count 5");
        }