Skip to content

Commit

Permalink
修复传大文件丢包的bug
Browse files Browse the repository at this point in the history
修复传大文件丢包的bug
  • Loading branch information
qieangel2013 committed Aug 25, 2016
1 parent 91a0519 commit 1f9d14e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php

php:
- 5.5
- 5.6
- 7.0

script:
- exit 0

3 changes: 1 addition & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
define('ServerLog',dirname(__DIR__).'/log/FileDistributed.log');
define('redis_server','192.168.102.163');
define('redis_port','6379');
define('MYPATH',dirname(__DIR__));
define('LISTENPATH',dirname(__DIR__));
define('LISTENPATH',dirname(__DIR__).'/img');
?>
Binary file added img/t018349127914f495ce.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions server/FileDistributedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public function __construct()
public function addServerClient($address)
{
$client = new swoole_client(SWOOLE_TCP, SWOOLE_SOCK_ASYNC);
/*$client->set(array(
'socket_buffer_size' => 1024 * 1024 * 2,
'open_eof_check' => true,
'package_eof' => "\r\n\r\n",
));*/
$client->on('Connect', array(
&$this,
'onConnect'
Expand Down Expand Up @@ -80,6 +75,16 @@ public function onReceive($client, $data)
{
$remote_info = json_decode($data, true);
if ($remote_info['type'] == 'filemes') {
$strlendata = file_get_contents($remote_info['data']['path']);
$datas = array(
'type' => 'filesize',
'data' => array(
'path' => $remote_info['data']['path'],
'filesize' => strlen($strlendata)
)
);
$client->send(json_encode($datas, true));
} else if ($remote_info['type'] == 'filesizemes') {
if ($client->sendfile($remote_info['data']['path'])) {
}
}
Expand Down
63 changes: 40 additions & 23 deletions server/FileDistributedServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class FileDistributedServer
private $curpath;
private $curtmp;
private $filefd;
private $filesizes;
private $tmpdata;
private $wd = array();
public function __construct()
{
Expand Down Expand Up @@ -93,9 +95,9 @@ public function onStart($serv)
'client' => $localclient
);
$this->filefd = inotify_init();
$watch_descriptor = inotify_add_watch($this->filefd, LISTENPATH, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE); //IN_MODIFY、IN_ALL_EVENTS、IN_CLOSE_WRITE
$this->wd[$watch_descriptor] = array(
'wd' => $watch_descriptor,
$wd = inotify_add_watch($this->filefd, LISTENPATH, IN_CREATE | IN_MOVED_TO | IN_CLOSE_WRITE); //IN_MODIFY、IN_ALL_EVENTS、IN_CLOSE_WRITE
$this->wd[$wd] = array(
'wd' => $wd,
'path' => LISTENPATH
);
swoole_event_add($this->filefd, function($fd) use ($localclient)
Expand All @@ -105,10 +107,10 @@ public function onStart($serv)
foreach ($events as $kk => $vv) {
if (isset($vv['name'])) {
if ($vv['mask'] == 1073742080) {
$wd = inotify_add_watch($this->filefd, LISTENPATH . $vv['name'], IN_MOVED_TO | IN_CLOSE_WRITE);
$wd = inotify_add_watch($this->filefd, LISTENPATH . '/' . $vv['name'], IN_MOVED_TO | IN_CLOSE_WRITE);
$this->wd[$wd] = array(
'wd' => $wd,
'path' => LISTENPATH . $vv['name']
'path' => LISTENPATH . '/' . $vv['name']
);
} else {
$path_listen = $this->wd[$vv['wd']]['path'] . '/' . $vv['name'];
Expand Down Expand Up @@ -174,30 +176,33 @@ public function onReceive($serv, $fd, $from_id, $data)
} else {
mkdir(dirname($this->curpath['path']), 0777, true);
}
$infofile = pathinfo($this->curpath['path']);
if (in_array($infofile['extension'], array(
'txt',
'log'
))) {
if (file_put_contents($this->curpath['path'], $data, FILE_APPEND)) {
}
} else {

$this->tmpdata .= $data;
if (strlen($this->tmpdata) == $this->filesizes) {
$infofile = pathinfo($this->curpath['path']);
if (in_array($infofile['extension'], array(
'jpg',
'png',
'jpeg',
'JPG',
'JPEG',
'PNG',
'bmp'
'txt',
'log'
))) {
if (strlen($data) > 1000) {
if (file_put_contents($this->curpath['path'], $data, FILE_APPEND)) {
if (file_put_contents($this->curpath['path'], $this->tmpdata)) {
$this->tmpdata = 0;
}
} else {
if (in_array($infofile['extension'], array(
'jpg',
'png',
'jpeg',
'JPG',
'JPEG',
'PNG',
'bmp'
))) {
if (file_put_contents($this->curpath['path'], $this->tmpdata)) {
$this->tmpdata = 0;
} //写入图片流
}
}
}

}
} else {
if ($remote_info['type'] == 'system' && $remote_info['data']['code'] == 10001) {
Expand Down Expand Up @@ -226,6 +231,18 @@ public function onReceive($serv, $fd, $from_id, $data)
}
} else {
switch ($remote_info['type']) {
case 'filesize':
if (isset($remote_info['data']['path'])) {
$data_s = array(
'type' => 'filesizemes',
'data' => array(
'path' => $remote_info['data']['path']
)
);
$this->filesizes = $remote_info['data']['filesize'];
$serv->send($fd, json_encode($data_s, true));
}
break;
case 'file':
if (isset($remote_info['data']['path'])) {
$this->curpath = $remote_info['data'];
Expand Down

0 comments on commit 1f9d14e

Please sign in to comment.